Skip to content

Commit 148f323

Browse files
committed
Improved error message when a file could not be read
1 parent d4bfc70 commit 148f323

7 files changed

Lines changed: 50 additions & 30 deletions

File tree

src/Command/CommandHelper.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use PHPStan\DependencyInjection\NeonAdapter;
1717
use PHPStan\File\FileFinder;
1818
use PHPStan\File\FileHelper;
19+
use PHPStan\File\FileReader;
1920
use Symfony\Component\Console\Input\InputInterface;
2021
use Symfony\Component\Console\Output\ConsoleOutputInterface;
2122
use Symfony\Component\Console\Output\OutputInterface;
@@ -109,9 +110,11 @@ public static function begin(
109110
throw new \PHPStan\Command\InceptionNotSuccessfulException();
110111
}
111112

112-
$pathsString = file_get_contents($pathsFile);
113-
if ($pathsString === false) {
114-
throw new \PHPStan\ShouldNotHappenException();
113+
try {
114+
$pathsString = FileReader::read($pathsFile);
115+
} catch (\PHPStan\File\CouldNotReadFileException $e) {
116+
$errorOutput->writeLineFormatted($e->getMessage());
117+
throw new \PHPStan\Command\InceptionNotSuccessfulException();
115118
}
116119

117120
$paths = array_values(array_filter(explode("\n", $pathsString), static function (string $path): bool {
@@ -225,10 +228,7 @@ public static function begin(
225228

226229
$memoryLimitFile = $container->getParameter('memoryLimitFile');
227230
if (file_exists($memoryLimitFile)) {
228-
$memoryLimitFileContents = file_get_contents($memoryLimitFile);
229-
if ($memoryLimitFileContents === false) {
230-
throw new \PHPStan\ShouldNotHappenException();
231-
}
231+
$memoryLimitFileContents = FileReader::read($memoryLimitFile);
232232
$errorOutput->writeLineFormatted('PHPStan crashed in the previous run probably because of excessive memory consumption.');
233233
$errorOutput->writeLineFormatted(sprintf('It consumed around %s of memory.', $memoryLimitFileContents));
234234
$errorOutput->writeLineFormatted('');

src/DependencyInjection/NeonAdapter.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Nette\Neon\Entity;
1010
use Nette\Neon\Neon;
1111
use PHPStan\File\FileHelper;
12+
use PHPStan\File\FileReader;
1213

1314
class NeonAdapter implements Adapter
1415
{
@@ -26,10 +27,8 @@ class NeonAdapter implements Adapter
2627
*/
2728
public function load(string $file): array
2829
{
29-
$contents = file_get_contents($file);
30-
if ($contents === false) {
31-
throw new \PHPStan\ShouldNotHappenException();
32-
}
30+
$contents = FileReader::read($file);
31+
3332
return $this->process((array) Neon::decode($contents), '', $file);
3433
}
3534

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\File;
4+
5+
class CouldNotReadFileException extends \PHPStan\AnalysedCodeException
6+
{
7+
8+
public function __construct(string $fileName)
9+
{
10+
parent::__construct(sprintf('Could not read file: %s', $fileName));
11+
}
12+
13+
}

src/File/FileReader.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\File;
4+
5+
use function file_get_contents;
6+
7+
class FileReader
8+
{
9+
10+
public static function read(string $fileName): string
11+
{
12+
$contents = @file_get_contents($fileName);
13+
if ($contents === false) {
14+
throw new \PHPStan\File\CouldNotReadFileException($fileName);
15+
}
16+
17+
return $contents;
18+
}
19+
20+
}

src/Parser/DirectParser.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PhpParser\ErrorHandler\Collecting;
66
use PhpParser\NodeTraverser;
7+
use PHPStan\File\FileReader;
78

89
class DirectParser implements Parser
910
{
@@ -26,11 +27,7 @@ public function __construct(\PhpParser\Parser $parser, NodeTraverser $traverser)
2627
*/
2728
public function parseFile(string $file): array
2829
{
29-
$contents = file_get_contents($file);
30-
if ($contents === false) {
31-
throw new \PHPStan\ShouldNotHappenException();
32-
}
33-
return $this->parseString($contents);
30+
return $this->parseString(FileReader::read($file));
3431
}
3532

3633
/**

src/Reflection/BetterReflection/SourceLocator/ComposerJsonAndInstalledJsonSourceLocatorMaker.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace PHPStan\Reflection\BetterReflection\SourceLocator;
44

55
use Nette\Utils\Json;
6+
use PHPStan\File\FileReader;
67
use Roave\BetterReflection\SourceLocator\Type\AggregateSourceLocator;
78
use Roave\BetterReflection\SourceLocator\Type\Composer\Psr\Psr0Mapping;
89
use Roave\BetterReflection\SourceLocator\Type\Composer\Psr\Psr4Mapping;
@@ -41,18 +42,10 @@ public function create(string $installationPath): SourceLocator
4142
$composerJsonPath = $installationPath . '/composer.json';
4243
$installedJsonPath = $installationPath . '/vendor/composer/installed.json';
4344

44-
$composerJsonContents = file_get_contents($composerJsonPath);
45-
if ($composerJsonContents === false) {
46-
throw new \PHPStan\ShouldNotHappenException();
47-
}
48-
45+
$composerJsonContents = FileReader::read($composerJsonPath);
4946
$composer = Json::decode($composerJsonContents, Json::FORCE_ARRAY);
5047

51-
$installedJsonContents = file_get_contents($installedJsonPath);
52-
if ($installedJsonContents === false) {
53-
throw new \PHPStan\ShouldNotHappenException();
54-
}
55-
48+
$installedJsonContents = FileReader::read($installedJsonPath);
5649
$installed = Json::decode($installedJsonContents, Json::FORCE_ARRAY);
5750

5851
$classMapPaths = array_merge(

src/Reflection/BetterReflection/SourceLocator/FileNodesFetcher.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PhpParser\NodeTraverser;
66
use PhpParser\Parser;
7+
use PHPStan\File\FileReader;
78
use Roave\BetterReflection\SourceLocator\Located\LocatedSource;
89

910
class FileNodesFetcher
@@ -29,10 +30,7 @@ public function fetchNodes(string $fileName): FetchedNodesResult
2930
$nodeTraverser = new NodeTraverser();
3031
$nodeTraverser->addVisitor($this->cachingVisitor);
3132

32-
$contents = file_get_contents($fileName);
33-
if ($contents === false) {
34-
throw new \PHPStan\ShouldNotHappenException();
35-
}
33+
$contents = FileReader::read($fileName);
3634

3735
/** @var \PhpParser\Node[] $ast */
3836
$ast = $this->phpParser->parse($contents);

0 commit comments

Comments
 (0)