Merge branch 'master' into laravel-9-test-fixes

This commit is contained in:
Andrew Nagy 2022-04-07 20:01:01 +00:00
commit 813874b655
8 changed files with 123 additions and 19 deletions

View File

@ -21,9 +21,15 @@
<PropertyNotSetInConstructor>
<errorLevel type="suppress">
<file name="src/Fakes/FakeMetaCommand.php" />
<file name="src/Fakes/FakeModelsCommand.php" />
<file name="src/Fakes/FakeModelsCommand291.php" />
<file name="src/Fakes/FakeModelsCommand210.php" />
</errorLevel>
</PropertyNotSetInConstructor>
<OverriddenMethodAccess>
<errorLevel type="suppress">
<file name="src/Fakes/FakeModelsCommand291.php" />
</errorLevel>
</OverriddenMethodAccess>
</issueHandlers>
<stubs>

View File

@ -6,6 +6,9 @@ use Barryvdh\LaravelIdeHelper\Console\MetaCommand;
class FakeMetaCommand extends MetaCommand
{
/**
* @return callable
*/
protected function registerClassAutoloadExceptions(): callable
{
// by default, the ide-helper throws exceptions when it cannot find a class. However it does not unregister that

View File

@ -0,0 +1,32 @@
<?php
namespace Psalm\LaravelPlugin\Fakes;
use Barryvdh\LaravelIdeHelper\Console\ModelsCommand;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Filesystem\Filesystem;
use Psalm\LaravelPlugin\Handlers\Eloquent\Schema\SchemaAggregator;
class FakeModelsCommand210 extends ModelsCommand
{
use FakeModelsCommandLogic;
/** @var SchemaAggregator */
private $schema;
public function __construct(Filesystem $files, SchemaAggregator $schema)
{
parent::__construct($files);
$this->schema = $schema;
}
/**
* Load the properties from the database table.
*
* @param Model $model
*/
public function getPropertiesFromTable($model): void
{
$this->getProperties($model);
}
}

View File

@ -0,0 +1,35 @@
<?php
namespace Psalm\LaravelPlugin\Fakes;
use Barryvdh\LaravelIdeHelper\Console\ModelsCommand;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Filesystem\Filesystem;
use Psalm\LaravelPlugin\Handlers\Eloquent\Schema\SchemaAggregator;
class FakeModelsCommand291 extends ModelsCommand
{
use FakeModelsCommandLogic;
/** @var SchemaAggregator */
private $schema;
/** @var array<class-string> */
private $model_classes = [];
public function __construct(Filesystem $files, SchemaAggregator $schema)
{
parent::__construct($files);
$this->schema = $schema;
}
/**
* Load the properties from the database table.
*
* @param Model $model
*/
protected function getPropertiesFromTable($model): void
{
$this->getProperties($model);
}
}

View File

@ -2,33 +2,21 @@
namespace Psalm\LaravelPlugin\Fakes;
use Barryvdh\LaravelIdeHelper\Console\ModelsCommand;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Str;
use Psalm\LaravelPlugin\Handlers\Eloquent\Schema\SchemaAggregator;
use function config;
use function get_class;
use function implode;
use function in_array;
use function implode;
class FakeModelsCommand extends ModelsCommand
trait FakeModelsCommandLogic
{
/** @var SchemaAggregator */
private $schema;
/** @var array<class-string> */
private $model_classes = [];
public function __construct(Filesystem $files, SchemaAggregator $schema)
{
parent::__construct($files);
$this->schema = $schema;
}
/** @return array<class-string> */
public function getModels()
public function getModels(): array
{
return $this->model_classes + $this->loadModels();
}

View File

@ -0,0 +1,37 @@
<?php
namespace Psalm\LaravelPlugin\Providers;
use Barryvdh\LaravelIdeHelper\Console\ModelsCommand;
use Composer\InstalledVersions;
use Illuminate\Filesystem\Filesystem;
use Psalm\LaravelPlugin\Fakes\FakeModelsCommand210;
use Psalm\LaravelPlugin\Fakes\FakeModelsCommand291;
use Psalm\LaravelPlugin\Handlers\Eloquent\Schema\SchemaAggregator;
use function version_compare;
use function assert;
use function is_string;
class FakeModelsCommandProvider
{
public static function getCommand(Filesystem $filesystem, SchemaAggregator $schemaAggregator): ModelsCommand
{
$ideHelperVersion = InstalledVersions::getVersion('barryvdh/laravel-ide-helper');
assert(is_string($ideHelperVersion));
// ide-helper released a breaking change in a non-major version. As a result, we need to monkey patch our code
if (version_compare($ideHelperVersion, '2.9.2', '<')) {
return new FakeModelsCommand291(
$filesystem,
$schemaAggregator
);
}
return new FakeModelsCommand210(
$filesystem,
$schemaAggregator
);
}
}

View File

@ -4,12 +4,10 @@ namespace Psalm\LaravelPlugin\Providers;
use Psalm\Internal\Analyzer\ProjectAnalyzer;
use Psalm\LaravelPlugin\Fakes\FakeFilesystem;
use Psalm\LaravelPlugin\Fakes\FakeModelsCommand;
use Psalm\LaravelPlugin\Handlers\Eloquent\Schema\SchemaAggregator;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;
use function dirname;
use function glob;
use function unlink;
@ -37,7 +35,7 @@ final class ModelStubProvider implements GeneratesStubs
$fake_filesystem = new FakeFilesystem();
$models_generator_command = new FakeModelsCommand(
$models_generator_command = FakeModelsCommandProvider::getCommand(
$fake_filesystem,
$schema_aggregator
);

View File

@ -89,6 +89,11 @@
<code>ExampleListener</code>
</UnusedClass>
</file>
<file src="app/Models/Example.php">
<UnusedClass occurrences="1">
<code>Example</code>
</UnusedClass>
</file>
<file src="app/Models/User.php">
<NonInvariantDocblockPropertyType occurrences="3">
<code>$casts</code>