diff --git a/src/Command/CommandHelper.php b/src/Command/CommandHelper.php index e8f56e6687..b3c4b909ea 100644 --- a/src/Command/CommandHelper.php +++ b/src/Command/CommandHelper.php @@ -16,6 +16,7 @@ use PHPStan\DependencyInjection\NeonAdapter; use PHPStan\File\FileFinder; use PHPStan\File\FileHelper; +use PHPStan\File\FileReader; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\ConsoleOutputInterface; use Symfony\Component\Console\Output\OutputInterface; @@ -109,9 +110,11 @@ public static function begin( throw new \PHPStan\Command\InceptionNotSuccessfulException(); } - $pathsString = file_get_contents($pathsFile); - if ($pathsString === false) { - throw new \PHPStan\ShouldNotHappenException(); + try { + $pathsString = FileReader::read($pathsFile); + } catch (\PHPStan\File\CouldNotReadFileException $e) { + $errorOutput->writeLineFormatted($e->getMessage()); + throw new \PHPStan\Command\InceptionNotSuccessfulException(); } $paths = array_values(array_filter(explode("\n", $pathsString), static function (string $path): bool { @@ -225,10 +228,7 @@ public static function begin( $memoryLimitFile = $container->getParameter('memoryLimitFile'); if (file_exists($memoryLimitFile)) { - $memoryLimitFileContents = file_get_contents($memoryLimitFile); - if ($memoryLimitFileContents === false) { - throw new \PHPStan\ShouldNotHappenException(); - } + $memoryLimitFileContents = FileReader::read($memoryLimitFile); $errorOutput->writeLineFormatted('PHPStan crashed in the previous run probably because of excessive memory consumption.'); $errorOutput->writeLineFormatted(sprintf('It consumed around %s of memory.', $memoryLimitFileContents)); $errorOutput->writeLineFormatted(''); diff --git a/src/DependencyInjection/NeonAdapter.php b/src/DependencyInjection/NeonAdapter.php index 88773f125f..bf9edad9af 100644 --- a/src/DependencyInjection/NeonAdapter.php +++ b/src/DependencyInjection/NeonAdapter.php @@ -9,6 +9,7 @@ use Nette\Neon\Entity; use Nette\Neon\Neon; use PHPStan\File\FileHelper; +use PHPStan\File\FileReader; class NeonAdapter implements Adapter { @@ -26,10 +27,8 @@ class NeonAdapter implements Adapter */ public function load(string $file): array { - $contents = file_get_contents($file); - if ($contents === false) { - throw new \PHPStan\ShouldNotHappenException(); - } + $contents = FileReader::read($file); + return $this->process((array) Neon::decode($contents), '', $file); } diff --git a/src/File/CouldNotReadFileException.php b/src/File/CouldNotReadFileException.php new file mode 100644 index 0000000000..d2b40fef15 --- /dev/null +++ b/src/File/CouldNotReadFileException.php @@ -0,0 +1,13 @@ +parseString($contents); + return $this->parseString(FileReader::read($file)); } /** diff --git a/src/Reflection/BetterReflection/SourceLocator/ComposerJsonAndInstalledJsonSourceLocatorMaker.php b/src/Reflection/BetterReflection/SourceLocator/ComposerJsonAndInstalledJsonSourceLocatorMaker.php index 3a92b26fae..1da1ad1f4c 100644 --- a/src/Reflection/BetterReflection/SourceLocator/ComposerJsonAndInstalledJsonSourceLocatorMaker.php +++ b/src/Reflection/BetterReflection/SourceLocator/ComposerJsonAndInstalledJsonSourceLocatorMaker.php @@ -3,6 +3,7 @@ namespace PHPStan\Reflection\BetterReflection\SourceLocator; use Nette\Utils\Json; +use PHPStan\File\FileReader; use Roave\BetterReflection\SourceLocator\Type\AggregateSourceLocator; use Roave\BetterReflection\SourceLocator\Type\Composer\Psr\Psr0Mapping; use Roave\BetterReflection\SourceLocator\Type\Composer\Psr\Psr4Mapping; @@ -41,18 +42,10 @@ public function create(string $installationPath): SourceLocator $composerJsonPath = $installationPath . '/composer.json'; $installedJsonPath = $installationPath . '/vendor/composer/installed.json'; - $composerJsonContents = file_get_contents($composerJsonPath); - if ($composerJsonContents === false) { - throw new \PHPStan\ShouldNotHappenException(); - } - + $composerJsonContents = FileReader::read($composerJsonPath); $composer = Json::decode($composerJsonContents, Json::FORCE_ARRAY); - $installedJsonContents = file_get_contents($installedJsonPath); - if ($installedJsonContents === false) { - throw new \PHPStan\ShouldNotHappenException(); - } - + $installedJsonContents = FileReader::read($installedJsonPath); $installed = Json::decode($installedJsonContents, Json::FORCE_ARRAY); $classMapPaths = array_merge( diff --git a/src/Reflection/BetterReflection/SourceLocator/FileNodesFetcher.php b/src/Reflection/BetterReflection/SourceLocator/FileNodesFetcher.php index 0ae7e7e08c..6ae6deacfd 100644 --- a/src/Reflection/BetterReflection/SourceLocator/FileNodesFetcher.php +++ b/src/Reflection/BetterReflection/SourceLocator/FileNodesFetcher.php @@ -4,6 +4,7 @@ use PhpParser\NodeTraverser; use PhpParser\Parser; +use PHPStan\File\FileReader; use Roave\BetterReflection\SourceLocator\Located\LocatedSource; class FileNodesFetcher @@ -29,10 +30,7 @@ public function fetchNodes(string $fileName): FetchedNodesResult $nodeTraverser = new NodeTraverser(); $nodeTraverser->addVisitor($this->cachingVisitor); - $contents = file_get_contents($fileName); - if ($contents === false) { - throw new \PHPStan\ShouldNotHappenException(); - } + $contents = FileReader::read($fileName); /** @var \PhpParser\Node[] $ast */ $ast = $this->phpParser->parse($contents);