Skip to content

Commit

Permalink
phpstan level max
Browse files Browse the repository at this point in the history
  • Loading branch information
smoench committed Aug 23, 2019
1 parent 7a1acb3 commit 6385bd7
Show file tree
Hide file tree
Showing 15 changed files with 187 additions and 37 deletions.
2 changes: 1 addition & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
parameters:
level: 5
level: max
paths: [src]
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ class AstClassReferenceResolver extends NodeVisitorAbstract
/** @var AstClassReference */
private $currentClassReference;

/** @var ClassDependencyResolver[] */
/** @var iterable|ClassDependencyResolver[] */
private $classDependencyResolvers;

/**
* @param ClassDependencyResolver[] $classDependencyResolvers
* @param iterable|ClassDependencyResolver[] $classDependencyResolvers
*/
public function __construct(AstFileReference $fileReference, iterable $classDependencyResolvers)
{
Expand Down
5 changes: 4 additions & 1 deletion src/AstRunner/AstParser/NikicPhpParser/NikicPhpParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,13 @@ class NikicPhpParser implements AstParser
private $cache;

/**
* @var ClassDependencyResolver[]
* @var iterable|ClassDependencyResolver[]
*/
private $classDependencyResolvers;

/**
* @param iterable|ClassDependencyResolver[] $classDependencyResolvers
*/
public function __construct(
FileParser $fileParser,
AstFileReferenceCache $cache,
Expand Down
6 changes: 5 additions & 1 deletion src/ClassNameLayerResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function __construct(
*/
public function getLayersByClassName(string $className): array
{
/** @var array<string, bool> $layers */
$layers = [];

if (!$astClassReference = $this->astMap->getClassReferenceByClassName($className)) {
Expand All @@ -52,7 +53,10 @@ public function getLayersByClassName(string $className): array
}
}

return array_keys($layers);
/** @var string[] $layerNames */
$layerNames = array_keys($layers);

return $layerNames;
}

public function getLayers(): array
Expand Down
16 changes: 16 additions & 0 deletions src/Configuration/Exception/FileCannotBeReadException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace SensioLabs\Deptrac\Configuration\Exception;

final class FileCannotBeReadException extends \RuntimeException
{
public static function fromFilename(string $filename): self
{
return new self(sprintf(
'File "%s" cannot be read.',
$filename
));
}
}
16 changes: 16 additions & 0 deletions src/Configuration/Exception/FileDoesNotExistsException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace SensioLabs\Deptrac\Configuration\Exception;

final class FileDoesNotExistsException extends \RuntimeException
{
public static function fromFilename(string $filename): self
{
return new self(sprintf(
'File "%s" does not exist. Run "deptrac init" to create one.',
$filename
));
}
}
2 changes: 1 addition & 1 deletion src/Configuration/Exception/FileExistsException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

namespace SensioLabs\Deptrac\Configuration\Exception;

final class FileExistsException extends \Exception
final class FileExistsException extends \RuntimeException
{
}
9 changes: 0 additions & 9 deletions src/Configuration/Exception/MissingFileException.php

This file was deleted.

15 changes: 10 additions & 5 deletions src/Configuration/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,31 @@
namespace SensioLabs\Deptrac\Configuration;

use SensioLabs\Deptrac\Configuration\Exception\FileCannotBeParsedAsYamlException;
use SensioLabs\Deptrac\Configuration\Exception\MissingFileException;
use SensioLabs\Deptrac\Configuration\Exception\FileDoesNotExistsException;
use SensioLabs\Deptrac\Configuration\Exception\FileCannotBeReadException;
use SensioLabs\Deptrac\Configuration\Exception\ParsedYamlIsNotAnArrayException;
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Yaml;

class Loader
{
/**
* @throws MissingFileException
* @throws FileDoesNotExistsException
* @throws FileCannotBeParsedAsYamlException
* @throws ParsedYamlIsNotAnArrayException
*/
public function load(string $file): Configuration
{
if (!file_exists($file)) {
throw new MissingFileException();
if (!is_file($file)) {
throw FileDoesNotExistsException::fromFilename($file);
}

if (false === ($content = file_get_contents($file))) {
throw FileCannotBeReadException::fromFilename($file);
}

try {
$data = Yaml::parse(file_get_contents($file));
$data = Yaml::parse($content);
} catch (ParseException $exception) {
throw FileCannotBeParsedAsYamlException::fromFilename($file);
}
Expand Down
18 changes: 6 additions & 12 deletions src/Console/Command/AnalyzeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace SensioLabs\Deptrac\Console\Command;

use SensioLabs\Deptrac\Analyser;
use SensioLabs\Deptrac\Configuration\Exception\MissingFileException;
use SensioLabs\Deptrac\Configuration\Loader as ConfigurationLoader;
use SensioLabs\Deptrac\Console\Command\Exception\SingleDepfileIsRequiredException;
use SensioLabs\Deptrac\OutputFormatterFactory;
use SensioLabs\Deptrac\Subscriber\ConsoleSubscriber;
use SensioLabs\Deptrac\Subscriber\ProgressSubscriber;
Expand Down Expand Up @@ -57,14 +57,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->printBanner($output);
}

try {
$configuration = $this->configurationLoader->load($input->getArgument('depfile'));
} catch (MissingFileException $e) {
$this->printConfigMissingError($output, $input->getArgument('depfile'));

return 1;
$file = $input->getArgument('depfile');
if (!is_string($file)) {
throw SingleDepfileIsRequiredException::fromArgument($file);
}

$configuration = $this->configurationLoader->load($file);

$this->dispatcher->addSubscriber(new ConsoleSubscriber($output));

if (!$input->getOption('no-progress')) {
Expand Down Expand Up @@ -96,11 +95,6 @@ protected function printBanner(OutputInterface $output): void
$output->writeln("\n<comment>deptrac is alpha, not production ready.\nplease help us and report feedback / bugs.</comment>\n");
}

protected function printConfigMissingError(OutputInterface $output, string $file): void
{
$output->writeln(sprintf('depfile "%s" not found, run "deptrac init" to create one.', $file));
}

protected function printCollectViolations(OutputInterface $output): void
{
if ($output->getVerbosity() > OutputInterface::VERBOSITY_NORMAL) {
Expand Down
16 changes: 16 additions & 0 deletions src/Console/Command/Exception/SingleDepfileIsRequiredException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace SensioLabs\Deptrac\Console\Command\Exception;

final class SingleDepfileIsRequiredException extends \RuntimeException
{
public static function fromArgument($argument): self
{
return new self(sprintf(
'Please specify a path to a depfile. Got "%s".',
gettype($argument)
));
}
}
35 changes: 35 additions & 0 deletions tests/Configuration/Exception/FileCannotBeReadExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Tests\SensioLabs\Deptrac\Configuration\Exception;

use PHPUnit\Framework\TestCase;
use SensioLabs\Deptrac\Configuration\Exception\FileCannotBeReadException;

/**
* @covers \SensioLabs\Deptrac\Configuration\Exception\FileCannotBeReadException
*/
final class FileCannotBeReadExceptionTest extends TestCase
{
public function testIsRuntimeException(): void
{
$exception = new FileCannotBeReadException();

self::assertInstanceOf(\RuntimeException::class, $exception);
}

public function testFromFilenameReturnsException(): void
{
$filename = __FILE__;

$exception = FileCannotBeReadException::fromFilename($filename);

$message = sprintf(
'File "%s" cannot be read.',
$filename
);

self::assertSame($message, $exception->getMessage());
}
}
35 changes: 35 additions & 0 deletions tests/Configuration/Exception/FileDoesNotExistsExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Tests\SensioLabs\Deptrac\Configuration\Exception;

use PHPUnit\Framework\TestCase;
use SensioLabs\Deptrac\Configuration\Exception\FileDoesNotExistsException;

/**
* @covers \SensioLabs\Deptrac\Configuration\Exception\FileDoesNotExistsException
*/
final class FileDoesNotExistsExceptionTest extends TestCase
{
public function testIsRuntimeException(): void
{
$exception = new FileDoesNotExistsException();

self::assertInstanceOf(\RuntimeException::class, $exception);
}

public function testFromFilenameReturnsException(): void
{
$filename = __FILE__;

$exception = FileDoesNotExistsException::fromFilename($filename);

$message = sprintf(
'File "%s" does not exist. Run "deptrac init" to create one.',
$filename
);

self::assertSame($message, $exception->getMessage());
}
}
10 changes: 5 additions & 5 deletions tests/Configuration/LoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use PHPUnit\Framework\TestCase;
use SensioLabs\Deptrac\Configuration\Exception\FileCannotBeParsedAsYamlException;
use SensioLabs\Deptrac\Configuration\Exception\MissingFileException;
use SensioLabs\Deptrac\Configuration\Exception\FileDoesNotExistsException;
use SensioLabs\Deptrac\Configuration\Exception\ParsedYamlIsNotAnArrayException;
use SensioLabs\Deptrac\Configuration\Loader;

Expand All @@ -15,13 +15,13 @@
*/
final class LoaderTest extends TestCase
{
public function testLoadThrowsMissingFileExceptionWhenFileDoesNotExist(): void
public function testLoadThrowsFileDoesNotExistsExceptionWhenFileDoesNotExist(): void
{
$file = __DIR__.'/../../examples/non-existent-file.yml';

$loader = new Loader();

self::expectException(MissingFileException::class);
$this->expectException(FileDoesNotExistsException::class);

$loader->load($file);
}
Expand All @@ -32,7 +32,7 @@ public function testLoadThrowsFileCannotBeParsedAsYamlExceptionWhenFileDoesNotCo

$loader = new Loader();

self::expectException(FileCannotBeParsedAsYamlException::class);
$this->expectException(FileCannotBeParsedAsYamlException::class);

$loader->load($file);
}
Expand All @@ -43,7 +43,7 @@ public function testLoadThrowsParsedYamlIsNotAnArrayExceptionWhenFileDoesNotCont

$loader = new Loader();

self::expectException(ParsedYamlIsNotAnArrayException::class);
$this->expectException(ParsedYamlIsNotAnArrayException::class);

$loader->load($file);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Tests\SensioLabs\Deptrac\Console\Command\Exception;

use PHPUnit\Framework\TestCase;
use SensioLabs\Deptrac\Console\Command\Exception\SingleDepfileIsRequiredException;

/**
* @covers \SensioLabs\Deptrac\Console\Command\Exception\SingleDepfileIsRequiredException
*/
final class SingleDepfileIsRequiredExceptionTest extends TestCase
{
public function testIsRuntimeException(): void
{
$exception = new SingleDepfileIsRequiredException();

self::assertInstanceOf(\RuntimeException::class, $exception);
}

public function testFromFilenameReturnsException(): void
{
$argument = [];

$exception = SingleDepfileIsRequiredException::fromArgument($argument);

$message = sprintf(
'Please specify a path to a depfile. Got "%s".',
gettype($argument)
);

self::assertSame($message, $exception->getMessage());
}
}

0 comments on commit 6385bd7

Please sign in to comment.