From 6338abb2d7cbefc58c1ac4adc5f6824b5d0c7324 Mon Sep 17 00:00:00 2001 From: sforward <3530221+sforward@users.noreply.github.com> Date: Mon, 1 Apr 2024 08:43:59 +0200 Subject: [PATCH] Fix return value of scopes from parent class (#1366) --- CHANGELOG.md | 1 + src/Console/ModelsCommand.php | 6 +- .../ModelsCommand/QueryScopes/Models/Post.php | 13 ++ .../QueryScopes/Models/PostParent.php | 15 ++ .../ModelsCommand/QueryScopes/Test.php | 24 +++ .../__snapshots__/Test__test__1.php | 189 ++++++++++++++++++ 6 files changed, 245 insertions(+), 3 deletions(-) create mode 100644 tests/Console/ModelsCommand/QueryScopes/Models/Post.php create mode 100644 tests/Console/ModelsCommand/QueryScopes/Models/PostParent.php create mode 100644 tests/Console/ModelsCommand/QueryScopes/Test.php create mode 100644 tests/Console/ModelsCommand/QueryScopes/__snapshots__/Test__test__1.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 7dc6410..231ae2a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ All notable changes to this project will be documented in this file. -------------- ### Fixed +- Fix return value of query scopes from parent class [#1366 / sforward](https://github.com/barryvdh/laravel-ide-helper/pull/1366) ### Changed diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index 5285fb0..fd6c72b 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -616,7 +616,7 @@ class ModelsCommand extends Command $this->setProperty($name, null, null, true, $comment); } } elseif (Str::startsWith($method, 'scope') && $method !== 'scopeQuery' && $method !== 'scope' && $method !== 'scopes') { - //Magic setAttribute + //Magic scopeAttribute $name = Str::camel(substr($method, 5)); if (!empty($name)) { $comment = $this->getCommentFromDocBlock($reflection); @@ -628,8 +628,8 @@ class ModelsCommand extends Command get_class($model->newModelQuery()) ); $modelName = $this->getClassNameInDestinationFile( - $reflection->getDeclaringClass(), - $reflection->getDeclaringClass()->getName() + new \ReflectionClass($model), + get_class($model) ); $this->setMethod($name, $builder . '|' . $modelName, $args, $comment); } diff --git a/tests/Console/ModelsCommand/QueryScopes/Models/Post.php b/tests/Console/ModelsCommand/QueryScopes/Models/Post.php new file mode 100644 index 0000000..9d64941 --- /dev/null +++ b/tests/Console/ModelsCommand/QueryScopes/Models/Post.php @@ -0,0 +1,13 @@ +app->make(ModelsCommand::class); + + $tester = $this->runCommand($command, [ + '--write' => true, + ]); + + $this->assertSame(0, $tester->getStatusCode()); + $this->assertStringContainsString('Written new phpDocBlock to', $tester->getDisplay()); + $this->assertMatchesMockedSnapshot(); + } +} diff --git a/tests/Console/ModelsCommand/QueryScopes/__snapshots__/Test__test__1.php b/tests/Console/ModelsCommand/QueryScopes/__snapshots__/Test__test__1.php new file mode 100644 index 0000000..515dcf8 --- /dev/null +++ b/tests/Console/ModelsCommand/QueryScopes/__snapshots__/Test__test__1.php @@ -0,0 +1,189 @@ +