mirror of
https://github.com/danog/psalm-plugin-laravel.git
synced 2024-11-30 04:39:01 +01:00
Merge pull request #182 from caugner/cannot-call-factory-in-laravel-8.x
Ensure factory() helper is gone in Laravel 8+
This commit is contained in:
commit
bb47983d73
@ -1,6 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Psalm\LaravelPlugin;
|
namespace Psalm\LaravelPlugin;
|
||||||
|
|
||||||
|
use Illuminate\Foundation\Application;
|
||||||
use Psalm\LaravelPlugin\Handlers\Application\ContainerHandler;
|
use Psalm\LaravelPlugin\Handlers\Application\ContainerHandler;
|
||||||
use Psalm\LaravelPlugin\Handlers\Application\OffsetHandler;
|
use Psalm\LaravelPlugin\Handlers\Application\OffsetHandler;
|
||||||
use Psalm\LaravelPlugin\Handlers\Eloquent\ModelMethodHandler;
|
use Psalm\LaravelPlugin\Handlers\Eloquent\ModelMethodHandler;
|
||||||
@ -20,7 +21,9 @@ use Psalm\Plugin\PluginEntryPointInterface;
|
|||||||
use Psalm\Plugin\RegistrationInterface;
|
use Psalm\Plugin\RegistrationInterface;
|
||||||
use SimpleXMLElement;
|
use SimpleXMLElement;
|
||||||
use Throwable;
|
use Throwable;
|
||||||
|
use function array_merge;
|
||||||
use function dirname;
|
use function dirname;
|
||||||
|
use function explode;
|
||||||
use function glob;
|
use function glob;
|
||||||
|
|
||||||
class Plugin implements PluginEntryPointInterface
|
class Plugin implements PluginEntryPointInterface
|
||||||
@ -39,9 +42,26 @@ class Plugin implements PluginEntryPointInterface
|
|||||||
$this->registerStubs($registration);
|
$this->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
|
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);
|
$registration->addStubFile($stubFilePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
25
stubs/6/helpers.stubphp
Normal file
25
stubs/6/helpers.stubphp
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @template TModel of \Illuminate\Database\Eloquent\Model
|
||||||
|
* @template TNameOrCountOrNull of string|int|null
|
||||||
|
* @template TCountOrNull of int|null
|
||||||
|
*
|
||||||
|
* @param class-string<TModel> $modelClassName
|
||||||
|
* @param TNameOrCountOrNull $nameOrCount
|
||||||
|
* @param TCountOrNull $count
|
||||||
|
*
|
||||||
|
* @return (
|
||||||
|
TCountOrNull is int
|
||||||
|
? Illuminate\Database\Eloquent\FactoryBuilder<TModel, TCountOrNull>
|
||||||
|
: (
|
||||||
|
TNameOrCountOrNull is int
|
||||||
|
? Illuminate\Database\Eloquent\FactoryBuilder<TModel, TNameOrCountOrNull>
|
||||||
|
: Illuminate\Database\Eloquent\FactoryBuilder<TModel, 1>
|
||||||
|
)
|
||||||
|
|
||||||
|
)
|
||||||
|
*/
|
||||||
|
function factory(string $modelClassName, $nameOrCount = null, $count = null)
|
||||||
|
{
|
||||||
|
}
|
25
stubs/7/helpers.stubphp
Normal file
25
stubs/7/helpers.stubphp
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @template TModel of \Illuminate\Database\Eloquent\Model
|
||||||
|
* @template TNameOrCountOrNull of string|int|null
|
||||||
|
* @template TCountOrNull of int|null
|
||||||
|
*
|
||||||
|
* @param class-string<TModel> $modelClassName
|
||||||
|
* @param TNameOrCountOrNull $nameOrCount
|
||||||
|
* @param TCountOrNull $count
|
||||||
|
*
|
||||||
|
* @return (
|
||||||
|
TCountOrNull is int
|
||||||
|
? Illuminate\Database\Eloquent\FactoryBuilder<TModel, TCountOrNull>
|
||||||
|
: (
|
||||||
|
TNameOrCountOrNull is int
|
||||||
|
? Illuminate\Database\Eloquent\FactoryBuilder<TModel, TNameOrCountOrNull>
|
||||||
|
: Illuminate\Database\Eloquent\FactoryBuilder<TModel, 1>
|
||||||
|
)
|
||||||
|
|
||||||
|
)
|
||||||
|
*/
|
||||||
|
function factory(string $modelClassName, $nameOrCount = null, $count = null)
|
||||||
|
{
|
||||||
|
}
|
@ -1,30 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
/**
|
|
||||||
* @template TModel of \Illuminate\Database\Eloquent\Model
|
|
||||||
* @template TNameOrCountOrNull of string|int|null
|
|
||||||
* @template TCountOrNull of int|null
|
|
||||||
*
|
|
||||||
* @param class-string<TModel> $modelClassName
|
|
||||||
* @param TNameOrCountOrNull $nameOrCount
|
|
||||||
* @param TCountOrNull $count
|
|
||||||
*
|
|
||||||
* @return (
|
|
||||||
TCountOrNull is int
|
|
||||||
? Illuminate\Database\Eloquent\FactoryBuilder<TModel, TCountOrNull>
|
|
||||||
: (
|
|
||||||
TNameOrCountOrNull is int
|
|
||||||
? Illuminate\Database\Eloquent\FactoryBuilder<TModel, TNameOrCountOrNull>
|
|
||||||
: Illuminate\Database\Eloquent\FactoryBuilder<TModel, 1>
|
|
||||||
)
|
|
||||||
|
|
||||||
)
|
|
||||||
*/
|
|
||||||
function factory(string $modelClassName, $nameOrCount = null, $count = null)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return a new response from the application.
|
* Return a new response from the application.
|
||||||
*
|
*
|
||||||
|
@ -15,27 +15,32 @@ Feature: factory()
|
|||||||
</plugins>
|
</plugins>
|
||||||
</psalm>
|
</psalm>
|
||||||
"""
|
"""
|
||||||
|
And I have the following code preamble
|
||||||
Scenario:
|
|
||||||
Given I have the following code
|
|
||||||
"""
|
"""
|
||||||
<?php declare(strict_types=1);
|
<?php declare(strict_types=1);
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Collection;
|
||||||
|
use Illuminate\Database\Eloquent\FactoryBuilder;
|
||||||
use Tests\Psalm\LaravelPlugin\Models\User;
|
use Tests\Psalm\LaravelPlugin\Models\User;
|
||||||
|
"""
|
||||||
|
|
||||||
|
Scenario: can use factory helper in Laravel 6.x and 7.x
|
||||||
|
Given I have the "laravel/framework" package satisfying the "6.* || 7.*"
|
||||||
|
And I have the following code
|
||||||
|
"""
|
||||||
class FactoryTest {
|
class FactoryTest {
|
||||||
/**
|
/**
|
||||||
* @return \Illuminate\Database\Eloquent\FactoryBuilder<User, 1>
|
* @return FactoryBuilder<User, 1>
|
||||||
*/
|
*/
|
||||||
public function getFactory(): \Illuminate\Database\Eloquent\FactoryBuilder
|
public function getFactory(): FactoryBuilder
|
||||||
{
|
{
|
||||||
return factory(User::class);
|
return factory(User::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return \Illuminate\Database\Eloquent\FactoryBuilder<User, 2>
|
* @return FactoryBuilder<User, 2>
|
||||||
*/
|
*/
|
||||||
public function getFactoryForTwo(): \Illuminate\Database\Eloquent\FactoryBuilder
|
public function getFactoryForTwo(): FactoryBuilder
|
||||||
{
|
{
|
||||||
return factory(User::class, 2);
|
return factory(User::class, 2);
|
||||||
}
|
}
|
||||||
@ -51,17 +56,17 @@ Feature: factory()
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return \Illuminate\Database\Eloquent\Collection<User>
|
* @return Collection<User>
|
||||||
*/
|
*/
|
||||||
public function createUsers(): \Illuminate\Database\Eloquent\Collection
|
public function createUsers(): Collection
|
||||||
{
|
{
|
||||||
return factory(User::class, 2)->create();
|
return factory(User::class, 2)->create();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return \Illuminate\Database\Eloquent\Collection<User>
|
* @return Collection<User>
|
||||||
*/
|
*/
|
||||||
public function createUsersWithNameAttribute(): \Illuminate\Database\Eloquent\Collection
|
public function createUsersWithNameAttribute(): Collection
|
||||||
{
|
{
|
||||||
return factory(User::class, 'new name', 2)->create();
|
return factory(User::class, 'new name', 2)->create();
|
||||||
}
|
}
|
||||||
@ -69,3 +74,22 @@ Feature: factory()
|
|||||||
"""
|
"""
|
||||||
When I run Psalm
|
When I run Psalm
|
||||||
Then I see no errors
|
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<User, 1>
|
||||||
|
*/
|
||||||
|
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 |
|
||||||
|
Loading…
Reference in New Issue
Block a user