Skip to content

Commit b36bcb2

Browse files
blyxxyzondrejmirtes
authored andcommitted
Exclude first-party stubs from validation
Stub files from vendor directories aren't validated. But the stub files that ship with PHPstan itself weren't excluded, and they were hitting the `checkMissingOverrideMethodAttribute` check. This didn't happen when PHPstan was installed through composer, since then the stub files happened to be in a vendor directory. But it did happen when PHPstan ran as a phar. Resolves phpstan/phpstan#11907.
1 parent e4e4eaa commit b36bcb2

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/PhpDoc/DefaultStubFilesProvider.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
use function array_filter;
1111
use function array_map;
1212
use function array_values;
13+
use function dirname;
1314
use function str_contains;
15+
use function str_starts_with;
1416

1517
#[AutowiredService(as: StubFilesProvider::class)]
1618
final class DefaultStubFilesProvider implements StubFilesProvider
@@ -60,7 +62,13 @@ public function getProjectStubFiles(): array
6062
return $this->cachedProjectFiles;
6163
}
6264

65+
$phpstanStubsDirectory = $this->fileHelper->normalizePath(dirname(dirname(__DIR__)) . '/stubs');
66+
6367
$filteredStubFiles = $this->getStubFiles();
68+
$filteredStubFiles = array_filter(
69+
$filteredStubFiles,
70+
static fn (string $file): bool => !str_starts_with($file, $phpstanStubsDirectory)
71+
);
6472
foreach ($this->composerAutoloaderProjectPaths as $composerAutoloaderProjectPath) {
6573
$composerConfig = ComposerHelper::getComposerConfig($composerAutoloaderProjectPath);
6674
if ($composerConfig === null) {

tests/PHPStan/PhpDoc/DefaultStubFilesProviderTest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
use Override;
66
use PHPStan\File\FileHelper;
77
use PHPStan\Testing\PHPStanTestCase;
8+
use function dirname;
89
use function sprintf;
10+
use const DIRECTORY_SEPARATOR;
911

1012
class DefaultStubFilesProviderTest extends PHPStanTestCase
1113
{
@@ -30,10 +32,15 @@ public function testGetStubFiles(): void
3032
public function testGetProjectStubFiles(): void
3133
{
3234
$thirdPartyStubFile = sprintf('%s/vendor/thirdpartyStub.stub', $this->currentWorkingDirectory);
33-
$defaultStubFilesProvider = $this->createDefaultStubFilesProvider(['/projectStub.stub', $thirdPartyStubFile]);
35+
$firstPartyStubFile = dirname(dirname(dirname(__DIR__))) . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR . 'spl.stub';
36+
$defaultStubFilesProvider = $this->createDefaultStubFilesProvider(['/projectStub.stub', $thirdPartyStubFile, $firstPartyStubFile]);
3437
$projectStubFiles = $defaultStubFilesProvider->getProjectStubFiles();
3538
$this->assertContains('/projectStub.stub', $projectStubFiles);
3639
$this->assertNotContains($thirdPartyStubFile, $projectStubFiles);
40+
$this->assertNotContains($firstPartyStubFile, $projectStubFiles);
41+
42+
$fileHelper = new FileHelper(__DIR__);
43+
$this->assertNotContains($fileHelper->normalizePath($firstPartyStubFile), $projectStubFiles);
3744
}
3845

3946
public function testGetProjectStubFilesWhenPathContainsWindowsSeparator(): void

0 commit comments

Comments
 (0)