Merge pull request #226 from ronb-lendesk/support-newer-ide-helper

Add support for later versions of IDE helper
This commit is contained in:
feek 2022-03-21 15:49:13 -04:00 committed by GitHub
commit f7d22b2273
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 130 additions and 23 deletions

View File

@ -24,7 +24,7 @@
"illuminate/view": "^6.0 || ^8.0",
"vimeo/psalm": "^4.8.1",
"orchestra/testbench": "^3.8 || ^4.0 || ^5.0 || ^6.22 || ^7.0",
"barryvdh/laravel-ide-helper": ">=2.8.0 <2.9.2"
"barryvdh/laravel-ide-helper": ">=2.8.0"
},
"require-dev": {
"codeception/codeception": "^4.1.6",
@ -64,7 +64,10 @@
}
},
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
},
"minimum-stability": "dev",
"prefer-stable": true

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

@ -7,12 +7,15 @@ use Barryvdh\LaravelIdeHelper\Console\MetaCommand;
class FakeMetaCommand extends MetaCommand
{
/**
* @return void
* @return callable
*/
protected function registerClassAutoloadExceptions()
protected function registerClassAutoloadExceptions(): callable
{
// by default, the ide-helper throws exceptions when it cannot find a class. However it does not unregister that
// autoloader when it is done, and we certainly do not want to throw exceptions when we are simply checking if
// a certain class exists. We are instead changing this to be a noop.
return function () {
};
}
}

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>