-
Notifications
You must be signed in to change notification settings - Fork 0
refactor: replace abstract visitor hierarchy with contract-based polymorphic dispatch #67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
f587240
5ff20d7
0520d5e
450fe96
a0ae529
c2bed30
d13ce08
7e18bad
18ad99c
6ee2921
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,8 +11,8 @@ | |
| use RonasIT\Larabuilder\Exceptions\InvalidPHPFileException; | ||
| use RonasIT\Larabuilder\NodeTraverser; | ||
| use RonasIT\Larabuilder\Printer; | ||
| use RonasIT\Larabuilder\Visitors\AddImports; | ||
| use RonasIT\Larabuilder\Visitors\AddTraits; | ||
| use RonasIT\Larabuilder\Visitors\AddImport; | ||
| use RonasIT\Larabuilder\Visitors\AddTrait; | ||
| use RonasIT\Larabuilder\Visitors\MethodVisitors\AddMethod; | ||
| use RonasIT\Larabuilder\Visitors\MethodVisitors\InsertCodeToMethod; | ||
| use RonasIT\Larabuilder\Visitors\MethodVisitors\RemoveMethod; | ||
|
|
@@ -67,14 +67,18 @@ public function removeArrayPropertyItem(string $propertyName, array $values): se | |
|
|
||
| public function addImports(array $imports): self | ||
| { | ||
| $this->traverser->addVisitor(new AddImports($imports)); | ||
| foreach ($imports as $import) { | ||
| $this->traverser->addVisitor(new AddImport($import)); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When callers pass multiple new imports, this now enqueues one Useful? React with 👍 / 👎. |
||
| } | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| public function addTraits(array $traits): self | ||
| { | ||
| $this->traverser->addVisitor(new AddTraits($traits)); | ||
| foreach ($traits as $trait) { | ||
| $this->traverser->addVisitor(new AddTrait($trait)); | ||
|
Comment on lines
+79
to
+80
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Useful? React with 👍 / 👎. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When Useful? React with 👍 / 👎. |
||
| } | ||
|
|
||
| $this->addImports($traits); | ||
|
|
||
|
|
||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| <?php | ||
|
|
||
| namespace RonasIT\Larabuilder\Visitors; | ||
|
|
||
| use PhpParser\Node; | ||
| use PhpParser\Node\Name; | ||
| use PhpParser\Node\Stmt\Namespace_; | ||
| use PhpParser\Node\Stmt\Use_; | ||
| use PhpParser\Node\UseItem; | ||
| use RonasIT\Larabuilder\Contracts\InsertNodeContract; | ||
|
|
||
| class AddImport extends AbstractNodeVisitor implements InsertNodeContract | ||
| { | ||
| protected array $allowedParentNodesTypes = self::ANY_TYPE; | ||
|
|
||
| public function __construct( | ||
| protected string $import, | ||
| ) { | ||
| } | ||
|
|
||
| public function afterTraverse(array $nodes): ?array | ||
| { | ||
| $targetNamespace = array_find($nodes, fn ($node) => $node instanceof Namespace_); | ||
|
|
||
| if (!is_null($targetNamespace)) { | ||
| /** @var Namespace_ $targetNamespace */ | ||
| $targetNodes = &$targetNamespace->stmts; | ||
| } else { | ||
| $targetNodes = &$nodes; | ||
| } | ||
|
|
||
| $this->insertNode($targetNodes); | ||
|
|
||
| return $nodes; | ||
| } | ||
|
|
||
| public function getInsertableNode(): Node | ||
| { | ||
| return new Use_([new UseItem(new Name($this->import))]); | ||
| } | ||
|
|
||
| protected function isDuplicate(array $stmts): bool | ||
| { | ||
| foreach ($stmts as $stmt) { | ||
| if (!($stmt instanceof Use_)) { | ||
| continue; | ||
| } | ||
|
|
||
| foreach ($stmt->uses as $useItem) { | ||
| if ($useItem->name->toString() === $this->import) { | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
| } |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| <?php | ||
|
|
||
| namespace RonasIT\Larabuilder\Visitors; | ||
|
|
||
| use PhpParser\Node; | ||
| use PhpParser\Node\Name; | ||
| use PhpParser\Node\Stmt\Class_; | ||
| use PhpParser\Node\Stmt\Enum_; | ||
| use PhpParser\Node\Stmt\Trait_; | ||
| use PhpParser\Node\Stmt\TraitUse; | ||
| use RonasIT\Larabuilder\Contracts\InsertNodeContract; | ||
|
|
||
| class AddTrait extends AbstractNodeVisitor implements InsertNodeContract | ||
| { | ||
| protected array $allowedParentNodesTypes = [ | ||
| Class_::class, | ||
| Trait_::class, | ||
| Enum_::class, | ||
| ]; | ||
|
|
||
| public function __construct( | ||
| protected string $trait, | ||
| ) { | ||
| $this->trait = class_basename($this->trait); | ||
| } | ||
|
|
||
| public function getInsertableNode(): Node | ||
| { | ||
| return new TraitUse([new Name($this->trait)]); | ||
| } | ||
|
|
||
| protected function isDuplicate(array $stmts): bool | ||
| { | ||
| foreach ($stmts as $stmt) { | ||
| if (!($stmt instanceof TraitUse)) { | ||
| continue; | ||
| } | ||
|
|
||
| foreach ($stmt->traits as $traitName) { | ||
| if ($traitName->getLast() === $this->trait) { | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
addImports()now forwards each raw array element directly intonew AddImport($import), butAddImport::__constructrequires astringand no longer benefits from the oldfilter()step. If callers pass optional values (for examplenullor empty strings from config/merge pipelines), this now throws aTypeError(fornull) or can emit an invaliduse ;statement (for empty string), where the previous implementation silently skipped those values.Useful? React with 👍 / 👎.