Skip to content

Commit

Permalink
Improved error message when a file could not be read
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jan 4, 2020
1 parent d4bfc70 commit 148f323
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 30 deletions.
14 changes: 7 additions & 7 deletions src/Command/CommandHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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('');
Expand Down
7 changes: 3 additions & 4 deletions src/DependencyInjection/NeonAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Nette\Neon\Entity;
use Nette\Neon\Neon;
use PHPStan\File\FileHelper;
use PHPStan\File\FileReader;

class NeonAdapter implements Adapter
{
Expand All @@ -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);
}

Expand Down
13 changes: 13 additions & 0 deletions src/File/CouldNotReadFileException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php declare(strict_types = 1);

namespace PHPStan\File;

class CouldNotReadFileException extends \PHPStan\AnalysedCodeException
{

public function __construct(string $fileName)
{
parent::__construct(sprintf('Could not read file: %s', $fileName));
}

}
20 changes: 20 additions & 0 deletions src/File/FileReader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php declare(strict_types = 1);

namespace PHPStan\File;

use function file_get_contents;

class FileReader
{

public static function read(string $fileName): string
{
$contents = @file_get_contents($fileName);
if ($contents === false) {
throw new \PHPStan\File\CouldNotReadFileException($fileName);
}

return $contents;
}

}
7 changes: 2 additions & 5 deletions src/Parser/DirectParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PhpParser\ErrorHandler\Collecting;
use PhpParser\NodeTraverser;
use PHPStan\File\FileReader;

class DirectParser implements Parser
{
Expand All @@ -26,11 +27,7 @@ public function __construct(\PhpParser\Parser $parser, NodeTraverser $traverser)
*/
public function parseFile(string $file): array
{
$contents = file_get_contents($file);
if ($contents === false) {
throw new \PHPStan\ShouldNotHappenException();
}
return $this->parseString($contents);
return $this->parseString(FileReader::read($file));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PhpParser\NodeTraverser;
use PhpParser\Parser;
use PHPStan\File\FileReader;
use Roave\BetterReflection\SourceLocator\Located\LocatedSource;

class FileNodesFetcher
Expand All @@ -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);
Expand Down

0 comments on commit 148f323

Please sign in to comment.