Skip to content

Commit e2bcfd6

Browse files
committed
classes make encapsulation.
1 parent d03d570 commit e2bcfd6

20 files changed

+304
-197
lines changed

dogfood-package.png

-595 Bytes
Loading

dogfood.png

45.8 KB
Loading

src/DiagramElement/Arrow.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,21 @@
88

99
abstract class Arrow {
1010
protected string $figure = '..>';
11-
public PhpClass $from;
12-
public PhpType $to;
11+
private PhpClass $from;
12+
private PhpType $to;
1313

1414
public function __construct(PhpClass $from, PhpType $to) {
1515
$this->from = $from;
1616
$this->to = $to;
1717
}
1818

19+
public function getFrom(): PhpClass {
20+
return $this->from;
21+
}
22+
23+
public function getTo(): PhpType {
24+
return $this->to;
25+
}
26+
1927
abstract public function toString(PhpClass $toClass): string;
2028
}

src/DiagramElement/ArrowDependency.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ class ArrowDependency extends Arrow {
77
protected string $figure = '..>';
88

99
public function toString(PhpClass $toClass): string {
10-
if (strpos($this->to->name, '[]') === false) {
11-
return sprintf(' %s %s %s', $this->from->getLogicalName(), $this->figure, $toClass->getLogicalName());
10+
if (strpos($this->getTo()->getName(), '[]') === false) {
11+
return sprintf(' %s %s %s', $this->getFrom()->getLogicalName(), $this->figure, $toClass->getLogicalName());
1212
}
13-
return sprintf(' %s "1" %s "*" %s', $this->from->getLogicalName(), $this->figure, str_replace('[]', '', $toClass->getLogicalName()));
13+
return sprintf(' %s "1" %s "*" %s', $this->getFrom()->getLogicalName(), $this->figure, str_replace('[]', '', $toClass->getLogicalName()));
1414
}
1515
}

src/DiagramElement/ArrowInheritance.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ class ArrowInheritance extends Arrow {
77
protected string $figure = '<|--';
88

99
public function toString(PhpClass $toClass): string {
10-
return sprintf(' %s %s %s', $toClass->getLogicalName(), $this->figure, $this->from->getLogicalName());
10+
return sprintf(' %s %s %s', $toClass->getLogicalName(), $this->figure, $this->getFrom()->getLogicalName());
1111
}
1212
}

src/DiagramElement/Entry.php

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,49 @@
55
use Smeghead\PhpClassDiagram\Php\ {
66
PhpClass,
77
PhpAccessModifier,
8+
PhpMethodParameter,
89
};
910

1011
class Entry {
11-
public Options $options;
12-
public string $directory;
13-
public PhpClass $class;
12+
private Options $options;
13+
private string $directory;
14+
private PhpClass $class;
15+
1416
public function __construct(string $directory, PhpClass $class, Options $options) {
1517
$this->directory = $directory;
1618
$this->class = $class;
1719
$this->options = $options;
1820
}
1921

22+
public function getOptions(): Options {
23+
return $this->options;
24+
}
25+
26+
public function getDirectory(): string {
27+
return $this->directory;
28+
}
29+
30+
public function getClass(): PhpClass {
31+
return $this->class;
32+
}
33+
2034
public function dump($level = 0): array {
2135
$indent = str_repeat(' ', $level);
2236
$lines = [];
23-
$meta = $this->class->getClassType()->meta === 'Stmt_Interface' ? 'interface' : 'class';
37+
$meta = $this->class->getClassType()->getMeta() === 'Stmt_Interface' ? 'interface' : 'class';
2438
if ($this->options->classProperties() || $this->options->classMethods()) {
2539
$lines[] = sprintf('%s%s %s {', $indent, $meta, $this->class->getLogicalName());
2640
if ($this->options->classProperties()) {
2741
foreach ($this->class->getProperties() as $p) {
28-
$lines[] = sprintf(' %s%s%s : %s', $indent, $this->modifier($p->accessModifier), $p->name, $p->type->name);
42+
$lines[] = sprintf(' %s%s%s : %s', $indent, $this->modifier($p->getAccessModifier()), $p->getName(), $p->getType()->getName());
2943
}
3044
}
3145
if ($this->options->classMethods()) {
3246
foreach ($this->class->getMethods() as $m) {
33-
$params = array_map(function($x){
34-
return $x->name;
35-
}, $m->params);
36-
$lines[] = sprintf(' %s%s%s(%s)', $indent, $this->modifier($m->accessModifier), $m->name, implode(', ', $params));
47+
$params = array_map(function(PhpMethodParameter $x){
48+
return $x->getName();
49+
}, $m->getParams());
50+
$lines[] = sprintf(' %s%s%s(%s)', $indent, $this->modifier($m->getAccessModifier()), $m->getName(), implode(', ', $params));
3751
}
3852
}
3953
$lines[] = sprintf('%s}', $indent);
@@ -45,19 +59,19 @@ public function dump($level = 0): array {
4559

4660
private function modifier(PhpAccessModifier $modifier): string {
4761
$expressions = [];
48-
if ($modifier->static) {
62+
if ($modifier->isStatic()) {
4963
$expressions[] = '{static}';
5064
}
51-
if ($modifier->abstract) {
65+
if ($modifier->isAbstract()) {
5266
$expressions[] = '{abstract}';
5367
}
54-
if ($modifier->public) {
68+
if ($modifier->isPublic()) {
5569
$expressions[] = '+';
5670
}
57-
if ($modifier->protected) {
71+
if ($modifier->isProtected()) {
5872
$expressions[] = '#';
5973
}
60-
if ($modifier->private) {
74+
if ($modifier->isPrivate()) {
6175
$expressions[] = '-';
6276
}
6377
return implode(' ' , $expressions);
@@ -67,16 +81,16 @@ public function getArrows(): array {
6781
$arrows = [];
6882
//フィールド変数の型に対しての依存をArrowとして追加する。
6983
foreach ($this->class->getProperties() as $p) {
70-
$arrows[] = new ArrowDependency($this->class, $p->type);
84+
$arrows[] = new ArrowDependency($this->class, $p->getType());
7185
}
7286
foreach ($this->class->getMethods() as $m) {
73-
if ( ! $m->accessModifier->public) {
87+
if ( ! $m->getAccessModifier()->isPublic()) {
7488
continue;
7589
}
76-
if (count($m->params) > 0) {
90+
if (count($m->getParams()) > 0) {
7791
continue;
7892
}
79-
$arrows[] = new ArrowDependency($this->class, $m->type);
93+
$arrows[] = new ArrowDependency($this->class, $m->getType());
8094
}
8195
$extends = $this->class->getExtends();
8296
if ( ! empty($extends)) {

src/DiagramElement/Package.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function getLogicalName(): string {
2828
public function addEntry(array $paths, Entry $entry): string {
2929
if (count($paths) === 0) {
3030
if (empty($this->package)) {
31-
$this->package = implode('.', $entry->class->getClassType()->namespace);
31+
$this->package = implode('.', $entry->getClass()->getClassType()->getNamespace());
3232
}
3333
$this->entries[] = $entry;
3434
return $this->package;
@@ -129,7 +129,7 @@ public function getEntries(): array {
129129
public function getUses($acc): array {
130130
$uses = [];
131131
foreach ($this->entries as $e) {
132-
$uses = array_merge($uses, $e->class->getUses());
132+
$uses = array_merge($uses, $e->getClass()->getUses());
133133
}
134134
$acc[$this->package] = $uses;
135135
foreach ($this->children as $n) {

src/DiagramElement/PackageRelations.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function getArrows(): array {
2626
$packageRelations = [];
2727
foreach ($this->uses as $namespace => $us) {
2828
$packages = array_unique(array_map(function($x){
29-
return implode('.', $x->namespace);
29+
return implode('.', $x->getNamespace());
3030
}, $us));
3131
// 対象となっているpackage以外のpackageは、即席で定義する必要がある。
3232
$all = array_unique(array_merge($all, $packages));

src/DiagramElement/Relation.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@ class Relation {
77
private Options $options;
88
private Package $package;
99

10+
/**
11+
* @param Entry[] $entries
12+
*/
1013
public function __construct(array $entries, Options $options) {
1114
$this->options = $options;
1215
$this->package = new Package([], 'ROOT', $options);
1316
foreach ($entries as $e) {
14-
$this->package->addEntry(preg_split('/[\\\\\/]/', $e->directory), $e);
17+
$this->package->addEntry(preg_split('/[\\\\\/]/', $e->getDirectory()), $e);
1518
}
1619
}
1720

@@ -31,10 +34,10 @@ public function dump(): array {
3134

3235
public function getRelations(): array {
3336
$entities = $this->package->getEntries();
34-
$relation_expressions = array_map(function($x) use ($entities){
37+
$relation_expressions = array_map(function(Arrow $x) use ($entities){
3538
foreach ($entities as $e) {
36-
if ($e->class->getClassType()->equals($x->to)) {
37-
return $x->toString($e->class);
39+
if ($e->getClass()->getClassType()->equals($x->getTo())) {
40+
return $x->toString($e->getClass());
3841
}
3942
}
4043
return null;

src/Php/PhpAccessModifier.php

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
};
88

99
class PhpAccessModifier {
10-
public bool $public = false;
11-
public bool $protected = false;
12-
public bool $private = false;
13-
public bool $abstract = false;
14-
public bool $final = false;
15-
public bool $static = false;
10+
protected bool $public = false;
11+
protected bool $protected = false;
12+
protected bool $private = false;
13+
protected bool $abstract = false;
14+
protected bool $final = false;
15+
protected bool $static = false;
1616

1717
public function __construct(Stmt $stmt) {
1818
$this->public = $stmt->isPublic();
@@ -23,4 +23,28 @@ public function __construct(Stmt $stmt) {
2323
$this->abstract = $stmt->isAbstract();
2424
}
2525
}
26+
27+
public function isPublic(): bool {
28+
return $this->public;
29+
}
30+
31+
public function isProtected(): bool {
32+
return $this->protected;
33+
}
34+
35+
public function isPrivate(): bool {
36+
return $this->private;
37+
}
38+
39+
public function isAbstract(): bool {
40+
return $this->abstract;
41+
}
42+
43+
public function isFinal(): bool {
44+
return $this->final;
45+
}
46+
47+
public function isStatic(): bool {
48+
return $this->static;
49+
}
2650
}

0 commit comments

Comments
 (0)