Skip to content

Fix inconsistent analysis on case-insensitive filesystems #3988

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 2.1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion .github/workflows/e2e-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ jobs:

e2e-tests:
name: "E2E tests"
runs-on: "ubuntu-latest"
runs-on: ${{ matrix.os }}
timeout-minutes: 60

strategy:
Expand All @@ -317,84 +317,127 @@ jobs:
- script: "bin/phpstan analyse -l 8 -a tests/e2e/data/timecop.php -c tests/e2e/data/empty.neon tests/e2e/data/timecop.php"
tools: "pecl"
extensions: "timecop-beta"
os: "ubuntu-latest"
- script: "bin/phpstan analyse -l 8 -a tests/e2e/data/soap.php -c tests/e2e/data/empty.neon tests/e2e/data/soap.php"
extensions: "soap"
os: "ubuntu-latest"
- script: "bin/phpstan analyse -l 8 -a tests/e2e/data/soap.php -c tests/e2e/data/empty.neon tests/e2e/data/soap.php"
extensions: ""
os: "ubuntu-latest"
- script: "bin/phpstan analyse -l 8 tests/e2e/anon-class/Granularity.php"
extensions: ""
os: "ubuntu-latest"
- script: "bin/phpstan analyse -l 8 e2e/phpstan-phpunit-190/test.php -c e2e/phpstan-phpunit-190/test.neon"
extensions: ""
os: "ubuntu-latest"
- script: "bin/phpstan analyse e2e/only-files-not-analysed-trait/src -c e2e/only-files-not-analysed-trait/ignore.neon"
extensions: ""
os: "ubuntu-latest"
- script: "bin/phpstan analyse e2e/only-files-not-analysed-trait/src/Foo.php e2e/only-files-not-analysed-trait/src/BarTrait.php -c e2e/only-files-not-analysed-trait/no-ignore.neon"
extensions: ""
os: "ubuntu-latest"
- script: |
cd e2e/baseline-uninit-prop-trait
../../bin/phpstan analyse --debug --configuration test-no-baseline.neon --generate-baseline test-baseline.neon
../../bin/phpstan analyse --debug --configuration test.neon
os: "ubuntu-latest"
- script: |
cd e2e/baseline-uninit-prop-trait
../../bin/phpstan analyse --configuration test-no-baseline.neon --generate-baseline test-baseline.neon
../../bin/phpstan analyse --configuration test.neon
os: "ubuntu-latest"
- script: |
cd e2e/discussion-11362
composer install
../../bin/phpstan
os: "ubuntu-latest"
- script: |
cd e2e/bug-11819
../../bin/phpstan
os: "ubuntu-latest"
- script: |
cd e2e/composer-and-phpstan-version-config
composer install --ignore-platform-reqs
../../bin/phpstan analyze test.php --level=0
os: "ubuntu-latest"
- script: |
cd e2e/composer-max-version
composer install
../../bin/phpstan analyze test.php --level=0
os: "ubuntu-latest"
- script: |
cd e2e/composer-min-max-version
composer install
../../bin/phpstan analyze test.php --level=0
os: "ubuntu-latest"
- script: |
cd e2e/composer-min-open-end-version
composer install
../../bin/phpstan analyze test.php --level=0
os: "ubuntu-latest"
- script: |
cd e2e/composer-min-version-v5
composer install --ignore-platform-reqs
../../bin/phpstan analyze test.php --level=0
os: "ubuntu-latest"
- script: |
cd e2e/composer-min-version-v7
composer install --ignore-platform-reqs
../../bin/phpstan analyze test.php --level=0
os: "ubuntu-latest"
- script: |
cd e2e/composer-min-version
composer install
../../bin/phpstan analyze test.php --level=0
os: "ubuntu-latest"
- script: |
cd e2e/composer-no-versions
composer install
../../bin/phpstan analyze test.php --level=0
os: "ubuntu-latest"
- script: |
cd e2e/composer-version-config-invalid
OUTPUT=$(../bashunit -a exit_code "1" ../../bin/phpstan)
echo "$OUTPUT"
../bashunit -a contains 'Invalid configuration' "$OUTPUT"
../bashunit -a contains 'Invalid PHP version range: phpVersion.max should be greater or equal to phpVersion.min.' "$OUTPUT"
os: "ubuntu-latest"
- script: |
cd e2e/composer-version-config-patch
composer install --ignore-platform-reqs
../../bin/phpstan analyze test.php --level=0
os: "ubuntu-latest"
- script: |
cd e2e/composer-version-config
composer install
../../bin/phpstan analyze test.php --level=0
os: "ubuntu-latest"
- script: |
cd e2e/bug-12972
OUTPUT=$(../bashunit -a exit_code "1" ../../bin/phpstan)
echo "$OUTPUT"
../bashunit -a contains 'Internal error: Failed opening required' "$OUTPUT"
../bashunit -a contains 'e2e/bug-12972/src/OTHER/file.php' "$OUTPUT"
os: "ubuntu-latest"
- script: |
cd e2e/bug-12972
../../bin/phpstan analyze
os: "macos-latest"
- script: |
cd e2e/bug-12972
../../bin/phpstan analyze
os: "windows-latest"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't get it. Why do you expect different outcomes on Linux vs. the rest?

Copy link
Contributor Author

@staabm staabm May 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because linux does not have a case-insensitive filesystem.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So you have a project that deliberately works only on case-insensitive filesystems?

Copy link
Contributor Author

@staabm staabm May 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the test mimics what php-src will do at runtime.
it will happily accept a different-cased path on windows/macos but doesn't on linux (acutally depending on the filesystem case-sensitivy not the OS itself).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like if the e2e test mimiced how your project actually worked. This just looks like PHPStan fixes macOS but breaks Linux.

steps:
- name: "Checkout"
uses: actions/checkout@v4

- name: "Install GNU Patch on macOS" # see https://github.com/cweagans/composer-patches/issues/326
if: runner.os == 'macOS'
run: |
brew install gpatch
echo "/opt/homebrew/opt/gpatch/libexec/gnubin" >> $GITHUB_PATH
Comment on lines +435 to +440
Copy link
Contributor Author

@staabm staabm May 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on macOS we need a GNU compatible patch command, as the apple-shipped patch command cannot apply our patch files:

Error: Cannot apply patch 0 (patches/cloudflare-ca.patch)!

In Patches.php line 331:
                                                       
  Cannot apply patch 0 (patches/cloudflare-ca.patch)!  

- name: "Install PHP"
uses: "shivammathur/setup-php@v2"
with:
Expand Down
7 changes: 7 additions & 0 deletions e2e/bug-12972/autoloader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

spl_autoload_register(function($class) {
if ($class === \other12972\MyClass::class) {
require_once __DIR__ . '/src/OTHER/file.php'; // wrong case sensitivity on purpose
}
});
8 changes: 8 additions & 0 deletions e2e/bug-12972/phpstan.dist.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
parameters:
level: 9

paths:
- src

bootstrapFiles:
- autoloader.php
7 changes: 7 additions & 0 deletions e2e/bug-12972/src/folder/file2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Foo12972;

use other12972\MyClass;

function doBar(MyClass $myClass):void {}
10 changes: 10 additions & 0 deletions e2e/bug-12972/src/other/file.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace other12972;

class MyClass {
public function doSomething(): int
{
return 1;
}
}
44 changes: 44 additions & 0 deletions src/Analyser/AnalysedFilesResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php declare(strict_types = 1);

namespace PHPStan\Analyser;

use PHPStan\File\FilesystemHelper;
use function array_fill_keys;
use function array_map;
use function strtolower;

final class AnalysedFilesResolver
{

/** @var bool[] filePath(string) => bool(true) */
private array $analysedFiles;

/**
* @param string[] $files
*/
public function __construct(array $files = [])
{
$this->setAnalysedFiles($files);
}

/**
* @param string[] $files
*/
public function setAnalysedFiles(array $files): void
{
if (FilesystemHelper::isCaseSensitive() === false) {
$files = array_map(static fn (string $file): string => strtolower($file), $files);
}
$this->analysedFiles = array_fill_keys($files, true);
}

public function isInAnalyzedFiles(string $file): bool
{
if (FilesystemHelper::isCaseSensitive() === false) {
$file = strtolower($file);
}

return isset($this->analysedFiles[$file]);
}

}
5 changes: 2 additions & 3 deletions src/Analyser/Analyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use PHPStan\Collectors\Registry as CollectorRegistry;
use PHPStan\Rules\Registry as RuleRegistry;
use Throwable;
use function array_fill_keys;
use function array_merge;
use function count;
use function memory_get_peak_usage;
Expand Down Expand Up @@ -47,7 +46,7 @@ public function analyse(
}

$this->nodeScopeResolver->setAnalysedFiles($allAnalysedFiles);
$allAnalysedFiles = array_fill_keys($allAnalysedFiles, true);
$analyzedFilesResolver = new AnalysedFilesResolver($allAnalysedFiles);

/** @var list<Error> $errors */
$errors = [];
Expand Down Expand Up @@ -78,7 +77,7 @@ public function analyse(
try {
$fileAnalyserResult = $this->fileAnalyser->analyseFile(
$file,
$allAnalysedFiles,
$analyzedFilesResolver,
$this->ruleRegistry,
$this->collectorRegistry,
null,
Expand Down
21 changes: 9 additions & 12 deletions src/Analyser/FileAnalyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,11 @@ public function __construct(
}

/**
* @param array<string, true> $analysedFiles
* @param callable(Node $node, Scope $scope): void|null $outerNodeCallback
*/
public function analyseFile(
string $file,
array $analysedFiles,
AnalysedFilesResolver $analysedFilesResolver,
RuleRegistry $ruleRegistry,
CollectorRegistry $collectorRegistry,
?callable $outerNodeCallback,
Expand All @@ -88,13 +87,14 @@ public function analyseFile(
$exportedNodes = [];
$linesToIgnore = [];
$unmatchedLineIgnores = [];

if (is_file($file)) {
try {
$this->collectErrors($analysedFiles);
$this->collectErrors($analysedFilesResolver);
$parserNodes = $this->parser->parseFile($file);
$linesToIgnore = $unmatchedLineIgnores = [$file => $this->getLinesToIgnoreFromTokens($parserNodes)];
$temporaryFileErrors = [];
$nodeCallback = function (Node $node, Scope $scope) use (&$fileErrors, &$fileCollectedData, &$fileDependencies, &$usedTraitFileDependencies, &$exportedNodes, $file, $ruleRegistry, $collectorRegistry, $outerNodeCallback, $analysedFiles, &$linesToIgnore, &$unmatchedLineIgnores, &$temporaryFileErrors): void {
$nodeCallback = function (Node $node, Scope $scope) use (&$fileErrors, &$fileCollectedData, &$fileDependencies, &$usedTraitFileDependencies, &$exportedNodes, $file, $ruleRegistry, $collectorRegistry, $outerNodeCallback, $analysedFilesResolver, &$linesToIgnore, &$unmatchedLineIgnores, &$temporaryFileErrors): void {
if ($node instanceof Node\Stmt\Trait_) {
foreach (array_keys($linesToIgnore[$file] ?? []) as $lineToIgnore) {
if ($lineToIgnore < $node->getStartLine() || $lineToIgnore > $node->getEndLine()) {
Expand Down Expand Up @@ -205,7 +205,7 @@ public function analyseFile(

try {
$dependencies = $this->dependencyResolver->resolveDependencies($node, $scope);
foreach ($dependencies->getFileDependencies($scope->getFile(), $analysedFiles) as $dependentFile) {
foreach ($dependencies->getFileDependencies($scope->getFile(), $analysedFilesResolver) as $dependentFile) {
$fileDependencies[] = $dependentFile;
}
if ($dependencies->getExportedNode() !== null) {
Expand All @@ -224,7 +224,7 @@ public function analyseFile(
}

$usedTraitDependencies = $this->dependencyResolver->resolveUsedTraitDependencies($node);
foreach ($usedTraitDependencies->getFileDependencies($scope->getFile(), $analysedFiles) as $dependentFile) {
foreach ($usedTraitDependencies->getFileDependencies($scope->getFile(), $analysedFilesResolver) as $dependentFile) {
$usedTraitFileDependencies[] = $dependentFile;
}
};
Expand Down Expand Up @@ -330,14 +330,11 @@ private function getLinesToIgnoreFromTokens(array $nodes): array
return $nodes[0]->getAttribute('linesToIgnore', []);
}

/**
* @param array<string, true> $analysedFiles
*/
private function collectErrors(array $analysedFiles): void
private function collectErrors(AnalysedFilesResolver $analyzedFilesResolver): void
{
$this->filteredPhpErrors = [];
$this->allPhpErrors = [];
set_error_handler(function (int $errno, string $errstr, string $errfile, int $errline) use ($analysedFiles): bool {
set_error_handler(function (int $errno, string $errstr, string $errfile, int $errline) use ($analyzedFilesResolver): bool {
if ((error_reporting() & $errno) === 0) {
// silence @ operator
return true;
Expand All @@ -351,7 +348,7 @@ private function collectErrors(array $analysedFiles): void
return true;
}

if (!isset($analysedFiles[$errfile])) {
if (!$analyzedFilesResolver->isInAnalyzedFiles($errfile)) {
return true;
}

Expand Down
11 changes: 4 additions & 7 deletions src/Analyser/Ignore/IgnoredErrorHelperResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

namespace PHPStan\Analyser\Ignore;

use PHPStan\Analyser\AnalysedFilesResolver;
use PHPStan\Analyser\Error;
use PHPStan\File\FileHelper;
use PHPStan\ShouldNotHappenException;
use function array_fill_keys;
use function array_key_exists;
use function array_values;
use function count;
use function is_array;
Expand Down Expand Up @@ -43,12 +42,11 @@ public function getErrors(): array

/**
* @param Error[] $errors
* @param string[] $analysedFiles
*/
public function process(
array $errors,
bool $onlyFiles,
array $analysedFiles,
AnalysedFilesResolver $analysedFilesResolver,
bool $hasInternalErrors,
): IgnoredErrorHelperProcessedResult
{
Expand Down Expand Up @@ -190,8 +188,6 @@ public function process(
), $unmatchedIgnoredError['file'], $unmatchedIgnoredError['line'], false))->withIdentifier('ignore.count');
}

$analysedFilesKeys = array_fill_keys($analysedFiles, true);

if (!$hasInternalErrors) {
foreach ($unmatchedIgnoredErrors as $unmatchedIgnoredError) {
$reportUnmatched = $unmatchedIgnoredError['reportUnmatched'] ?? $this->reportUnmatchedIgnoredErrors;
Expand All @@ -214,7 +210,8 @@ public function process(
), $unmatchedIgnoredError['file'], $unmatchedIgnoredError['line'], false))->withIdentifier('ignore.count');
}
} elseif (isset($unmatchedIgnoredError['realPath'])) {
if (!array_key_exists($unmatchedIgnoredError['realPath'], $analysedFilesKeys)) {

if (!$analysedFilesResolver->isInAnalyzedFiles($unmatchedIgnoredError['realPath'])) {
continue;
}

Expand Down
Loading
Loading