Skip to content

Commit 0d83f12

Browse files
committed
added feature detect
1 parent 829bbf8 commit 0d83f12

File tree

3 files changed

+48
-6
lines changed

3 files changed

+48
-6
lines changed

src/Analyser/AnalysedFilesResolver.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,26 @@
22

33
namespace PHPStan\Analyser;
44

5+
use PHPStan\File\FilesystemHelper;
56
use function array_fill_keys;
67
use function array_map;
78
use function strtolower;
8-
use const PHP_OS_FAMILY;
99

1010
final class AnalysedFilesResolver
1111
{
1212

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

16-
private bool $caseInsensitiveFilesystem;
17-
1816
/**
1917
* @param string[] $files
2018
*/
2119
public function __construct(array $files = [])
2220
{
23-
$this->caseInsensitiveFilesystem = PHP_OS_FAMILY === 'Windows' || PHP_OS_FAMILY === 'Darwin';
21+
if ($files === []) {
22+
return;
23+
}
24+
2425
$this->setAnalysedFiles($files);
2526
}
2627

@@ -29,15 +30,15 @@ public function __construct(array $files = [])
2930
*/
3031
public function setAnalysedFiles(array $files): void
3132
{
32-
if ($this->caseInsensitiveFilesystem) {
33+
if (FilesystemHelper::isCaseSensitive() === false) {
3334
$files = array_map(static fn (string $file): string => strtolower($file), $files);
3435
}
3536
$this->analysedFiles = array_fill_keys($files, true);
3637
}
3738

3839
public function isInAnalyzedFiles(string $file): bool
3940
{
40-
if ($this->caseInsensitiveFilesystem) {
41+
if (FilesystemHelper::isCaseSensitive() === false) {
4142
$file = strtolower($file);
4243
}
4344

src/DependencyInjection/ContainerFactory.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use PHPStan\BetterReflection\SourceLocator\Type\SourceLocator;
2525
use PHPStan\Command\CommandHelper;
2626
use PHPStan\File\FileHelper;
27+
use PHPStan\File\FilesystemHelper;
2728
use PHPStan\Node\Printer\Printer;
2829
use PHPStan\Php\PhpVersion;
2930
use PHPStan\Reflection\PhpVersionStaticAccessor;
@@ -190,6 +191,7 @@ public static function postInitializeContainer(Container $container): void
190191
$container->getService('typeSpecifier');
191192

192193
BleedingEdgeToggle::setBleedingEdge($container->getParameter('featureToggles')['bleedingEdge']);
194+
FilesystemHelper::checkIsCaseSensitive($container->getParameter('tmpDir'));
193195
}
194196

195197
public function getCurrentWorkingDirectory(): string

src/File/FilesystemHelper.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\File;
4+
5+
use function file_get_contents;
6+
use function file_put_contents;
7+
use function strtoupper;
8+
9+
final class FilesystemHelper
10+
{
11+
12+
private const SIGNATURE_CONTENT = 'PHPStan case-sensitivity check';
13+
14+
private static ?bool $isCaseSensitive = null;
15+
16+
public static function isCaseSensitive(): bool
17+
{
18+
return self::$isCaseSensitive;
19+
}
20+
21+
public static function checkIsCaseSensitive(string $tmpDir): void
22+
{
23+
if (self::$isCaseSensitive !== null) {
24+
return;
25+
}
26+
27+
$fileName = 'test-file-system-case-sensitivity.tmp';
28+
@file_put_contents($tmpDir . '/' . $fileName, self::SIGNATURE_CONTENT);
29+
30+
if (@file_get_contents($tmpDir . '/' . strtoupper($fileName)) === self::SIGNATURE_CONTENT) {
31+
self::$isCaseSensitive = false;
32+
33+
return;
34+
}
35+
36+
self::$isCaseSensitive = true;
37+
}
38+
39+
}

0 commit comments

Comments
 (0)