Skip to content

Commit

Permalink
Merge pull request #226 from ronb-lendesk/support-newer-ide-helper
Browse files Browse the repository at this point in the history
Add support for later versions of IDE helper
  • Loading branch information
mr-feek authored Mar 21, 2022
2 parents 902d217 + 17c59ef commit f7d22b2
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 23 deletions.
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -64,7 +64,10 @@
}
},
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
},
"minimum-stability": "dev",
"prefer-stable": true
Expand Down
8 changes: 7 additions & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
7 changes: 5 additions & 2 deletions src/Fakes/FakeMetaCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
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 () {
};
}
}
32 changes: 32 additions & 0 deletions src/Fakes/FakeModelsCommand210.php
Original file line number Diff line number Diff line change
@@ -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);
}
}
35 changes: 35 additions & 0 deletions src/Fakes/FakeModelsCommand291.php
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
37 changes: 37 additions & 0 deletions src/Providers/FakeModelsCommandProvider.php
Original file line number Diff line number Diff line change
@@ -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
);
}
}
4 changes: 1 addition & 3 deletions src/Providers/ModelStubProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@

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;

Expand Down Expand Up @@ -37,7 +35,7 @@ public static function generateStubFile(): void

$fake_filesystem = new FakeFilesystem();

$models_generator_command = new FakeModelsCommand(
$models_generator_command = FakeModelsCommandProvider::getCommand(
$fake_filesystem,
$schema_aggregator
);
Expand Down
5 changes: 5 additions & 0 deletions tests/laravel-test-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down

0 comments on commit f7d22b2

Please sign in to comment.