Skip to content

Commit e50f7f0

Browse files
authored
Merge pull request #2 from tm1000/laravel-9-test-fixes
Laravel 9 Test fixes
2 parents 0dc88d3 + 48b4fd7 commit e50f7f0

13 files changed

+126
-128
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
fail-fast: false
1010
matrix:
1111
php: [8.0, 8.1]
12-
illuminate_version: [8.67.*, 9.*]
12+
illuminate_version: [8.67.*, 9.7.*]
1313
stability: [prefer-lowest, prefer-stable]
1414

1515
name: P${{ matrix.php }} | I ${{ matrix.illuminate_version }} | ${{ matrix.stability }}

psalm.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,15 @@
2121
<PropertyNotSetInConstructor>
2222
<errorLevel type="suppress">
2323
<file name="src/Fakes/FakeMetaCommand.php" />
24-
<file name="src/Fakes/FakeModelsCommand.php" />
24+
<file name="src/Fakes/FakeModelsCommand291.php" />
25+
<file name="src/Fakes/FakeModelsCommand210.php" />
2526
</errorLevel>
2627
</PropertyNotSetInConstructor>
28+
<OverriddenMethodAccess>
29+
<errorLevel type="suppress">
30+
<file name="src/Fakes/FakeModelsCommand291.php" />
31+
</errorLevel>
32+
</OverriddenMethodAccess>
2733
</issueHandlers>
2834

2935
<stubs>

src/Fakes/FakeMetaCommand.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
class FakeMetaCommand extends MetaCommand
88
{
9+
/**
10+
* @return callable
11+
*/
912
protected function registerClassAutoloadExceptions(): callable
1013
{
1114
// by default, the ide-helper throws exceptions when it cannot find a class. However it does not unregister that

src/Fakes/FakeModelsCommand210.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Psalm\LaravelPlugin\Fakes;
4+
5+
use Barryvdh\LaravelIdeHelper\Console\ModelsCommand;
6+
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Filesystem\Filesystem;
8+
use Psalm\LaravelPlugin\Handlers\Eloquent\Schema\SchemaAggregator;
9+
10+
class FakeModelsCommand210 extends ModelsCommand
11+
{
12+
use FakeModelsCommandLogic;
13+
14+
/** @var SchemaAggregator */
15+
private $schema;
16+
17+
public function __construct(Filesystem $files, SchemaAggregator $schema)
18+
{
19+
parent::__construct($files);
20+
$this->schema = $schema;
21+
}
22+
23+
/**
24+
* Load the properties from the database table.
25+
*
26+
* @param Model $model
27+
*/
28+
public function getPropertiesFromTable($model): void
29+
{
30+
$this->getProperties($model);
31+
}
32+
}

src/Fakes/FakeModelsCommand291.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Psalm\LaravelPlugin\Fakes;
4+
5+
use Barryvdh\LaravelIdeHelper\Console\ModelsCommand;
6+
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Filesystem\Filesystem;
8+
use Psalm\LaravelPlugin\Handlers\Eloquent\Schema\SchemaAggregator;
9+
10+
class FakeModelsCommand291 extends ModelsCommand
11+
{
12+
use FakeModelsCommandLogic;
13+
14+
/** @var SchemaAggregator */
15+
private $schema;
16+
17+
/** @var array<class-string> */
18+
private $model_classes = [];
19+
20+
public function __construct(Filesystem $files, SchemaAggregator $schema)
21+
{
22+
parent::__construct($files);
23+
$this->schema = $schema;
24+
}
25+
26+
/**
27+
* Load the properties from the database table.
28+
*
29+
* @param Model $model
30+
*/
31+
protected function getPropertiesFromTable($model): void
32+
{
33+
$this->getProperties($model);
34+
}
35+
}

src/Fakes/FakeModelsCommand.php renamed to src/Fakes/FakeModelsCommandLogic.php

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,21 @@
22

33
namespace Psalm\LaravelPlugin\Fakes;
44

5-
use Barryvdh\LaravelIdeHelper\Console\ModelsCommand;
65
use Illuminate\Database\Eloquent\Model;
7-
use Illuminate\Filesystem\Filesystem;
86
use Illuminate\Support\Str;
9-
use Psalm\LaravelPlugin\Handlers\Eloquent\Schema\SchemaAggregator;
107

118
use function config;
129
use function get_class;
13-
use function implode;
1410
use function in_array;
11+
use function implode;
1512

16-
class FakeModelsCommand extends ModelsCommand
13+
trait FakeModelsCommandLogic
1714
{
18-
/** @var SchemaAggregator */
19-
private $schema;
20-
2115
/** @var array<class-string> */
2216
private $model_classes = [];
2317

24-
public function __construct(Filesystem $files, SchemaAggregator $schema)
25-
{
26-
parent::__construct($files);
27-
$this->schema = $schema;
28-
}
29-
3018
/** @return array<class-string> */
31-
public function getModels()
19+
public function getModels(): array
3220
{
3321
return $this->model_classes + $this->loadModels();
3422
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Psalm\LaravelPlugin\Providers;
4+
5+
use Barryvdh\LaravelIdeHelper\Console\ModelsCommand;
6+
use Composer\InstalledVersions;
7+
use Illuminate\Filesystem\Filesystem;
8+
use Psalm\LaravelPlugin\Fakes\FakeModelsCommand210;
9+
use Psalm\LaravelPlugin\Fakes\FakeModelsCommand291;
10+
use Psalm\LaravelPlugin\Handlers\Eloquent\Schema\SchemaAggregator;
11+
12+
use function version_compare;
13+
use function assert;
14+
use function is_string;
15+
16+
class FakeModelsCommandProvider
17+
{
18+
public static function getCommand(Filesystem $filesystem, SchemaAggregator $schemaAggregator): ModelsCommand
19+
{
20+
$ideHelperVersion = InstalledVersions::getVersion('barryvdh/laravel-ide-helper');
21+
22+
assert(is_string($ideHelperVersion));
23+
24+
// ide-helper released a breaking change in a non-major version. As a result, we need to monkey patch our code
25+
if (version_compare($ideHelperVersion, '2.9.2', '<')) {
26+
return new FakeModelsCommand291(
27+
$filesystem,
28+
$schemaAggregator
29+
);
30+
}
31+
32+
return new FakeModelsCommand210(
33+
$filesystem,
34+
$schemaAggregator
35+
);
36+
}
37+
}

src/Providers/ModelStubProvider.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@
44

55
use Psalm\Internal\Analyzer\ProjectAnalyzer;
66
use Psalm\LaravelPlugin\Fakes\FakeFilesystem;
7-
use Psalm\LaravelPlugin\Fakes\FakeModelsCommand;
87
use Psalm\LaravelPlugin\Handlers\Eloquent\Schema\SchemaAggregator;
98
use Symfony\Component\Console\Input\ArrayInput;
109
use Symfony\Component\Console\Output\NullOutput;
1110

12-
use function dirname;
1311
use function glob;
1412
use function unlink;
1513

@@ -37,7 +35,7 @@ public static function generateStubFile(): void
3735

3836
$fake_filesystem = new FakeFilesystem();
3937

40-
$models_generator_command = new FakeModelsCommand(
38+
$models_generator_command = FakeModelsCommandProvider::getCommand(
4139
$fake_filesystem,
4240
$schema_aggregator
4341
);

tests/Support/Module.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,9 @@ public function runPsalmIn(string $dir, array $options = []): void
147147
/**
148148
* @Then I see exit code :code
149149
*/
150-
public function seeExitCode(int $exitCode): void
150+
public function seeExitCode(string $exitCode): void
151151
{
152-
if ($this->exitCode === $exitCode) {
152+
if ($this->exitCode === (int) $exitCode) {
153153
return;
154154
}
155155

tests/acceptance/EloquentBuilderTypes.feature

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -177,32 +177,6 @@ Feature: Eloquent Builder types
177177
When I run Psalm
178178
Then I see no errors
179179

180-
Scenario: cannot call firstOrNew and firstOrCreate without parameters in Laravel 6.x
181-
Given I have the "laravel/framework" package satisfying the "6.*"
182-
And I have the following code
183-
"""
184-
/**
185-
* @psalm-param Builder<User> $builder
186-
* @psalm-return User
187-
*/
188-
function test_firstOrCreate(Builder $builder): User {
189-
return $builder->firstOrCreate();
190-
}
191-
192-
/**
193-
* @psalm-param Builder<User> $builder
194-
* @psalm-return User
195-
*/
196-
function test_firstOrNew(Builder $builder): User {
197-
return $builder->firstOrNew();
198-
}
199-
"""
200-
When I run Psalm
201-
Then I see these errors
202-
| Type | Message |
203-
| TooFewArguments | Too few arguments for method Illuminate\Database\Eloquent\Builder::firstorcreate saw 0 |
204-
| TooFewArguments | Too few arguments for method Illuminate\Database\Eloquent\Builder::firstornew saw 0 |
205-
206180
Scenario: can call firstOrNew and firstOrCreate without parameters in Laravel 8.x
207181
Given I have the "laravel/framework" package satisfying the ">= 8.0"
208182
And I have the following code

0 commit comments

Comments
 (0)