From 8dde77667621d7e806d005a9f6878c72bbfb38fc Mon Sep 17 00:00:00 2001 From: smeghead Date: Wed, 1 Mar 2023 21:57:09 +0900 Subject: [PATCH] format source files. --- composer.json | 4 + src/Config/Options.php | 41 ++++++---- src/DiagramElement/Arrow.php | 27 +++--- src/DiagramElement/ArrowDependency.php | 11 ++- src/DiagramElement/ArrowInheritance.php | 11 ++- src/DiagramElement/Entry.php | 39 +++++---- .../ExternalPackage/PackageHierarchy.php | 31 ++++--- src/DiagramElement/Package.php | 53 ++++++++---- src/DiagramElement/PackageArrow.php | 20 +++-- src/DiagramElement/PackageRelations.php | 36 +++++--- src/DiagramElement/Relation.php | 32 +++++--- src/Main.php | 39 +++++---- src/Php/PhpAccessModifier.php | 33 +++++--- src/Php/PhpClass.php | 82 +++++++++++-------- src/Php/PhpMethod.php | 34 +++++--- src/Php/PhpMethodParameter.php | 17 ++-- src/Php/PhpProperty.php | 22 +++-- src/Php/PhpReader.php | 28 ++++--- src/Php/PhpType.php | 34 +++++--- src/Php/PhpTypeExpression.php | 45 ++++++---- 20 files changed, 415 insertions(+), 224 deletions(-) diff --git a/composer.json b/composer.json index 5280047..151eb8f 100644 --- a/composer.json +++ b/composer.json @@ -2,6 +2,10 @@ "name": "smeghead/php-class-diagram", "description": "A CLI tool that parses the PHP source directory and outputs PlantUML scripts.", "type": "library", + "keywords": [ + "classdiagram", + "PlantUML" + ], "require": { "php" : ">=8.0", "symfony/finder": "^5.3", diff --git a/src/Config/Options.php b/src/Config/Options.php index fe4fffd..787ee30 100644 --- a/src/Config/Options.php +++ b/src/Config/Options.php @@ -1,7 +1,11 @@ -opt = $opt; } - public function help(): bool { + public function help(): bool + { if (isset($this->opt['h'])) { return true; } @@ -26,7 +32,8 @@ public function help(): bool { return false; } - public function diagram(): string { + public function diagram(): string + { if (isset($this->opt['class-diagram'])) { return self::DIAGRAM_CLASS; } @@ -40,7 +47,8 @@ public function diagram(): string { return self::DIAGRAM_CLASS; } - public function classProperties(): bool { + public function classProperties(): bool + { if (isset($this->opt['enable-class-properties'])) { return true; } @@ -51,7 +59,8 @@ public function classProperties(): bool { return true; } - public function classMethods(): bool { + public function classMethods(): bool + { if (isset($this->opt['enable-class-methods'])) { return true; } @@ -62,7 +71,8 @@ public function classMethods(): bool { return true; } - public function phpVersion(): string { + public function phpVersion(): string + { if (isset($this->opt['php5'])) { return self::PHP5; } @@ -79,11 +89,12 @@ public function phpVersion(): string { /** * @return string[] specified headers */ - public function headers(): array { + public function headers(): array + { $headers = []; if (isset($this->opt['header'])) { $headers = $this->opt['header']; - if ( ! is_array($headers)) { + if (!is_array($headers)) { $headers = [$headers]; } } @@ -93,11 +104,12 @@ public function headers(): array { /** * @return string[] specified includes */ - public function includes(): array { + public function includes(): array + { $includes = []; if (isset($this->opt['include'])) { $includes = $this->opt['include']; - if ( ! is_array($includes)) { + if (!is_array($includes)) { $includes = [$includes]; } } else { @@ -109,11 +121,12 @@ public function includes(): array { /** * @return string[] specified excludes */ - public function excludes(): array { + public function excludes(): array + { $excludes = []; if (isset($this->opt['exclude'])) { $excludes = $this->opt['exclude']; - if ( ! is_array($excludes)) { + if (!is_array($excludes)) { $excludes = [$excludes]; } } diff --git a/src/DiagramElement/Arrow.php b/src/DiagramElement/Arrow.php index 3d9edec..d8b7e2b 100644 --- a/src/DiagramElement/Arrow.php +++ b/src/DiagramElement/Arrow.php @@ -1,27 +1,34 @@ -'; private PhpClass $from; private PhpType $to; - public function __construct(PhpClass $from, PhpType $to) { + public function __construct(PhpClass $from, PhpType $to) + { $this->from = $from; $this->to = $to; } - public function getFrom(): PhpClass { - return $this->from; + public function getFrom(): PhpClass + { + return $this->from; } - public function getTo(): PhpType { - return $this->to; + public function getTo(): PhpType + { + return $this->to; } abstract public function toString(PhpClass $toClass): string; diff --git a/src/DiagramElement/ArrowDependency.php b/src/DiagramElement/ArrowDependency.php index 5f6451c..7a690ce 100644 --- a/src/DiagramElement/ArrowDependency.php +++ b/src/DiagramElement/ArrowDependency.php @@ -1,12 +1,17 @@ -'; - public function toString(PhpClass $toClass): string { + public function toString(PhpClass $toClass): string + { if (strpos($this->getTo()->getName(), '[]') === false) { return sprintf(' %s %s %s', $this->getFrom()->getClassNameAlias(), $this->figure, $toClass->getClassNameAlias()); } diff --git a/src/DiagramElement/ArrowInheritance.php b/src/DiagramElement/ArrowInheritance.php index 112728a..2dc81d5 100644 --- a/src/DiagramElement/ArrowInheritance.php +++ b/src/DiagramElement/ArrowInheritance.php @@ -1,12 +1,17 @@ -getClassNameAlias(), $this->figure, $this->getFrom()->getClassNameAlias()); } } diff --git a/src/DiagramElement/Entry.php b/src/DiagramElement/Entry.php index 9de6776..d8d2259 100644 --- a/src/DiagramElement/Entry.php +++ b/src/DiagramElement/Entry.php @@ -1,37 +1,46 @@ -directory = $directory; $this->class = $class; $this->options = $options; } - public function getOptions(): Options { + public function getOptions(): Options + { return $this->options; } - public function getDirectory(): string { + public function getDirectory(): string + { return $this->directory; } - public function getClass(): PhpClass { + public function getClass(): PhpClass + { return $this->class; } - public function dump($level = 0): array { + public function dump($level = 0): array + { $indent = str_repeat(' ', $level); $lines = []; // $meta = $this->class->getClassType()->getMeta() === 'Stmt_Interface' ? 'interface' : 'class'; @@ -45,7 +54,7 @@ public function dump($level = 0): array { } if ($this->options->classMethods()) { foreach ($this->class->getMethods() as $m) { - $params = array_map(function(PhpMethodParameter $x){ + $params = array_map(function (PhpMethodParameter $x) { return $x->getName(); }, $m->getParams()); $lines[] = sprintf(' %s%s%s(%s)', $indent, $this->modifier($m->getAccessModifier()), $m->getName(), implode(', ', $params)); @@ -58,7 +67,8 @@ public function dump($level = 0): array { return $lines; } - private function modifier(PhpAccessModifier $modifier): string { + private function modifier(PhpAccessModifier $modifier): string + { $expressions = []; if ($modifier->isStatic()) { $expressions[] = '{static}'; @@ -75,10 +85,11 @@ private function modifier(PhpAccessModifier $modifier): string { if ($modifier->isPrivate()) { $expressions[] = '-'; } - return implode(' ' , $expressions); + return implode(' ', $expressions); } - public function getArrows(): array { + public function getArrows(): array + { $arrows = []; //フィールド変数の型に対しての依存をArrowとして追加する。 foreach ($this->class->getProperties() as $p) { @@ -87,7 +98,7 @@ public function getArrows(): array { } } foreach ($this->class->getMethods() as $m) { - if ( ! $m->getAccessModifier()->isPublic()) { + if (!$m->getAccessModifier()->isPublic()) { continue; } foreach ($m->getParams() as $p) { @@ -100,7 +111,7 @@ public function getArrows(): array { } } $extends = $this->class->getExtends(); - if ( ! empty($extends)) { + if (!empty($extends)) { //継承先に対してArrowを追加する。 foreach ($extends as $extend) { $arrows[] = new ArrowInheritance($this->class, $extend); diff --git a/src/DiagramElement/ExternalPackage/PackageHierarchy.php b/src/DiagramElement/ExternalPackage/PackageHierarchy.php index 78171df..0a34f96 100644 --- a/src/DiagramElement/ExternalPackage/PackageHierarchy.php +++ b/src/DiagramElement/ExternalPackage/PackageHierarchy.php @@ -1,7 +1,11 @@ -externalPackages = $externalPackages; $this->root = new PackageNode('root'); foreach ($externalPackages as $p) { @@ -20,7 +25,8 @@ public function __construct(array $externalPackages) { } } - public function dump(): string { + public function dump(): string + { $elements = []; foreach ($this->root->getChildren() as $c) { $elements[] = $c->dump(1); @@ -29,26 +35,30 @@ public function dump(): string { } } -class PackageNode { +class PackageNode +{ private string $name; /** @var PackageNode[] children */ private array $children = []; - public function __construct(string $name) { + public function __construct(string $name) + { $this->name = $name; } /** * @return PackageNode[] */ - public function getChildren(): array { + public function getChildren(): array + { return $this->children; } /** * @param string[] $packages */ - public function register(array $packages): void { + public function register(array $packages): void + { if (count($packages) === 0) { return; } @@ -64,7 +74,8 @@ public function register(array $packages): void { $this->children[] = $new; } - public function dump(int $indent): string { + public function dump(int $indent): string + { $lines = []; $lines[] = sprintf('%spackage %s as %s #DDDDDD {', str_repeat(' ', $indent), $this->name, $this->name); foreach ($this->children as $c) { @@ -73,4 +84,4 @@ public function dump(int $indent): string { $lines[] = sprintf('%s}', str_repeat(' ', $indent)); return implode("\n", $lines); } -} \ No newline at end of file +} diff --git a/src/DiagramElement/Package.php b/src/DiagramElement/Package.php index 58286f9..0821a29 100644 --- a/src/DiagramElement/Package.php +++ b/src/DiagramElement/Package.php @@ -1,9 +1,13 @@ -parents = $parents; $this->name = $name; $this->options = $options; } - public function getLogicalName(): string { + public function getLogicalName(): string + { return implode('.', array_merge(array_slice($this->parents, 1), [$this->name])); } - public function addEntry(array $paths, Entry $entry): string { + public function addEntry(array $paths, Entry $entry): string + { if (count($paths) === 0) { if (empty($this->package)) { $this->package = implode('.', $entry->getClass()->getNamespace()); @@ -46,7 +53,8 @@ public function addEntry(array $paths, Entry $entry): string { return $this->package; } - private function findChild(string $dir): Package { + private function findChild(string $dir): Package + { if (empty($dir)) { return $this; } @@ -60,7 +68,8 @@ private function findChild(string $dir): Package { return end($this->children); } - public function dump($level = 0): array { + public function dump($level = 0): array + { $indent = str_repeat(' ', $level); $lines = []; if ($this->name !== 'ROOT') { @@ -83,7 +92,8 @@ public function dump($level = 0): array { return $lines; } - public function dumpPackages($level = 1): array { + public function dumpPackages($level = 1): array + { $indent = str_repeat(' ', $level); $lines = []; $target = empty($this->package) ? $this->getLogicalName() : $this->package; @@ -112,7 +122,8 @@ public function dumpPackages($level = 1): array { /** * @return Arrow[] 矢印一覧 */ - public function getArrows(): array { + public function getArrows(): array + { $arrows = []; foreach ($this->entries as $e) { $arrows = array_merge($arrows, $e->getArrows()); @@ -125,8 +136,9 @@ public function getArrows(): array { /** * @return Entry[] クラスなどの一覧 - */ - public function getEntries(): array { + */ + public function getEntries(): array + { $entries = $this->entries; foreach ($this->children as $n) { $entries = array_merge($entries, $n->getEntries()); @@ -136,8 +148,9 @@ public function getEntries(): array { /** * @return array useの一覧 - */ - public function getUses($acc): array { + */ + public function getUses($acc): array + { $uses = []; foreach ($this->entries as $e) { $uses = array_merge($uses, $e->getClass()->getUses()); @@ -153,7 +166,8 @@ public function getUses($acc): array { /** * 解析対象になっているpackage一覧を取得する。 */ - public function getTargetPackages($acc = []) { + public function getTargetPackages($acc = []) + { $acc[$this->package] = $this->getLogicalName(); foreach ($this->children as $n) { $acc = $n->getTargetPackages($acc); @@ -161,11 +175,13 @@ public function getTargetPackages($acc = []) { return $acc; } - public function findPackage(string $package): ?Package { + public function findPackage(string $package): ?Package + { return $this->recFindPackage($package); } - public function is(string $package): bool { + public function is(string $package): bool + { $segments = $this->parents; $segments[] = $this->name; return implode('.', $segments) === $package; @@ -175,13 +191,14 @@ public function is(string $package): bool { * @param string $package パッケージの表記(例: hoge.fuga) * @return ?Package */ - private function recFindPackage(string $package): ?Package { + private function recFindPackage(string $package): ?Package + { if ($this->is($package)) { return $this; } foreach ($this->children as $c) { $p = $c->recFindPackage($package); - if ( ! empty($p)) { + if (!empty($p)) { return $p; } } diff --git a/src/DiagramElement/PackageArrow.php b/src/DiagramElement/PackageArrow.php index 6713a46..d335944 100644 --- a/src/DiagramElement/PackageArrow.php +++ b/src/DiagramElement/PackageArrow.php @@ -1,21 +1,28 @@ -from = $from; $this->to = $to; } - public function bothSideArrow() { + public function bothSideArrow() + { $this->bothSideArrow = true; } - public function isOpposite(self $other) { + public function isOpposite(self $other) + { if ($this->from !== $other->to) { return false; } @@ -25,7 +32,8 @@ public function isOpposite(self $other) { return true; } - public function toString(): string { + public function toString(): string + { $format = $this->bothSideArrow ? ' %s <-[#red,plain,thickness=4]-> %s' : ' %s --> %s'; diff --git a/src/DiagramElement/PackageRelations.php b/src/DiagramElement/PackageRelations.php index 14e424d..422f3dc 100644 --- a/src/DiagramElement/PackageRelations.php +++ b/src/DiagramElement/PackageRelations.php @@ -1,10 +1,14 @@ - */ private array $uses; private Package $rootPackage; @@ -13,32 +17,35 @@ class PackageRelations { * @param array $uses * @param Package $rootPackage */ - public function __construct(array $uses, Package $rootPackage) { + public function __construct(array $uses, Package $rootPackage) + { $this->uses = $uses; $this->rootPackage = $rootPackage; } - private function displayPackage(string $package): string { + private function displayPackage(string $package): string + { $p = $this->rootPackage->findPackage($package); - if ( ! empty($p)) { + if (!empty($p)) { if (empty($p->package)) { return $p->getLogicalName(); } } return $package; //外部のpackageはpackage表示 } - - public function getArrows(): array { + + public function getArrows(): array + { $lines = []; $all = []; $packageRelations = []; foreach ($this->uses as $namespace => $us) { - $packageNames = array_unique(array_map(function(PhpType $x){ + $packageNames = array_unique(array_map(function (PhpType $x) { return implode('.', $x->getNamespace()); }, $us)); // 対象となっているpackage以外のpackageは、即席で定義する必要がある。 $all = array_unique(array_merge($all, $packageNames)); - $packageRelations[$namespace] = array_map(function(string $x) { + $packageRelations[$namespace] = array_map(function (string $x) { return $this->displayPackage($x); }, $packageNames); } @@ -67,7 +74,8 @@ public function getArrows(): array { return $lines; } - private function mergeBothSideArrow(array $arrows): array { + private function mergeBothSideArrow(array $arrows): array + { $merged = []; $bothSideArrows = []; foreach ($arrows as $a) { @@ -79,17 +87,19 @@ private function mergeBothSideArrow(array $arrows): array { } foreach ($bothSideArrows as $b) { foreach ($merged as &$m) { - if ( ! $m->isOpposite($b)) { + if (!$m->isOpposite($b)) { continue; } $m->bothSideArrow(); break; - } unset($m); + } + unset($m); } return $merged; } - private function existsUpsidedown(array $arrows, PackageArrow $arrow): bool { + private function existsUpsidedown(array $arrows, PackageArrow $arrow): bool + { foreach ($arrows as $a) { if ($a->isOpposite($arrow)) { return true; diff --git a/src/DiagramElement/Relation.php b/src/DiagramElement/Relation.php index 9bdc471..12eab0f 100644 --- a/src/DiagramElement/Relation.php +++ b/src/DiagramElement/Relation.php @@ -1,16 +1,21 @@ -options = $options; $this->package = new Package([], 'ROOT', $options); foreach ($entries as $e) { @@ -18,13 +23,17 @@ public function __construct(array $entries, Options $options) { } } - public function getPackage(): Package { + public function getPackage(): Package + { return $this->package; } - public function dump(): array { + public function dump(): array + { $lines = ['@startuml class-diagram']; - $lines = array_merge($lines, array_map(function($x){ return ' ' . $x;}, $this->options->headers())); + $lines = array_merge($lines, array_map(function ($x) { + return ' ' . $x; + }, $this->options->headers())); $lines = array_merge($lines, $this->package->dump()); $lines = array_merge($lines, $this->getRelations()); $lines[] = '@enduml'; @@ -32,9 +41,10 @@ public function dump(): array { return $lines; } - public function getRelations(): array { + public function getRelations(): array + { $entities = $this->package->getEntries(); - $relation_expressions = array_map(function(Arrow $x) use ($entities){ + $relation_expressions = array_map(function (Arrow $x) use ($entities) { foreach ($entities as $e) { if ($e->getClass()->getClassType()->equals($x->getTo())) { return $x->toString($e->getClass()); @@ -47,7 +57,8 @@ public function getRelations(): array { return array_unique($relation_expressions); } - public function dumpPackages(): array { + public function dumpPackages(): array + { $lines = ['@startuml package-related-diagram']; $lines = array_merge($lines, $this->options->headers()); $lines = array_merge($lines, $this->package->dumpPackages()); @@ -59,7 +70,8 @@ public function dumpPackages(): array { return $lines; } - public function getUses(): array { + public function getUses(): array + { return $this->package->getUses([]); } } diff --git a/src/Main.php b/src/Main.php index ce6a265..b1bef0e 100644 --- a/src/Main.php +++ b/src/Main.php @@ -1,18 +1,23 @@ -files()->in($directory); $finder->files()->name($options->includes()); @@ -33,18 +38,18 @@ public function __construct(string $directory, Options $options) { } $relation = new Relation($entries, $options); switch ($options->diagram()) { - case Options::DIAGRAM_CLASS: - echo implode("\r\n", $relation->dump()) . "\r\n"; - break; - case OPTIONS::DIAGRAM_PACKAGE: - echo implode("\r\n", $relation->dumpPackages()) . "\r\n"; - break; - case OPTIONS::DIAGRAM_JIG: - echo implode("\r\n", $relation->dump()) . "\r\n"; - echo implode("\r\n", $relation->dumpPackages()) . "\r\n"; - break; - default: - throw new \Exception('invalid diagram.'); + case Options::DIAGRAM_CLASS: + echo implode("\r\n", $relation->dump()) . "\r\n"; + break; + case OPTIONS::DIAGRAM_PACKAGE: + echo implode("\r\n", $relation->dumpPackages()) . "\r\n"; + break; + case OPTIONS::DIAGRAM_JIG: + echo implode("\r\n", $relation->dump()) . "\r\n"; + echo implode("\r\n", $relation->dumpPackages()) . "\r\n"; + break; + default: + throw new \Exception('invalid diagram.'); } } } diff --git a/src/Php/PhpAccessModifier.php b/src/Php/PhpAccessModifier.php index 531d51f..4e191fa 100644 --- a/src/Php/PhpAccessModifier.php +++ b/src/Php/PhpAccessModifier.php @@ -1,14 +1,18 @@ -public = $stmt->isPublic(); $this->protected = $stmt->isProtected(); $this->private = $stmt->isPrivate(); $this->static = $stmt->isStatic(); - if ($stmt instanceOf ClassMethod) { + if ($stmt instanceof ClassMethod) { $this->abstract = $stmt->isAbstract(); } } - public function isPublic(): bool { + public function isPublic(): bool + { return $this->public; } - public function isProtected(): bool { + public function isProtected(): bool + { return $this->protected; } - public function isPrivate(): bool { + public function isPrivate(): bool + { return $this->private; } - public function isAbstract(): bool { + public function isAbstract(): bool + { return $this->abstract; } - public function isFinal(): bool { + public function isFinal(): bool + { return $this->final; } - public function isStatic(): bool { + public function isStatic(): bool + { return $this->static; } } diff --git a/src/Php/PhpClass.php b/src/Php/PhpClass.php index 11b34f7..a61b399 100644 --- a/src/Php/PhpClass.php +++ b/src/Php/PhpClass.php @@ -1,7 +1,10 @@ -full = $full; } - public function getNamespace(): array { - foreach($this->full as $stmt) { - if ($stmt instanceOf Namespace_) { + public function getNamespace(): array + { + foreach ($this->full as $stmt) { + if ($stmt instanceof Namespace_) { return $stmt->name->parts; } } @@ -49,9 +55,10 @@ public function getNamespace(): array { * return logical name. * @return string logical name. */ - public function getLogicalName(): string { + public function getLogicalName(): string + { $type = $this->getClassType(); - $parts = $this->dirs; + $parts = $this->dirs; $parts[] = $type->getName(); return implode('.', $parts); } @@ -60,14 +67,16 @@ public function getLogicalName(): string { * return className alias in class-diagram. * @return string className alias */ - public function getClassNameAlias(): string { + public function getClassNameAlias(): string + { return str_replace(['.', '[', ']'], '_', $this->getLogicalName()); } - public function getClassType(): PhpType { + public function getClassType(): PhpType + { $namespace = []; foreach ($this->full as $stmt) { - if ($stmt instanceOf Namespace_) { + if ($stmt instanceof Namespace_) { $namespace = $stmt->name->parts; if ($namespace === null) { var_dump($stmt); @@ -81,7 +90,8 @@ public function getClassType(): PhpType { /** * @return PhpProperty[] プロパティ一覧 */ - public function getProperties(): array { + public function getProperties(): array + { $properties = $this->getPropertiesFromSyntax(); $props = []; foreach ($properties as $p) { @@ -93,33 +103,36 @@ public function getProperties(): array { /** * @return Property[] プロパティ一覧 */ - protected function getPropertiesFromSyntax(): array { + protected function getPropertiesFromSyntax(): array + { return $this->syntax->getProperties(); } /** * @return PhpType[] use一覧 */ - public function getUses(): array { + public function getUses(): array + { return $this->getUsesRec($this->full); } - private function getUsesRec($stmts, $uses = []) { + private function getUsesRec($stmts, $uses = []) + { foreach ($stmts as $stmt) { - if ($stmt instanceOf GroupUse) { + if ($stmt instanceof GroupUse) { $prefix = $stmt->prefix->parts; foreach ($stmt->uses as $u) { $parts = $u->name->parts; $name = array_pop($parts); - $uses[] = new PhpType(array_merge($prefix, $parts), '', $name, $u->alias); + $uses[] = new PhpType(array_merge($prefix, $parts), '', $name, $u->alias); } - } else if ($stmt instanceOf Use_) { + } else if ($stmt instanceof Use_) { foreach ($stmt->uses as $u) { $parts = $u->name->parts; $name = array_pop($parts); - $uses[] = new PhpType($parts, '', $name, $u->alias); + $uses[] = new PhpType($parts, '', $name, $u->alias); } - } else if ($stmt instanceOf Namespace_) { + } else if ($stmt instanceof Namespace_) { $uses = array_merge($uses, $this->getUsesRec($stmt->stmts, $uses)); } } @@ -133,7 +146,8 @@ private function getUsesRec($stmts, $uses = []) { * * 自身のクラス名が目的のクラスかどうか ... (不要かもしれない。暗黙の参照と統合可能 * * 暗黙の参照として、自身のnamespaceを返却する */ - protected function findNamespaceByTypeParts(array $type_parts): array { + protected function findNamespaceByTypeParts(array $type_parts): array + { $type = str_replace('[]', '', array_pop($type_parts)); $primitives = ['string', 'bool', 'boolean', 'int', 'integer', 'float', 'double', 'array', 'object', 'resource']; if (in_array($type, $primitives)) { @@ -155,35 +169,39 @@ protected function findNamespaceByTypeParts(array $type_parts): array { } /** @return PhpMethod[] メソッド一覧 */ - public function getMethods(): array { + public function getMethods(): array + { $methods = []; foreach ($this->syntax->stmts as $stmt) { - if ($stmt instanceOf ClassMethod) { + if ($stmt instanceof ClassMethod) { $methods[] = $this->getMethodInfo($stmt); } } return $methods; } - protected function getMethodInfo(ClassMethod $method): PhpMethod { + protected function getMethodInfo(ClassMethod $method): PhpMethod + { return new PhpMethod($method, $this); } /** * @return PhpType[] 継承元と実装元型一覧 */ - public function getExtends(): array { + public function getExtends(): array + { $extends = []; - if ( ! empty($this->syntax->extends)) { + if (!empty($this->syntax->extends)) { $Name = $this->syntax->extends; if (is_array($this->syntax->extends)) { $Name = $this->syntax->extends[0]; - } - if ($Name instanceOf FullyQualified) { + } + if ($Name instanceof FullyQualified) { $extends[] = new PhpType( array_slice($Name->parts, 0, count($Name->parts) - 1), '', - end($Name->parts)); + end($Name->parts) + ); } else { $parts = $Name->parts; $namespace = []; @@ -194,7 +212,7 @@ public function getExtends(): array { $extends[] = new PhpType(array_merge($namespace, $parts), 'Stmt_Class', $typeName); } } - if ( ! empty($this->syntax->implements)) { + if (!empty($this->syntax->implements)) { foreach ($this->syntax->implements as $i) { $parts = $i->parts; $namespace = []; diff --git a/src/Php/PhpMethod.php b/src/Php/PhpMethod.php index 9ed9f3f..a85ca02 100644 --- a/src/Php/PhpMethod.php +++ b/src/Php/PhpMethod.php @@ -1,29 +1,34 @@ -getDocComment(); - if ( ! empty($doc)) { + if (!empty($doc)) { $docString = $doc->getText(); } - $params = array_map(function(Param $x) use ($class, $docString){ + $params = array_map(function (Param $x) use ($class, $docString) { $type = PhpTypeExpression::buildByMethodParam($x, $class->getNamespace(), $docString, $x->var->name, $class->getUses()); return new PhpMethodParameter($x->var->name, $type); }, $method->getParams()); @@ -33,26 +38,31 @@ public function __construct(ClassMethod $method, PhpClass $class) { $this->accessModifier = new PhpAccessModifier($method); } - private function getTypeFromMethod(ClassMethod $method, PhpClass $class): PhpTypeExpression { + private function getTypeFromMethod(ClassMethod $method, PhpClass $class): PhpTypeExpression + { return PhpTypeExpression::buildByMethodReturn($method, $class->getNamespace(), $class->getUses()); } - public function getName(): string { + public function getName(): string + { return $this->name; } - public function getType(): PhpTypeExpression { + public function getType(): PhpTypeExpression + { return $this->type; } /** * @return PhpMethodParameter[] */ - public function getParams(): array { + public function getParams(): array + { return $this->params; } - public function getAccessModifier(): PhpAccessModifier { + public function getAccessModifier(): PhpAccessModifier + { return $this->accessModifier; } } diff --git a/src/Php/PhpMethodParameter.php b/src/Php/PhpMethodParameter.php index fa18b2f..ec8fc53 100644 --- a/src/Php/PhpMethodParameter.php +++ b/src/Php/PhpMethodParameter.php @@ -1,20 +1,27 @@ -name = $name; $this->type = $type; } - public function getName(): string { + public function getName(): string + { return $this->name; } - public function getType(): PhpTypeExpression { + public function getType(): PhpTypeExpression + { return $this->type; } } diff --git a/src/Php/PhpProperty.php b/src/Php/PhpProperty.php index 6f9b550..dec7840 100644 --- a/src/Php/PhpProperty.php +++ b/src/Php/PhpProperty.php @@ -1,30 +1,38 @@ -name = $p->props[0]->name->toString(); $this->type = PhpTypeExpression::buildByVar($p, $class->getNamespace(), $class->getUses()); $this->accessModifier = new PhpAccessModifier($p); } - public function getName(): string { + public function getName(): string + { return $this->name; } - public function getType(): PhpTypeExpression { + public function getType(): PhpTypeExpression + { return $this->type; } - public function getAccessModifier(): PhpAccessModifier { + public function getAccessModifier(): PhpAccessModifier + { return $this->accessModifier; } } diff --git a/src/Php/PhpReader.php b/src/Php/PhpReader.php index 773a472..c99758d 100644 --- a/src/Php/PhpReader.php +++ b/src/Php/PhpReader.php @@ -1,21 +1,26 @@ -directory = $directory; $this->filename = $filename; $this->class = $class; @@ -24,7 +29,8 @@ private function __construct(string $directory, string $filename, PhpClass $clas /** * @return PhpReader[] */ - public static function parseFile(string $directory, string $filename, Options $options): array { + public static function parseFile(string $directory, string $filename, Options $options): array + { $code = file_get_contents($filename); $targetVesion = ParserFactory::PREFER_PHP7; @@ -59,17 +65,18 @@ public static function parseFile(string $directory, string $filename, Options $o /** * @return PhpClass[] */ - private static function getClasses(string $relativePath, array $ast): array { + private static function getClasses(string $relativePath, array $ast): array + { if (count($ast) === 0) { return null; } $classes = []; foreach ($ast as $element) { - if ($element instanceOf ClassLike) { + if ($element instanceof ClassLike) { $classes[] = new PhpClass($relativePath, $element, $ast); - } else if ($element instanceOf Namespace_) { + } else if ($element instanceof Namespace_) { foreach ($element->stmts as $e) { - if ($e instanceOf ClassLike) { + if ($e instanceof ClassLike) { $classes[] = new PhpClass($relativePath, $e, $ast); } } @@ -78,7 +85,8 @@ private static function getClasses(string $relativePath, array $ast): array { return $classes; } - public function getInfo(): PhpClass { + public function getInfo(): PhpClass + { return $this->class; } } diff --git a/src/Php/PhpType.php b/src/Php/PhpType.php index 9d4bb92..06842ec 100644 --- a/src/Php/PhpType.php +++ b/src/Php/PhpType.php @@ -1,14 +1,19 @@ -namespace = $namespace; $this->meta = $meta; $this->name = $name; @@ -16,15 +21,18 @@ public function __construct(array $namespace, string $meta, string $name, $alias $this->nullable = $nullable; } - public function getName(): string { + public function getName(): string + { return $this->name; } - public function getMeta(): string { + public function getMeta(): string + { return $this->meta; } - public function getMetaName(): string { + public function getMetaName(): string + { switch ($this->meta) { case 'Stmt_Interface': return 'interface'; @@ -40,19 +48,23 @@ public function getMetaName(): string { /** * @return string[] namespace */ - public function getNamespace(): array { + public function getNamespace(): array + { return $this->namespace; } - public function getAlias(): string { + public function getAlias(): string + { return $this->alias; } - public function getNullable(): bool { + public function getNullable(): bool + { return $this->nullable; } - - public function equals(PhpType $other): bool { + + public function equals(PhpType $other): bool + { if (str_replace('[]', '', $this->name) !== str_replace('[]', '', $other->name)) { return false; } diff --git a/src/Php/PhpTypeExpression.php b/src/Php/PhpTypeExpression.php index 25adff6..6dd6351 100644 --- a/src/Php/PhpTypeExpression.php +++ b/src/Php/PhpTypeExpression.php @@ -1,4 +1,6 @@ -uses = $uses; $type = $stmt->{$targetType === self::RETURN_TYPE ? 'returnType' : 'type'}; - if ( ! empty($docString)) { + if (!empty($docString)) { foreach (explode('|', $docString) as $typeString) { $this->types[] = $this->parseType($type, $currentNamespace, $typeString); } @@ -124,7 +128,8 @@ public static function buildByMethodReturn( /** * 単体テスト用factory */ - public static function buildByPhpType(PhpType $type): self { + public static function buildByPhpType(PhpType $type): self + { $instance = new self(new Name('dummy'), self::FOR_TEST, [], '', []); $instance->types[] = $type; return $instance; @@ -135,7 +140,8 @@ public static function buildByPhpType(PhpType $type): self { * @param string[] $currentNamespace 名前空間配列 * @param ?string $typeString コメントの型表記 */ - private function parseType(Property|Identifier|NullableType|Name|UnionType|null $type, array $currentNamespace, ?string $typeString = '') { + private function parseType(Property|Identifier|NullableType|Name|UnionType|null $type, array $currentNamespace, ?string $typeString = '') + { $parts = []; if (!empty($typeString)) { $primitiveTypes = [ @@ -147,7 +153,7 @@ private function parseType(Property|Identifier|NullableType|Name|UnionType|null 'array', 'object', 'callable', - 'resource', + 'resource', ]; if (in_array($typeString, $primitiveTypes)) { $parts = [$typeString]; // primitive typeは、namespaceを付与しない。 @@ -157,7 +163,7 @@ private function parseType(Property|Identifier|NullableType|Name|UnionType|null $parts = explode('\\', $docString); } else { // usesを検索して適切なnamespaceを探す必要がある。 - $targets = array_values(array_filter($this->uses, function(PhpType $t) use($typeString) { + $targets = array_values(array_filter($this->uses, function (PhpType $t) use ($typeString) { $xParts = explode('\\', $typeString); $name = end($xParts); // docString で配列が指定されていた場合は、[] を除外して比較する。 @@ -166,7 +172,8 @@ private function parseType(Property|Identifier|NullableType|Name|UnionType|null if (count($targets) > 0) { $parts = array_merge( $targets[0]->getNamespace(), - [sprintf('%s%s', $targets[0]->getName(), preg_match('/\[\]$/', $typeString) ? '[]' : '')]); + [sprintf('%s%s', $targets[0]->getName(), preg_match('/\[\]$/', $typeString) ? '[]' : '')] + ); } else { $parts = array_merge($currentNamespace, explode('\\', $typeString)); } @@ -175,18 +182,18 @@ private function parseType(Property|Identifier|NullableType|Name|UnionType|null } $nullable = false; if (count($parts) === 0) { // docCommentから取得できない時には、$typeを解析する。 - if ($type instanceOf NullableType) { + if ($type instanceof NullableType) { $type = $type->type; $nullable = true; } - if ($type instanceOf Identifier) { + if ($type instanceof Identifier) { $parts[] = $type->name; - } else if ($type instanceOf FullyQualified) { + } else if ($type instanceof FullyQualified) { $parts = $type->parts; - } else if ($type instanceOf Name) { + } else if ($type instanceof Name) { $typeParts = $type->parts; // usesを検索して適切なnamespaceを探す必要がある。 - $targets = array_values(array_filter($this->uses, function(PhpType $t) use($typeParts) { + $targets = array_values(array_filter($this->uses, function (PhpType $t) use ($typeParts) { $name = end($typeParts); return $name === $t->getName(); })); @@ -204,15 +211,17 @@ private function parseType(Property|Identifier|NullableType|Name|UnionType|null /** * @return PhpType[] types */ - public function getTypes(): array { + public function getTypes(): array + { return $this->types; } - public function getName(): string { + public function getName(): string + { $types = []; foreach ($this->types as $type) { $types[] = sprintf('%s%s', $type->getNullable() ? '?' : '', $type->getName()); } return implode('|', $types); } -} \ No newline at end of file +}