From 24be61d2fdf951184035eee30308972accf5e87a Mon Sep 17 00:00:00 2001 From: Ruslan Guskov Date: Sun, 24 Aug 2025 16:27:30 +0300 Subject: [PATCH 1/8] feat: Php parser and modify user model file after installed clerk auth type --- src/Commands/InitCommand.php | 14 ++- src/Support/Parser/PhpParser.php | 118 ++++++++++++++++++ .../ArrayVisitors/BaseArrayVisitor.php | 32 +++++ .../BaseMethodReturnArrayVisitor.php | 25 ++++ ...emoveValueFromMethodReturnArrayVisitor.php | 28 +++++ ...lueToArrayPropertyPropertyArrayVisitor.php | 34 +++++ .../BasePropertyArrayVisitor.php | 15 +++ ...eFromArrayPropertyPropertyArrayVisitor.php | 28 +++++ tests/InitCommandTest.php | 28 +++++ .../InitCommandTest/modified_user_model.php | 35 ++++++ tests/fixtures/InitCommandTest/user_model.php | 35 ++++++ 11 files changed, 391 insertions(+), 1 deletion(-) create mode 100644 src/Support/Parser/PhpParser.php create mode 100644 src/Support/Parser/Visitors/ArrayVisitors/BaseArrayVisitor.php create mode 100644 src/Support/Parser/Visitors/ArrayVisitors/MethodReturnArrayVisitors/BaseMethodReturnArrayVisitor.php create mode 100644 src/Support/Parser/Visitors/ArrayVisitors/MethodReturnArrayVisitors/RemoveValueFromMethodReturnArrayVisitor.php create mode 100644 src/Support/Parser/Visitors/ArrayVisitors/PropertyArrayVisitors/AddValueToArrayPropertyPropertyArrayVisitor.php create mode 100644 src/Support/Parser/Visitors/ArrayVisitors/PropertyArrayVisitors/BasePropertyArrayVisitor.php create mode 100644 src/Support/Parser/Visitors/ArrayVisitors/PropertyArrayVisitors/RemoveValueFromArrayPropertyPropertyArrayVisitor.php create mode 100644 tests/fixtures/InitCommandTest/modified_user_model.php create mode 100644 tests/fixtures/InitCommandTest/user_model.php diff --git a/src/Commands/InitCommand.php b/src/Commands/InitCommand.php index 40bac89..941eb9e 100644 --- a/src/Commands/InitCommand.php +++ b/src/Commands/InitCommand.php @@ -14,6 +14,7 @@ use RonasIT\ProjectInitializator\Enums\RoleEnum; use RonasIT\ProjectInitializator\Enums\AppTypeEnum; use Winter\LaravelConfigWriter\ArrayFile; +use RonasIT\ProjectInitializator\Support\Parser\PhpParser; class InitCommand extends Command implements Isolatable { @@ -226,7 +227,7 @@ public function handle(): void protected function setAutoDocContactEmail(string $email): void { $config = ArrayFile::open(base_path('config/auto-doc.php')); - + $config->set('info.contact.email', $email); $config->write(); @@ -560,6 +561,8 @@ protected function enableClerk(): void viewName: 'ClerkUserRepository', path: 'app/Support/Clerk', ); + + $this->modifyUserModel(); } // TODO: try to use package after fixing https://github.com/wintercms/laravel-config-writer/issues/6 @@ -588,4 +591,13 @@ protected function addClerkToAuthConfigCallback(): callable return $matches[1] . $existing . $clerkGuard . $newLine . " ],"; }; } + + protected function modifyUserModel(): void + { + (new PhpParser('app/Models/User.php')) + ->addValueToArrayProperty(['fillable'], 'clerk_id') + ->removeValueFromArrayProperty(['fillable', 'hidden'], 'password') + ->removeValueFromMethodReturnArray(['casts'], 'password') + ->save(); + } } diff --git a/src/Support/Parser/PhpParser.php b/src/Support/Parser/PhpParser.php new file mode 100644 index 0000000..cd3eb98 --- /dev/null +++ b/src/Support/Parser/PhpParser.php @@ -0,0 +1,118 @@ +createForNewestSupportedVersion(); + $this->ast = $parser->parse(file_get_contents($filePath)); + + $this->traverser = new NodeTraverser(); + $this->printer = new Standard(); + + $this->addEmptySpacesVisitor(); + } + + public function removeValueFromArrayProperty(array $propertyNames, string $value): self + { + $this->traverser->addVisitor(new RemoveValueFromArrayPropertyPropertyArrayVisitor($propertyNames, $value)); + + return $this; + } + + public function removeValueFromMethodReturnArray(array $methodNames, string $value): self + { + $this->traverser->addVisitor(new RemoveValueFromMethodReturnArrayVisitor($methodNames, $value)); + + return $this; + } + + public function addValueToArrayProperty(array $propertyNames, string $value): self + { + $this->traverser->addVisitor(new AddValueToArrayPropertyPropertyArrayVisitor($propertyNames, $value)); + + return $this; + } + + public function save(): void + { + $modifiedAst = $this->traverser->traverse($this->ast); + file_put_contents($this->filePath, $this->printer->prettyPrintFile($modifiedAst)); + } + + protected function addEmptySpacesVisitor(): void + { + $this->traverser->addVisitor( + new class extends NodeVisitorAbstract { + public function enterNode(Node $node): void + { + if (!$node instanceof Namespace_) { + return; + } + + $this->addLineAfterLastUse($node); + $this->addEndLine($node); + } + + private function addLineAfterLastUse(Namespace_ $namespace): void + { + $lastUseIndex = $this->findLastUseIndex($namespace); + + if ($lastUseIndex === null) { + return; + } + + $afterLastUse = $namespace->stmts[$lastUseIndex + 1] ?? null; + + if (!$afterLastUse instanceof Nop) { + array_splice($namespace->stmts, $lastUseIndex + 1, 0, [new Nop()]); + } + } + + private function addEndLine(Namespace_ $namespace): void + { + $lastStmtIndex = count($namespace->stmts) - 1; + + if ($lastStmtIndex < 0) { + return; + } + + if (!$namespace->stmts[$lastStmtIndex] instanceof Nop) { + $namespace->stmts[] = new Nop(); + } + } + + private function findLastUseIndex(Namespace_ $namespace): ?int + { + $lastUseIndex = null; + + foreach ($namespace->stmts as $i => $stmt) { + if ($stmt instanceof Use_) { + $lastUseIndex = $i; + } + } + + return $lastUseIndex; + } + } + ); + } +} \ No newline at end of file diff --git a/src/Support/Parser/Visitors/ArrayVisitors/BaseArrayVisitor.php b/src/Support/Parser/Visitors/ArrayVisitors/BaseArrayVisitor.php new file mode 100644 index 0000000..19ebaa5 --- /dev/null +++ b/src/Support/Parser/Visitors/ArrayVisitors/BaseArrayVisitor.php @@ -0,0 +1,32 @@ +stmtNames, true) + && $default instanceof Array_; + } + + protected function getTargetItems(array $items): array + { + $filteredArray = array_filter($items, fn($item) => !$this->isTargetItem($item)); + + return array_values($filteredArray); + } + + abstract protected function isTargetItem(?ArrayItem $item): bool; +} \ No newline at end of file diff --git a/src/Support/Parser/Visitors/ArrayVisitors/MethodReturnArrayVisitors/BaseMethodReturnArrayVisitor.php b/src/Support/Parser/Visitors/ArrayVisitors/MethodReturnArrayVisitors/BaseMethodReturnArrayVisitor.php new file mode 100644 index 0000000..dad2b7e --- /dev/null +++ b/src/Support/Parser/Visitors/ArrayVisitors/MethodReturnArrayVisitors/BaseMethodReturnArrayVisitor.php @@ -0,0 +1,25 @@ +key instanceof String_ + && $item->key->value === $this->value; + } + + protected function isTargetReturnArray(Node $stmt): bool + { + return $stmt instanceof Return_ + && $stmt->expr instanceof Array_; + } +} \ No newline at end of file diff --git a/src/Support/Parser/Visitors/ArrayVisitors/MethodReturnArrayVisitors/RemoveValueFromMethodReturnArrayVisitor.php b/src/Support/Parser/Visitors/ArrayVisitors/MethodReturnArrayVisitors/RemoveValueFromMethodReturnArrayVisitor.php new file mode 100644 index 0000000..68587f5 --- /dev/null +++ b/src/Support/Parser/Visitors/ArrayVisitors/MethodReturnArrayVisitors/RemoveValueFromMethodReturnArrayVisitor.php @@ -0,0 +1,28 @@ +name->toString(); + + if (!in_array($methodName, $this->stmtNames, true)) { + return; + } + + foreach ($node->stmts as $stmt) { + if ($this->isTargetReturnArray($stmt)) { + $stmt->expr->items = $this->getTargetItems($stmt->expr->items); + } + } + } +} \ No newline at end of file diff --git a/src/Support/Parser/Visitors/ArrayVisitors/PropertyArrayVisitors/AddValueToArrayPropertyPropertyArrayVisitor.php b/src/Support/Parser/Visitors/ArrayVisitors/PropertyArrayVisitors/AddValueToArrayPropertyPropertyArrayVisitor.php new file mode 100644 index 0000000..b7070f9 --- /dev/null +++ b/src/Support/Parser/Visitors/ArrayVisitors/PropertyArrayVisitors/AddValueToArrayPropertyPropertyArrayVisitor.php @@ -0,0 +1,34 @@ +props[0]; + + $propertyName = $property->name->toString(); + + if (!$this->isTargetProperty($propertyName, $property->default)) { + return; + } + + $array = $property->default; + + if (!empty($this->getTargetItems($array->items))) { + return; + } + + $array->items[] = new ArrayItem(new String_($this->value)); + } +} \ No newline at end of file diff --git a/src/Support/Parser/Visitors/ArrayVisitors/PropertyArrayVisitors/BasePropertyArrayVisitor.php b/src/Support/Parser/Visitors/ArrayVisitors/PropertyArrayVisitors/BasePropertyArrayVisitor.php new file mode 100644 index 0000000..f3c42ec --- /dev/null +++ b/src/Support/Parser/Visitors/ArrayVisitors/PropertyArrayVisitors/BasePropertyArrayVisitor.php @@ -0,0 +1,15 @@ +value->value === $this->value; + } +} \ No newline at end of file diff --git a/src/Support/Parser/Visitors/ArrayVisitors/PropertyArrayVisitors/RemoveValueFromArrayPropertyPropertyArrayVisitor.php b/src/Support/Parser/Visitors/ArrayVisitors/PropertyArrayVisitors/RemoveValueFromArrayPropertyPropertyArrayVisitor.php new file mode 100644 index 0000000..63884ce --- /dev/null +++ b/src/Support/Parser/Visitors/ArrayVisitors/PropertyArrayVisitors/RemoveValueFromArrayPropertyPropertyArrayVisitor.php @@ -0,0 +1,28 @@ +props[0]; + + $propertyName = $property->name->toString(); + + if (!$this->isTargetProperty($propertyName, $property->default)) { + return; + } + + $array = $property->default; + + $array->items = $this->getTargetItems($array->items); + } +} \ No newline at end of file diff --git a/tests/InitCommandTest.php b/tests/InitCommandTest.php index 3004ea2..7d2d234 100644 --- a/tests/InitCommandTest.php +++ b/tests/InitCommandTest.php @@ -232,6 +232,20 @@ public function testRunWithAdminAndDefaultReadmeCreation() ], ); + $this->mockNativeFunction('RonasIT\ProjectInitializator\Support\Parser', + [ + $this->functionCall( + name: 'file_get_contents', + arguments: ['app/Models/User.php'], + result: $this->getFixture('user_model.php'), + ), + $this->functionCall( + name: 'file_put_contents', + arguments: ['app/Models/User.php', $this->getFixture('modified_user_model.php')], + ), + ] + ); + $this->mockFilePutContent( 'env.example.yml', 'env.development.yml', @@ -827,6 +841,20 @@ public function testRunWithClerkMobileApp(): void ], ); + $this->mockNativeFunction('RonasIT\ProjectInitializator\Support\Parser', + [ + $this->functionCall( + name: 'file_get_contents', + arguments: ['app/Models/User.php'], + result: $this->getFixture('user_model.php'), + ), + $this->functionCall( + name: 'file_put_contents', + arguments: ['app/Models/User.php', $this->getFixture('modified_user_model.php')], + ), + ] + ); + $this->mockFilePutContent( 'env.example.yml', 'env.development.yml', diff --git a/tests/fixtures/InitCommandTest/modified_user_model.php b/tests/fixtures/InitCommandTest/modified_user_model.php new file mode 100644 index 0000000..ee5e8b4 --- /dev/null +++ b/tests/fixtures/InitCommandTest/modified_user_model.php @@ -0,0 +1,35 @@ + */ + use HasFactory, Notifiable; + /** + * The attributes that are mass assignable. + * + * @var list + */ + protected $fillable = ['name', 'email', 'clerk_id']; + /** + * The attributes that should be hidden for serialization. + * + * @var list + */ + protected $hidden = ['remember_token']; + /** + * Get the attributes that should be cast. + * + * @return array + */ + protected function casts(): array + { + return ['email_verified_at' => 'datetime']; + } +} diff --git a/tests/fixtures/InitCommandTest/user_model.php b/tests/fixtures/InitCommandTest/user_model.php new file mode 100644 index 0000000..76b1e64 --- /dev/null +++ b/tests/fixtures/InitCommandTest/user_model.php @@ -0,0 +1,35 @@ + */ + use HasFactory, Notifiable; + /** + * The attributes that are mass assignable. + * + * @var list + */ + protected $fillable = ['name', 'email', 'password', 'clerk_id']; + /** + * The attributes that should be hidden for serialization. + * + * @var list + */ + protected $hidden = ['password', 'remember_token']; + /** + * Get the attributes that should be cast. + * + * @return array + */ + protected function casts(): array + { + return ['email_verified_at' => 'datetime', 'password' => 'hashed']; + } +} \ No newline at end of file From 41d089bf4c37cc658f10918b74ed1146dc42a94a Mon Sep 17 00:00:00 2001 From: Ruslan Guskov Date: Sun, 24 Aug 2025 16:30:50 +0300 Subject: [PATCH 2/8] style: fix --- src/Support/Parser/PhpParser.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Support/Parser/PhpParser.php b/src/Support/Parser/PhpParser.php index cd3eb98..da2c11c 100644 --- a/src/Support/Parser/PhpParser.php +++ b/src/Support/Parser/PhpParser.php @@ -23,9 +23,11 @@ class PhpParser public function __construct(protected string $filePath) { $parser = (new ParserFactory())->createForNewestSupportedVersion(); + $this->ast = $parser->parse(file_get_contents($filePath)); $this->traverser = new NodeTraverser(); + $this->printer = new Standard(); $this->addEmptySpacesVisitor(); @@ -55,6 +57,7 @@ public function addValueToArrayProperty(array $propertyNames, string $value): se public function save(): void { $modifiedAst = $this->traverser->traverse($this->ast); + file_put_contents($this->filePath, $this->printer->prettyPrintFile($modifiedAst)); } From 7b4394fcbab6dc7426f2d473eb9670d802c37312 Mon Sep 17 00:00:00 2001 From: Ruslan Guskov Date: Sun, 24 Aug 2025 16:33:45 +0300 Subject: [PATCH 3/8] style:fix --- src/Support/Parser/PhpParser.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Support/Parser/PhpParser.php b/src/Support/Parser/PhpParser.php index da2c11c..ed512c0 100644 --- a/src/Support/Parser/PhpParser.php +++ b/src/Support/Parser/PhpParser.php @@ -17,7 +17,9 @@ class PhpParser { private array $ast; + private NodeTraverser $traverser; + private Standard $printer; public function __construct(protected string $filePath) @@ -57,7 +59,7 @@ public function addValueToArrayProperty(array $propertyNames, string $value): se public function save(): void { $modifiedAst = $this->traverser->traverse($this->ast); - + file_put_contents($this->filePath, $this->printer->prettyPrintFile($modifiedAst)); } From 88cf03b71287181fe579a13b1eac73ca392f71eb Mon Sep 17 00:00:00 2001 From: Ruslan Guskov Date: Tue, 26 Aug 2025 13:26:08 +0300 Subject: [PATCH 4/8] refactor: make bind for parser --- src/Commands/InitCommand.php | 4 +++- src/ProjectInitializatorServiceProvider.php | 6 +++++ .../Parser/Factories/PhpParserFactory.php | 21 +++++++++++++++++ src/Support/Parser/PhpParser.php | 23 ++++++++----------- 4 files changed, 39 insertions(+), 15 deletions(-) create mode 100644 src/Support/Parser/Factories/PhpParserFactory.php diff --git a/src/Commands/InitCommand.php b/src/Commands/InitCommand.php index 941eb9e..2d972f2 100644 --- a/src/Commands/InitCommand.php +++ b/src/Commands/InitCommand.php @@ -594,7 +594,9 @@ protected function addClerkToAuthConfigCallback(): callable protected function modifyUserModel(): void { - (new PhpParser('app/Models/User.php')) + $parser = app(PhpParser::class, ['filePath' => 'app/Models/User.php']); + + $parser ->addValueToArrayProperty(['fillable'], 'clerk_id') ->removeValueFromArrayProperty(['fillable', 'hidden'], 'password') ->removeValueFromMethodReturnArray(['casts'], 'password') diff --git a/src/ProjectInitializatorServiceProvider.php b/src/ProjectInitializatorServiceProvider.php index 4ea49e9..da99ad8 100644 --- a/src/ProjectInitializatorServiceProvider.php +++ b/src/ProjectInitializatorServiceProvider.php @@ -4,6 +4,8 @@ use Illuminate\Support\ServiceProvider; use RonasIT\ProjectInitializator\Commands\InitCommand; +use RonasIT\ProjectInitializator\Support\Parser\PhpParser; +use RonasIT\ProjectInitializator\Support\Parser\Factories\PhpParserFactory; class ProjectInitializatorServiceProvider extends ServiceProvider { @@ -14,5 +16,9 @@ public function boot(): void ]); $this->loadViewsFrom(__DIR__ . '/../resources/views', 'initializator'); + + $this->app->bind(PhpParser::class, function ($app, $params) { + return PhpParserFactory::create($params['filePath']); + }); } } diff --git a/src/Support/Parser/Factories/PhpParserFactory.php b/src/Support/Parser/Factories/PhpParserFactory.php new file mode 100644 index 0000000..e8e1be1 --- /dev/null +++ b/src/Support/Parser/Factories/PhpParserFactory.php @@ -0,0 +1,21 @@ +createForNewestSupportedVersion(), + new NodeTraverser(), + new Standard(), + ); + } +} \ No newline at end of file diff --git a/src/Support/Parser/PhpParser.php b/src/Support/Parser/PhpParser.php index ed512c0..f1cbd96 100644 --- a/src/Support/Parser/PhpParser.php +++ b/src/Support/Parser/PhpParser.php @@ -2,9 +2,6 @@ namespace RonasIT\ProjectInitializator\Support\Parser; -use PhpParser\ParserFactory; -use PhpParser\NodeTraverser; -use PhpParser\PrettyPrinter\Standard; use RonasIT\ProjectInitializator\Support\Parser\Visitors\ArrayVisitors\PropertyArrayVisitors\RemoveValueFromArrayPropertyPropertyArrayVisitor; use RonasIT\ProjectInitializator\Support\Parser\Visitors\ArrayVisitors\MethodReturnArrayVisitors\RemoveValueFromMethodReturnArrayVisitor; use RonasIT\ProjectInitializator\Support\Parser\Visitors\ArrayVisitors\PropertyArrayVisitors\AddValueToArrayPropertyPropertyArrayVisitor; @@ -13,25 +10,23 @@ use PhpParser\NodeVisitorAbstract; use PhpParser\Node\Stmt\Nop; use PhpParser\Node\Stmt\Use_; +use PhpParser\Parser; +use PhpParser\PrettyPrinterAbstract; +use PhpParser\NodeTraverserInterface; class PhpParser { private array $ast; - private NodeTraverser $traverser; - - private Standard $printer; - - public function __construct(protected string $filePath) + public function __construct( + protected string $filePath, + protected Parser $parser, + protected NodeTraverserInterface $traverser, + protected PrettyPrinterAbstract $printer, + ) { - $parser = (new ParserFactory())->createForNewestSupportedVersion(); - $this->ast = $parser->parse(file_get_contents($filePath)); - $this->traverser = new NodeTraverser(); - - $this->printer = new Standard(); - $this->addEmptySpacesVisitor(); } From ffd2950c608a3200aaf68a035057d6162b0f413e Mon Sep 17 00:00:00 2001 From: Ruslan Guskov Date: Tue, 26 Aug 2025 13:31:51 +0300 Subject: [PATCH 5/8] tests: fix test fixture --- tests/fixtures/InitCommandTest/user_model.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/fixtures/InitCommandTest/user_model.php b/tests/fixtures/InitCommandTest/user_model.php index 76b1e64..6e4b7e2 100644 --- a/tests/fixtures/InitCommandTest/user_model.php +++ b/tests/fixtures/InitCommandTest/user_model.php @@ -16,7 +16,7 @@ class User extends Authenticatable * * @var list */ - protected $fillable = ['name', 'email', 'password', 'clerk_id']; + protected $fillable = ['name', 'email', 'password']; /** * The attributes that should be hidden for serialization. * From 8da124c3d37ff0b801b530bfa19011103f2f2f04 Mon Sep 17 00:00:00 2001 From: Ruslan Guskov Date: Wed, 27 Aug 2025 13:53:06 +0300 Subject: [PATCH 6/8] fix: add value visitor --- .../AddValueToArrayPropertyPropertyArrayVisitor.php | 2 +- tests/fixtures/InitCommandTest/user_model.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Support/Parser/Visitors/ArrayVisitors/PropertyArrayVisitors/AddValueToArrayPropertyPropertyArrayVisitor.php b/src/Support/Parser/Visitors/ArrayVisitors/PropertyArrayVisitors/AddValueToArrayPropertyPropertyArrayVisitor.php index b7070f9..24b6bba 100644 --- a/src/Support/Parser/Visitors/ArrayVisitors/PropertyArrayVisitors/AddValueToArrayPropertyPropertyArrayVisitor.php +++ b/src/Support/Parser/Visitors/ArrayVisitors/PropertyArrayVisitors/AddValueToArrayPropertyPropertyArrayVisitor.php @@ -25,7 +25,7 @@ public function enterNode(Node $node): void $array = $property->default; - if (!empty($this->getTargetItems($array->items))) { + if ($this->getTargetItems($array->items) !== $array->items) { return; } diff --git a/tests/fixtures/InitCommandTest/user_model.php b/tests/fixtures/InitCommandTest/user_model.php index 6e4b7e2..76b1e64 100644 --- a/tests/fixtures/InitCommandTest/user_model.php +++ b/tests/fixtures/InitCommandTest/user_model.php @@ -16,7 +16,7 @@ class User extends Authenticatable * * @var list */ - protected $fillable = ['name', 'email', 'password']; + protected $fillable = ['name', 'email', 'password', 'clerk_id']; /** * The attributes that should be hidden for serialization. * From eca70eac0c0ab96d3ab491d2df56bdb631619ec2 Mon Sep 17 00:00:00 2001 From: Ruslan Guskov Date: Wed, 27 Aug 2025 13:59:26 +0300 Subject: [PATCH 7/8] fix: fixture --- tests/fixtures/InitCommandTest/user_model.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/fixtures/InitCommandTest/user_model.php b/tests/fixtures/InitCommandTest/user_model.php index 76b1e64..6e4b7e2 100644 --- a/tests/fixtures/InitCommandTest/user_model.php +++ b/tests/fixtures/InitCommandTest/user_model.php @@ -16,7 +16,7 @@ class User extends Authenticatable * * @var list */ - protected $fillable = ['name', 'email', 'password', 'clerk_id']; + protected $fillable = ['name', 'email', 'password']; /** * The attributes that should be hidden for serialization. * From 361a5a4af8ad880d7a097eb068fb2825c5943649 Mon Sep 17 00:00:00 2001 From: Ruslan Guskov Date: Mon, 1 Sep 2025 13:32:59 +0300 Subject: [PATCH 8/8] fix: target item check --- .../MethodReturnArrayVisitors/BaseMethodReturnArrayVisitor.php | 3 ++- .../PropertyArrayVisitors/BasePropertyArrayVisitor.php | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Support/Parser/Visitors/ArrayVisitors/MethodReturnArrayVisitors/BaseMethodReturnArrayVisitor.php b/src/Support/Parser/Visitors/ArrayVisitors/MethodReturnArrayVisitors/BaseMethodReturnArrayVisitor.php index dad2b7e..1b3fc41 100644 --- a/src/Support/Parser/Visitors/ArrayVisitors/MethodReturnArrayVisitors/BaseMethodReturnArrayVisitor.php +++ b/src/Support/Parser/Visitors/ArrayVisitors/MethodReturnArrayVisitors/BaseMethodReturnArrayVisitor.php @@ -13,7 +13,8 @@ class BaseMethodReturnArrayVisitor extends BaseArrayVisitor { protected function isTargetItem(?ArrayItem $item): bool { - return $item->key instanceof String_ + return !empty($item) + && $item->key instanceof String_ && $item->key->value === $this->value; } diff --git a/src/Support/Parser/Visitors/ArrayVisitors/PropertyArrayVisitors/BasePropertyArrayVisitor.php b/src/Support/Parser/Visitors/ArrayVisitors/PropertyArrayVisitors/BasePropertyArrayVisitor.php index f3c42ec..dbe9839 100644 --- a/src/Support/Parser/Visitors/ArrayVisitors/PropertyArrayVisitors/BasePropertyArrayVisitor.php +++ b/src/Support/Parser/Visitors/ArrayVisitors/PropertyArrayVisitors/BasePropertyArrayVisitor.php @@ -9,7 +9,7 @@ class BasePropertyArrayVisitor extends BaseArrayVisitor { protected function isTargetItem(?ArrayItem $item): bool { - return $item !== null + return !empty($item) && $item->value->value === $this->value; } } \ No newline at end of file