diff --git a/src/Plugin.php b/src/Plugin.php index 2b40d8d..29fa34c 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -1,6 +1,7 @@ registerStubs($registration); } + protected function getCommonStubs(): array + { + return glob(dirname(__DIR__) . '/stubs/*.stubphp'); + } + + protected function getStubsForVersion(string $version): array + { + [$majorVersion] = explode('.', $version); + + return glob(dirname(__DIR__) . '/stubs/'.$majorVersion.'/*.stubphp'); + } + private function registerStubs(RegistrationInterface $registration): void { - foreach (glob(dirname(__DIR__) . '/stubs/*.stubphp') as $stubFilePath) { + $stubs = array_merge( + $this->getCommonStubs(), + $this->getStubsForVersion(Application::VERSION), + ); + + foreach ($stubs as $stubFilePath) { $registration->addStubFile($stubFilePath); } diff --git a/stubs/6/helpers.stubphp b/stubs/6/helpers.stubphp new file mode 100644 index 0000000..8349dea --- /dev/null +++ b/stubs/6/helpers.stubphp @@ -0,0 +1,25 @@ + $modelClassName + * @param TNameOrCountOrNull $nameOrCount + * @param TCountOrNull $count + * + * @return ( + TCountOrNull is int + ? Illuminate\Database\Eloquent\FactoryBuilder + : ( + TNameOrCountOrNull is int + ? Illuminate\Database\Eloquent\FactoryBuilder + : Illuminate\Database\Eloquent\FactoryBuilder + ) + + ) + */ +function factory(string $modelClassName, $nameOrCount = null, $count = null) +{ +} diff --git a/stubs/7/helpers.stubphp b/stubs/7/helpers.stubphp new file mode 100644 index 0000000..8349dea --- /dev/null +++ b/stubs/7/helpers.stubphp @@ -0,0 +1,25 @@ + $modelClassName + * @param TNameOrCountOrNull $nameOrCount + * @param TCountOrNull $count + * + * @return ( + TCountOrNull is int + ? Illuminate\Database\Eloquent\FactoryBuilder + : ( + TNameOrCountOrNull is int + ? Illuminate\Database\Eloquent\FactoryBuilder + : Illuminate\Database\Eloquent\FactoryBuilder + ) + + ) + */ +function factory(string $modelClassName, $nameOrCount = null, $count = null) +{ +} diff --git a/stubs/helpers.stubphp b/stubs/helpers.stubphp index f40c377..bee3de5 100644 --- a/stubs/helpers.stubphp +++ b/stubs/helpers.stubphp @@ -1,30 +1,5 @@ $modelClassName - * @param TNameOrCountOrNull $nameOrCount - * @param TCountOrNull $count - * - * @return ( - TCountOrNull is int - ? Illuminate\Database\Eloquent\FactoryBuilder - : ( - TNameOrCountOrNull is int - ? Illuminate\Database\Eloquent\FactoryBuilder - : Illuminate\Database\Eloquent\FactoryBuilder - ) - - ) - */ -function factory(string $modelClassName, $nameOrCount = null, $count = null) -{ - -} - /** * Return a new response from the application. * diff --git a/tests/acceptance/FactoryTypes.feature b/tests/acceptance/FactoryTypes.feature index 1e0d0d4..aaf16a0 100644 --- a/tests/acceptance/FactoryTypes.feature +++ b/tests/acceptance/FactoryTypes.feature @@ -15,27 +15,32 @@ Feature: factory() """ - - Scenario: - Given I have the following code + And I have the following code preamble """ - */ - public function getFactory(): \Illuminate\Database\Eloquent\FactoryBuilder + * @return FactoryBuilder + */ + public function getFactory(): FactoryBuilder { return factory(User::class); } /** - * @return \Illuminate\Database\Eloquent\FactoryBuilder - */ - public function getFactoryForTwo(): \Illuminate\Database\Eloquent\FactoryBuilder + * @return FactoryBuilder + */ + public function getFactoryForTwo(): FactoryBuilder { return factory(User::class, 2); } @@ -51,17 +56,17 @@ Feature: factory() } /** - * @return \Illuminate\Database\Eloquent\Collection - */ - public function createUsers(): \Illuminate\Database\Eloquent\Collection + * @return Collection + */ + public function createUsers(): Collection { return factory(User::class, 2)->create(); } /** - * @return \Illuminate\Database\Eloquent\Collection - */ - public function createUsersWithNameAttribute(): \Illuminate\Database\Eloquent\Collection + * @return Collection + */ + public function createUsersWithNameAttribute(): Collection { return factory(User::class, 'new name', 2)->create(); } @@ -69,3 +74,22 @@ Feature: factory() """ When I run Psalm Then I see no errors + + Scenario: cannot use factory helper in Laravel 8.x and later + Given I have the "laravel/framework" package satisfying the ">= 8.0" + And I have the following code + """ + class FactoryTest { + /** + * @return FactoryBuilder + */ + public function getFactory(): FactoryBuilder + { + return factory(User::class); + } + } + """ + When I run Psalm + Then I see these errors + | Type | Message | + | UndefinedFunction | Function factory does not exist |