Skip to content

Commit

Permalink
use latest phpstan version
Browse files Browse the repository at this point in the history
  • Loading branch information
smoench committed Jan 31, 2020
1 parent 8af5afd commit f196fed
Show file tree
Hide file tree
Showing 25 changed files with 109 additions and 42 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ jobs:
run: php7.4 $(which composer) install --no-interaction --no-progress --no-suggest --optimize-autoloader

- name: PHPStan
uses: docker://oskarstark/phpstan-ga:0.11.19.1
uses: docker://oskarstark/phpstan-ga:0.12.8
with:
args: analyse

Expand Down Expand Up @@ -112,7 +112,7 @@ jobs:
run: composer install --no-interaction --no-progress --no-suggest --optimize-autoloader

- name: "Download humbug/box"
run: wget --no-clobber --output-document=./box https://github.com/humbug/box/releases/download/3.8.3/box.phar || true
run: wget --no-clobber --output-document=./box https://github.com/humbug/box/releases/download/3.8.4/box.phar || true

- name: "Make humbug/box executable"
run: chmod +x ./box
Expand Down
2 changes: 2 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ parameters:
inferPrivatePropertyTypeFromConstructor: true
level: max
paths: [src]
ignoreErrors:
- '/Parameter #1 $id of method Fhaculty\Graph\Graph::createVertex() expects int|null, string given./'
9 changes: 5 additions & 4 deletions src/AstRunner/AstMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,17 @@ public function getClassInherits(string $className): iterable
}

/**
* @return AstInherit[]|iterable
* @param \ArrayObject<string, bool>|null $alreadyResolved
*
* @return iterable<AstInherit>
*/
private function resolveDepsRecursive(
AstInherit $inheritDependency,
\ArrayObject $alreadyResolved = null,
\SplStack $path = null
): iterable {
if (null === $alreadyResolved) {
$alreadyResolved = new \ArrayObject();
}
$alreadyResolved = $alreadyResolved ?? new \ArrayObject();

if (null === $path) {
$path = new \SplStack();
$path->push($inheritDependency);
Expand Down
6 changes: 5 additions & 1 deletion src/AstRunner/AstParser/AstFileReferenceFileCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ class AstFileReferenceFileCache implements AstFileReferenceCache
/** @var array<string, array> */
private $cache;
private $cacheFile;

/** @var bool */
private $loaded = false;

/** @var array<string, bool> */
private $parsedFiles = [];

public function __construct(string $cacheFile)
Expand Down Expand Up @@ -128,7 +132,7 @@ public function write(): void

$cache = array_filter(
$this->cache,
function ($key): bool {
function (string $key): bool {
return isset($this->parsedFiles[$key]);
},
ARRAY_FILTER_USE_KEY
Expand Down
5 changes: 4 additions & 1 deletion src/AstRunner/AstParser/AstFileReferenceInMemoryCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

class AstFileReferenceInMemoryCache implements AstFileReferenceCache
{
/**
* @var array<string, AstFileReference>
*/
private $cache = [];

public function has(string $filepath): bool
Expand All @@ -26,7 +29,7 @@ public function get(string $filepath): ?AstFileReference

public function set(AstFileReference $fileReference): void
{
$filepath = realpath($fileReference->getFilepath());
$filepath = (string) realpath($fileReference->getFilepath());

$this->cache[$filepath] = $fileReference;
}
Expand Down
5 changes: 3 additions & 2 deletions src/AstRunner/AstParser/NikicPhpParser/NikicPhpParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,12 @@ public function parse($data): AstFileReference
throw new \LogicException('data not supported');
}

if (null !== $fileReference = $this->cache->get($data->getRealPath())) {
$realPath = (string) $data->getRealPath();
if (null !== $fileReference = $this->cache->get($realPath)) {
return $fileReference;
}

$fileReference = new AstFileReference($data->getRealPath());
$fileReference = new AstFileReference($realPath);

$traverser = new NodeTraverser();
$traverser->addVisitor(new NameResolver());
Expand Down
6 changes: 3 additions & 3 deletions src/ClassNameLayerResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@

class ClassNameLayerResolver implements ClassNameLayerResolverInterface
{
protected $configuration;
protected $astMap;
protected $collectorRegistry;
private $configuration;
private $astMap;
private $collectorRegistry;

public function __construct(
Configuration $configuration,
Expand Down
3 changes: 3 additions & 0 deletions src/ClassNameLayerResolverCacheDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ class ClassNameLayerResolverCacheDecorator implements ClassNameLayerResolverInte
{
private $classNameLayerResolver;

/** @var array<string, string[]> */
private $layerNamesByClassCache = [];

/** @var string[] */
private $layerNamesCache = [];

public function __construct(ClassNameLayerResolverInterface $classNameLayerResolver)
Expand Down
3 changes: 3 additions & 0 deletions src/Collector/ClassNameCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public function satisfy(
return 1 === preg_match($this->getPattern($configuration), $astClassReference->getClassName());
}

/**
* @param array<string, string> $configuration
*/
private function getPattern(array $configuration): string
{
if (!isset($configuration['regex'])) {
Expand Down
3 changes: 3 additions & 0 deletions src/Collector/ClassNameRegexCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public function satisfy(
return 1 === preg_match($this->getPattern($configuration), $astClassReference->getClassName());
}

/**
* @param array<string, string> $configuration
*/
private function getPattern(array $configuration): string
{
if (!isset($configuration['regex'])) {
Expand Down
2 changes: 1 addition & 1 deletion src/Collector/CollectorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ interface CollectorInterface
public function getType(): string;

/**
* @param array $configuration List of arguments passed for this collector declaration
* @param array<string, mixed> $configuration List of arguments passed for this collector declaration
*/
public function satisfy(
array $configuration,
Expand Down
3 changes: 3 additions & 0 deletions src/Collector/DirectoryCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public function satisfy(
return $fileReference && 1 === preg_match($this->getPattern($configuration), $fileReference->getFilepath());
}

/**
* @param array<string, string> $configuration
*/
private function getPattern(array $configuration): string
{
if (!isset($configuration['regex'])) {
Expand Down
3 changes: 3 additions & 0 deletions src/Collector/MethodCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public function satisfy(
return false;
}

/**
* @param array<string, string> $configuration
*/
private function getPattern(array $configuration): string
{
if (!isset($configuration['name'])) {
Expand Down
9 changes: 6 additions & 3 deletions src/Configuration/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ class Configuration
private $ruleset;
private $skipViolations;

public static function fromArray(array $arr): self
/**
* @param array<string, mixed> $args
*/
public static function fromArray(array $args): self
{
$options = (new OptionsResolver())->setRequired([
'layers',
Expand All @@ -28,9 +31,9 @@ public static function fromArray(array $arr): self
->addAllowedTypes('exclude_files', ['array', 'null'])
->addAllowedTypes('ruleset', 'array')
->addAllowedTypes('skip_violations', 'array')
->resolve($arr);
->resolve($args);

return new static(
return new self(
array_map(static function ($v): ConfigurationLayer {
return ConfigurationLayer::fromArray($v);
}, $options['layers']),
Expand Down
18 changes: 14 additions & 4 deletions src/Configuration/ConfigurationCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,28 @@

namespace SensioLabs\Deptrac\Configuration;

class ConfigurationCollector
final class ConfigurationCollector
{
private $type;

/** @var array<string, string> */
private $args;

public static function fromArray(array $arr): self
/**
* @param array<string, string> $args
*/
public static function fromArray(array $args): self
{
if (!isset($arr['type'])) {
if (!isset($args['type'])) {
throw new \InvalidArgumentException('Collector needs a type.');
}

return new static($arr['type'], $arr);
return new static($args['type'], $args);
}

/**
* @param array<string, string> $args
*/
private function __construct(string $type, array $args)
{
$this->type = $type;
Expand All @@ -30,6 +37,9 @@ public function getType(): string
return $this->type;
}

/**
* @return array<string, string> $args
*/
public function getArgs(): array
{
return $this->args;
Expand Down
9 changes: 6 additions & 3 deletions src/Configuration/ConfigurationLayer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,22 @@

use Symfony\Component\OptionsResolver\OptionsResolver;

class ConfigurationLayer
final class ConfigurationLayer
{
/** @var ConfigurationCollector[] */
private $collectors;

private $name;

public static function fromArray(array $arr): self
/**
* @param array<string, mixed> $args
*/
public static function fromArray(array $args): self
{
$options = (new OptionsResolver())->setRequired([
'name',
'collectors',
])->resolve($arr);
])->resolve($args);

return new static(
array_map(static function ($v): ConfigurationCollector {
Expand Down
2 changes: 1 addition & 1 deletion src/Configuration/ConfigurationRuleset.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace SensioLabs\Deptrac\Configuration;

class ConfigurationRuleset
final class ConfigurationRuleset
{
/** @var array<string, string[]> */
private $layerMap;
Expand Down
2 changes: 1 addition & 1 deletion src/Configuration/ConfigurationSkippedViolation.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/**
* @author Dmitry Balabka <dmitry.balabka@gmail.com>
*/
class ConfigurationSkippedViolation
final class ConfigurationSkippedViolation
{
/** @var array<string, string[]> */
private $classesDeps;
Expand Down
28 changes: 16 additions & 12 deletions src/OutputFormatter/GraphVizOutputFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

class GraphVizOutputFormatter implements OutputFormatterInterface
{
private static $argument_display = 'display';
private static $argument_dump_image = 'dump-image';
private static $argument_dump_dot = 'dump-dot';
private static $argument_dump_html = 'dump-html';
private const ARGUMENT_DISPLAY = 'display';
private const ARGUMENT_DUMP_IMAGE = 'dump-image';
private const ARGUMENT_DUMP_DOT = 'dump-dot';
private const ARGUMENT_DUMP_HTML = 'dump-html';

public function getName(): string
{
Expand All @@ -38,10 +38,10 @@ public function enabledByDefault(): bool
public function configureOptions(): array
{
return [
OutputFormatterOption::newValueOption(static::$argument_display, 'should try to open graphviz image', true),
OutputFormatterOption::newValueOption(static::$argument_dump_image, 'path to a dumped png file', ''),
OutputFormatterOption::newValueOption(static::$argument_dump_dot, 'path to a dumped dot file', ''),
OutputFormatterOption::newValueOption(static::$argument_dump_html, 'path to a dumped html file', ''),
OutputFormatterOption::newValueOption(static::ARGUMENT_DISPLAY, 'should try to open graphviz image', true),
OutputFormatterOption::newValueOption(static::ARGUMENT_DUMP_IMAGE, 'path to a dumped png file', ''),
OutputFormatterOption::newValueOption(static::ARGUMENT_DUMP_DOT, 'path to a dumped dot file', ''),
OutputFormatterOption::newValueOption(static::ARGUMENT_DUMP_HTML, 'path to a dumped html file', ''),
];
}

Expand Down Expand Up @@ -84,30 +84,32 @@ public function finish(
}
}

$display = $outputFormatterInput->getOption(static::$argument_display);
$display = $outputFormatterInput->getOption(static::ARGUMENT_DISPLAY);
if (true === filter_var($display, FILTER_VALIDATE_BOOLEAN)) {
(new GraphViz())->display($graph);
}

if ($dumpImagePath = $outputFormatterInput->getOption(static::$argument_dump_image)) {
if ($dumpImagePath = $outputFormatterInput->getOption(static::ARGUMENT_DUMP_IMAGE)) {
$imagePath = (new GraphViz())->createImageFile($graph);
rename($imagePath, $dumpImagePath);
$output->writeln('<info>Image dumped to '.realpath($dumpImagePath).'</info>');
}

if ($dumpDotPath = $outputFormatterInput->getOption(static::$argument_dump_dot)) {
if ($dumpDotPath = $outputFormatterInput->getOption(static::ARGUMENT_DUMP_DOT)) {
file_put_contents($dumpDotPath, (new GraphViz())->createScript($graph));
$output->writeln('<info>Script dumped to '.realpath($dumpDotPath).'</info>');
}

if ($dumpHtmlPath = $outputFormatterInput->getOption(static::$argument_dump_html)) {
if ($dumpHtmlPath = $outputFormatterInput->getOption(static::ARGUMENT_DUMP_HTML)) {
file_put_contents($dumpHtmlPath, (new GraphViz())->createImageHtml($graph));
$output->writeln('<info>HTML dumped to '.realpath($dumpHtmlPath).'</info>');
}
}

/**
* @param Violation[] $violations
*
* @return array<string, array<string, int>>
*/
private function calculateViolations(array $violations): array
{
Expand All @@ -129,6 +131,8 @@ private function calculateViolations(array $violations): array

/**
* @param Rule[] $rules
*
* @return array<string, array<string, int>>
*/
private function calculateLayerDependencies(array $rules): array
{
Expand Down
Loading

0 comments on commit f196fed

Please sign in to comment.