Skip to content

Commit

Permalink
Update dependencies and fix all phpstan errors
Browse files Browse the repository at this point in the history
  • Loading branch information
navarr committed Dec 21, 2023
1 parent 028add4 commit 08034e6
Show file tree
Hide file tree
Showing 13 changed files with 95 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ TRAEFIK_DOMAIN=depends.test
TRAEFIK_SUBDOMAIN=app

WARDEN_PHP=1
PHP_VERSION=8.0
PHP_VERSION=8.2
PHP_XDEBUG_3=1

COMPOSER_VERSION=2
Expand Down
11 changes: 7 additions & 4 deletions .github/workflows/commit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.0
php-version: 8.3
coverage: xdebug
tools: composer:v2

Expand All @@ -32,8 +32,11 @@ jobs:
phpstan:
name: Static Analysis Check
runs-on: ubuntu-latest
strategy:
matrix:
php: [8.0, 8.1, 8.2, 8.3]
container:
image: davidalger/php:8.0
image: davidalger/php:${{ matrix.php }}
steps:
- name: Checkout Repository
uses: actions/checkout@v2
Expand All @@ -48,7 +51,7 @@ jobs:
name: Code Style (PSR-12)
runs-on: ubuntu-latest
container:
image: davidalger/php:8.0
image: davidalger/php:8.3
steps:
- name: Checkout Repository
uses: actions/checkout@v2
Expand All @@ -67,7 +70,7 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.0
php-version: 8.3
coverage: xdebug
tools: composer:v2

Expand Down
9 changes: 7 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"symfony/console": "^5",
"nikic/php-parser": "^4",
"navarr/attribute-dependency": "^1.0.1",
"php-di/php-di": "^6"
"php-di/php-di": "^7"
},
"suggest": {
"ext-fileinfo": "Use MIME types for PHP file detection",
Expand All @@ -23,7 +23,7 @@
"roave/security-advisories": "dev-master",
"phpstan/phpstan": "^1",
"phpunit/phpunit": "^9.5",
"infection/infection": "^0.23.0",
"infection/infection": "^0.27.0",
"squizlabs/php_codesniffer": "^3.6",
"jetbrains/phpstorm-attributes": "^1.0"
},
Expand Down Expand Up @@ -56,5 +56,10 @@
".gitattributes",
".env"
]
},
"config": {
"allow-plugins": {
"infection/extension-installer": true
}
}
}
2 changes: 1 addition & 1 deletion src/Controller/CliApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class CliApplication

public static function execute(): int
{
$application = new Application('DepAnno', static::VERSION);
$application = new Application('DepAnno', self::VERSION);

Check warning on line 21 in src/Controller/CliApplication.php

View check run for this annotation

Codecov / codecov/patch

src/Controller/CliApplication.php#L21

Added line #L21 was not covered by tests
$application->add(new WhyBlockCommandController());
return $application->run();
}
Expand Down
24 changes: 16 additions & 8 deletions src/Controller/Composer/ComposerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use Navarr\Depends\ScopeDeterminer\ComposerScopeDeterminer;
use Navarr\Depends\ScopeDeterminer\ScopeDeterminerInterface;
use Psr\Container\ContainerInterface;
use RuntimeException;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand Down Expand Up @@ -128,13 +129,13 @@ protected function execute(
}

$outputFormat = strtolower($outputFormat);
if (!in_array($outputFormat, static::ACCEPTABLE_FORMATS)) {
if (!in_array($outputFormat, self::ACCEPTABLE_FORMATS)) {

Check warning on line 132 in src/Controller/Composer/ComposerCommand.php

View check run for this annotation

Codecov / codecov/patch

src/Controller/Composer/ComposerCommand.php#L132

Added line #L132 was not covered by tests
$outputFormat = 'text';
}

if ($input->getOption(static::ALL_DEPS)) {
if ($input->getOption(self::ALL_DEPS)) {

Check warning on line 136 in src/Controller/Composer/ComposerCommand.php

View check run for this annotation

Codecov / codecov/patch

src/Controller/Composer/ComposerCommand.php#L136

Added line #L136 was not covered by tests
$composerScope = ComposerScopeDeterminer::SCOPE_ALL_DEPENDENCIES;
} elseif ($input->getOption(static::ROOT_DEPS)) {
} elseif ($input->getOption(self::ROOT_DEPS)) {

Check warning on line 138 in src/Controller/Composer/ComposerCommand.php

View check run for this annotation

Codecov / codecov/patch

src/Controller/Composer/ComposerCommand.php#L138

Added line #L138 was not covered by tests
$composerScope = ComposerScopeDeterminer::SCOPE_ROOT_DEPENDENCIES;
} else {
$composerScope = ComposerScopeDeterminer::SCOPE_PROJECT_ONLY;
Expand All @@ -145,22 +146,29 @@ protected function execute(
[
InputInterface::class => $input,
OutputInterface::class => $output,
IssueHandlerInterface::class => $input->getOption(static::FAIL_ON_ERROR)
IssueHandlerInterface::class => $input->getOption(self::FAIL_ON_ERROR)

Check warning on line 149 in src/Controller/Composer/ComposerCommand.php

View check run for this annotation

Codecov / codecov/patch

src/Controller/Composer/ComposerCommand.php#L149

Added line #L149 was not covered by tests
? FailOnIssueHandler::class
: NotifyOnIssueHandler::class,
Composer::class => $this->getComposer(true),
ParserInterface::class => static function (ContainerInterface $container) use ($input) {
$parsers = [$container->get(AstParser::class)];
if ($input->getOption(static::LEGACY_ANNOTATION)) {
$parsers[] = $container->get(LegacyParser::class);
$parser = $container->get(AstParser::class);
if (!$parser instanceof ParserInterface) {
throw new RuntimeException('AstParser not found');

Check warning on line 156 in src/Controller/Composer/ComposerCommand.php

View check run for this annotation

Codecov / codecov/patch

src/Controller/Composer/ComposerCommand.php#L154-L156

Added lines #L154 - L156 were not covered by tests
}
$parsers = [$parser];
if ($input->getOption(self::LEGACY_ANNOTATION)) {
$legacyParser = $container->get(LegacyParser::class);
if ($legacyParser instanceof ParserInterface) {
$parsers[] = $legacyParser;

Check warning on line 162 in src/Controller/Composer/ComposerCommand.php

View check run for this annotation

Codecov / codecov/patch

src/Controller/Composer/ComposerCommand.php#L158-L162

Added lines #L158 - L162 were not covered by tests
}
}
return new ParserPool($parsers);
},
WriterInterface::class => autowire(StdOutWriter::class),
ComposerScopeDeterminer::class => autowire(ComposerScopeDeterminer::class)
->property('scope', $composerScope),
ScopeDeterminerInterface::class => autowire(ComposerScopeDeterminer::class),
OutputHandlerInterface::class => autowire(static::FORMAT_MAPPER[$outputFormat]),
OutputHandlerInterface::class => autowire(self::FORMAT_MAPPER[$outputFormat]),

Check warning on line 171 in src/Controller/Composer/ComposerCommand.php

View check run for this annotation

Codecov / codecov/patch

src/Controller/Composer/ComposerCommand.php#L171

Added line #L171 was not covered by tests
]
);
$container = $containerBuilder->build();
Expand Down
26 changes: 19 additions & 7 deletions src/Controller/WhyBlockCommandController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use Navarr\Depends\ScopeDeterminer\PhpFileFinder;
use Navarr\Depends\ScopeDeterminer\ScopeDeterminerInterface;
use Psr\Container\ContainerInterface;
use RuntimeException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -120,7 +121,7 @@ protected function execute(
}

$outputFormat = strtolower($outputFormat);
if (!in_array($outputFormat, static::ACCEPTABLE_FORMATS)) {
if (!in_array($outputFormat, self::ACCEPTABLE_FORMATS)) {

Check warning on line 124 in src/Controller/WhyBlockCommandController.php

View check run for this annotation

Codecov / codecov/patch

src/Controller/WhyBlockCommandController.php#L124

Added line #L124 was not covered by tests
$outputFormat = 'text';
}

Expand All @@ -129,24 +130,35 @@ protected function execute(
[
InputInterface::class => $input,
OutputInterface::class => $output,
IssueHandlerInterface::class => $input->getOption(static::FAIL_ON_ERROR)
IssueHandlerInterface::class => $input->getOption(self::FAIL_ON_ERROR)

Check warning on line 133 in src/Controller/WhyBlockCommandController.php

View check run for this annotation

Codecov / codecov/patch

src/Controller/WhyBlockCommandController.php#L133

Added line #L133 was not covered by tests
? FailOnIssueHandler::class
: NotifyOnIssueHandler::class,
ParserInterface::class => static function (ContainerInterface $container) use ($input) {
$parsers = [$container->get(AstParser::class)];
if ($input->getOption(static::LEGACY_ANNOTATION)) {
$parsers[] = $container->get(LegacyParser::class);
$parser = $container->get(AstParser::class);
if (!$parser instanceof ParserInterface) {
throw new RuntimeException('AstParser not found');

Check warning on line 139 in src/Controller/WhyBlockCommandController.php

View check run for this annotation

Codecov / codecov/patch

src/Controller/WhyBlockCommandController.php#L137-L139

Added lines #L137 - L139 were not covered by tests
}
$parsers = [$parser];
if ($input->getOption(self::LEGACY_ANNOTATION)) {
$legacyParser = $container->get(LegacyParser::class);
if ($legacyParser instanceof ParserInterface) {
$parsers[] = $legacyParser;

Check warning on line 145 in src/Controller/WhyBlockCommandController.php

View check run for this annotation

Codecov / codecov/patch

src/Controller/WhyBlockCommandController.php#L141-L145

Added lines #L141 - L145 were not covered by tests
}
}
return new ParserPool($parsers);
},
WriterInterface::class => autowire(StdOutWriter::class),
ScopeDeterminerInterface::class => static function (ContainerInterface $container) use ($directory) {
$phpFileFinder = $container->get(PhpFileFinder::class);
if (!$phpFileFinder instanceof PhpFileFinder) {
throw new RuntimeException('PhpFileFinder not found');

Check warning on line 154 in src/Controller/WhyBlockCommandController.php

View check run for this annotation

Codecov / codecov/patch

src/Controller/WhyBlockCommandController.php#L152-L154

Added lines #L152 - L154 were not covered by tests
}
return new DirectoryScopeDeterminer(
$container->get(PhpFileFinder::class),
$phpFileFinder,

Check warning on line 157 in src/Controller/WhyBlockCommandController.php

View check run for this annotation

Codecov / codecov/patch

src/Controller/WhyBlockCommandController.php#L157

Added line #L157 was not covered by tests
$directory
);
},
OutputHandlerInterface::class => autowire(static::FORMAT_MAPPER[$outputFormat]),
OutputHandlerInterface::class => autowire(self::FORMAT_MAPPER[$outputFormat]),

Check warning on line 161 in src/Controller/WhyBlockCommandController.php

View check run for this annotation

Codecov / codecov/patch

src/Controller/WhyBlockCommandController.php#L161

Added line #L161 was not covered by tests
]
);
$container = $containerBuilder->build();
Expand Down
7 changes: 6 additions & 1 deletion src/Factory/CollectingFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use DI\NotFoundException;
use Navarr\Attribute\Dependency;
use PhpParser\ErrorHandler\Collecting;
use RuntimeException;

#[Dependency('php-di/php-di', '^6', 'Container::make')]
#[Dependency('nikic/php-parser', '^4', 'Existence of Collecting error handler')]
Expand All @@ -32,6 +33,10 @@ public function __construct(Container $container)
*/
public function create(): Collecting
{
return $this->container->make(Collecting::class);
$result = $this->container->make(Collecting::class);
if (!$result instanceof Collecting) {

Check warning on line 37 in src/Factory/CollectingFactory.php

View workflow job for this annotation

GitHub Actions / Infection Check

Escaped Mutant for Mutator "InstanceOf_": --- Original +++ New @@ @@ public function create() : Collecting { $result = $this->container->make(Collecting::class); - if (!$result instanceof Collecting) { + if (!true) { throw new RuntimeException('Container did not return a Collecting'); } return $result; } }
throw new RuntimeException('Container did not return a Collecting');
}

Check warning on line 39 in src/Factory/CollectingFactory.php

View check run for this annotation

Codecov / codecov/patch

src/Factory/CollectingFactory.php#L38-L39

Added lines #L38 - L39 were not covered by tests
return $result;
}
}
7 changes: 6 additions & 1 deletion src/Factory/FindingVisitorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use DI\NotFoundException;
use Navarr\Attribute\Dependency;
use PhpParser\NodeVisitor\FindingVisitor;
use RuntimeException;

#[Dependency('php-di/php-di', '^6', 'Container::make')]
#[Dependency('nikic/php-parser', '^4', 'Existence of FindingVisitor')]
Expand All @@ -33,6 +34,10 @@ public function __construct(Container $container)
*/
public function create(array $args = []): FindingVisitor
{
return $this->container->make(FindingVisitor::class, $args);
$result = $this->container->make(FindingVisitor::class, $args);
if (!$result instanceof FindingVisitor) {

Check warning on line 38 in src/Factory/FindingVisitorFactory.php

View workflow job for this annotation

GitHub Actions / Infection Check

Escaped Mutant for Mutator "InstanceOf_": --- Original +++ New @@ @@ public function create(array $args = []) : FindingVisitor { $result = $this->container->make(FindingVisitor::class, $args); - if (!$result instanceof FindingVisitor) { + if (!true) { throw new RuntimeException('Container did not return a FindingVisitor'); } return $result; } }
throw new RuntimeException('Container did not return a FindingVisitor');
}
return $result;
}
}
7 changes: 6 additions & 1 deletion src/Factory/NodeTraverserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use DI\NotFoundException;
use Navarr\Attribute\Dependency;
use PhpParser\NodeTraverser;
use RuntimeException;

#[Dependency('php-di/php-di', '^6', 'Container::make')]
#[Dependency('nikic/php-parser', '^4', 'Existence of NodeTraverser')]
Expand All @@ -33,6 +34,10 @@ public function __construct(Container $container)
*/
public function create(array $args = []): NodeTraverser
{
return $this->container->make(NodeTraverser::class, $args);
$result = $this->container->make(NodeTraverser::class, $args);
if (!$result instanceof NodeTraverser) {
throw new RuntimeException('Container did not return a NodeTraverser');

Check warning on line 39 in src/Factory/NodeTraverserFactory.php

View check run for this annotation

Codecov / codecov/patch

src/Factory/NodeTraverserFactory.php#L39

Added line #L39 was not covered by tests
}
return $result;
}
}
1 change: 1 addition & 0 deletions src/Parser/AstParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public function parse(

$traverser->traverse($ast);

/** @var Attribute[] $attributes Since we have a filter callback, this will always be an Attribute */
$attributes = $finder->getFoundNodes();

$argIndex = [
Expand Down
12 changes: 6 additions & 6 deletions src/Parser/LegacyParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,23 @@ public function parse(string $contents): array
}

/**
* @param string[][] $matches
* @param array<array<int, array<int, int|string>>> $matches
* @param string $contents
* @return DeclaredDependency[]
*/
private function processMatches(array $matches, string $contents): array
{
$results = [];

$matchCount = count($matches[0]) ?? 0;
$matchCount = count($matches[0]) ?: 0;
for ($match = 0; $match < $matchCount; ++$match) {
$package = strtolower($matches[static::INLINE_MATCH_PACKAGE][$match][0]);
$version = $matches[static::INLINE_MATCH_VERSION][$match][0];
$package = strtolower((string)$matches[self::INLINE_MATCH_PACKAGE][$match][0]);
$version = (string)$matches[self::INLINE_MATCH_VERSION][$match][0] ?: null;

$line = substr_count(mb_substr($contents, 0, (int)$matches[0][$match][1]), "\n") + 1;

$reason = trim($matches[static::INLINE_MATCH_REASON][$match][0]) ?? 'No reason provided';
if (substr($reason, -2) === '*/') {
$reason = trim((string)$matches[self::INLINE_MATCH_REASON][$match][0]) ?: 'No reason provided';
if (str_ends_with($reason, '*/')) {
$reason = trim(substr($reason, 0, -2));
}

Expand Down
28 changes: 17 additions & 11 deletions src/ScopeDeterminer/ComposerScopeDeterminer.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ private function getRequirementsForPackage(Link $package): array
* Retrieve all PHP files out of the directories and files listed in the autoload directive
*
* @param string $basePath Base directory of the package who's autoload we're processing
* @param array<array> $autoload Result of {@see PackageInterface::getAutoload()}
* @param array<string, array<int|string, array<string>|string>> $autoload
* Result of {@see PackageInterface::getAutoload()}
* @param string[] $results Array of file paths to merge with
* @return string[] File paths
*/
Expand All @@ -123,17 +124,22 @@ private function getAllFilesForAutoload(
array $results = []
): array {
foreach ($autoload as $map) {
foreach ($map as $dir) {
$realDir = realpath($basePath . DIRECTORY_SEPARATOR . $dir);
if ($realDir === false) {
continue;
foreach ($map as $dirs) {
if (is_string($dirs)) {
$dirs = [$dirs];

Check warning on line 129 in src/ScopeDeterminer/ComposerScopeDeterminer.php

View check run for this annotation

Codecov / codecov/patch

src/ScopeDeterminer/ComposerScopeDeterminer.php#L127-L129

Added lines #L127 - L129 were not covered by tests
}
if (is_file($realDir)) {
$results[] = $realDir;
continue;
}
if (is_dir($realDir)) {
$results = $this->phpFileFinder->findAll($dir, $results);
foreach ($dirs as $dir) {
$realDir = realpath($basePath . DIRECTORY_SEPARATOR . $dir);
if ($realDir === false) {
continue;

Check warning on line 134 in src/ScopeDeterminer/ComposerScopeDeterminer.php

View check run for this annotation

Codecov / codecov/patch

src/ScopeDeterminer/ComposerScopeDeterminer.php#L131-L134

Added lines #L131 - L134 were not covered by tests
}
if (is_file($realDir)) {
$results[] = $realDir;
continue;

Check warning on line 138 in src/ScopeDeterminer/ComposerScopeDeterminer.php

View check run for this annotation

Codecov / codecov/patch

src/ScopeDeterminer/ComposerScopeDeterminer.php#L136-L138

Added lines #L136 - L138 were not covered by tests
}
if (is_dir($realDir)) {
$results = $this->phpFileFinder->findAll($dir, $results);

Check warning on line 141 in src/ScopeDeterminer/ComposerScopeDeterminer.php

View check run for this annotation

Codecov / codecov/patch

src/ScopeDeterminer/ComposerScopeDeterminer.php#L140-L141

Added lines #L140 - L141 were not covered by tests
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/ScopeDeterminer/PhpFileDeterminer.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ public function isPhp(
// There are so many approaches we could take here, but we're going with this one:

$mimeType = $this->mimeDeterminer->getMimeType($file);
if ($mimeType && in_array($mimeType, static::PHP_MIME_TYPES)) {
if ($mimeType && in_array($mimeType, self::PHP_MIME_TYPES)) {
// Mime type is PHP. That's good enough for me
return true;
}

$parts = explode('.', $file);
if (in_array(end($parts), static::PHP_FILE_EXTENSIONS)) {
if (in_array(end($parts), self::PHP_FILE_EXTENSIONS)) {
// Extension matches list - so it was probably intended to be PHP
return true;
}
Expand Down

0 comments on commit 08034e6

Please sign in to comment.