Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use roave/better-reflection to guess packages #346

Draft
wants to merge 5 commits into
base: 4.1.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"ext-phar": "*",
"composer-runtime-api": "^2.0.0",
"nikic/php-parser": "^4.13.0",
"roave/better-reflection": "^5.0",
"symfony/console": "^6.0.0",
"webmozart/assert": "^1.9.1",
"webmozart/glob": "^4.4.0"
Expand Down
165 changes: 163 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion infection.json.dist
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"src"
]
},
"timeout": 30,
"timeout": 60,
"logs": {
"text": "php://stderr"
},
Expand Down
8 changes: 7 additions & 1 deletion src/ComposerRequireChecker/Cli/CheckCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use ComposerRequireChecker\DefinedSymbolsLocator\LocateDefinedSymbolsFromComposerRuntimeApi;
use ComposerRequireChecker\DefinedSymbolsLocator\LocateDefinedSymbolsFromExtensions;
use ComposerRequireChecker\DependencyGuesser\DependencyGuesser;
use ComposerRequireChecker\DependencyGuesser\GuessFromInstalledComposerPackages;
use ComposerRequireChecker\DependencyGuesser\GuessFromLoadedExtensions;
use ComposerRequireChecker\Exception\InvalidJson;
use ComposerRequireChecker\Exception\NotReadable;
use ComposerRequireChecker\FileLocator\LocateComposerPackageDirectDependenciesSourceFiles;
Expand Down Expand Up @@ -212,7 +214,11 @@ public function __invoke(string $string): void
$resultsWriter = new CliText($output);
}

$guesser = new DependencyGuesser($options);
$guesser = new DependencyGuesser([
new GuessFromLoadedExtensions($options),
new GuessFromInstalledComposerPackages(dirname($composerJson)),
]);

$resultsWriter->write(
array_map(
static function (string $unknownSymbol) use ($guesser): array {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@

namespace ComposerRequireChecker\DependencyGuesser;

use ComposerRequireChecker\Cli\Options;
use Generator;

class DependencyGuesser
{
/** @var Guesser[] */
/** @var array<Guesser> */
private array $guessers = [];

public function __construct(?Options $options = null)
/**
* @param array<Guesser> $guessers
*/
public function __construct(array $guessers)
{
$this->guessers[] = new GuessFromLoadedExtensions($options);
$this->guessers = $guessers;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

declare(strict_types=1);

namespace ComposerRequireChecker\DependencyGuesser;

use ComposerRequireChecker\JsonLoader;
use Generator;
use Roave\BetterReflection\BetterReflection;
use Roave\BetterReflection\Identifier\Identifier;
use Roave\BetterReflection\Identifier\IdentifierType;
use Roave\BetterReflection\Reflection\ReflectionClass;
use Roave\BetterReflection\Reflection\ReflectionConstant;
use Roave\BetterReflection\Reflection\ReflectionFunction;
use Roave\BetterReflection\Reflector\DefaultReflector;
use Roave\BetterReflection\SourceLocator\Type\Composer\Factory\MakeLocatorForInstalledJson;
use Roave\BetterReflection\SourceLocator\Type\MemoizingSourceLocator;
use Roave\BetterReflection\SourceLocator\Type\SourceLocator;

use function preg_match;
use function preg_quote;
use function sprintf;
use function str_replace;

use const DIRECTORY_SEPARATOR;

final class GuessFromInstalledComposerPackages implements Guesser
{
private SourceLocator $sourceLocator;
private string $pathRegex;

public function __construct(string $installationPath)
{
$this->sourceLocator = new MemoizingSourceLocator((new MakeLocatorForInstalledJson())(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This locator does not include PHP symbols

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just realized that this is potentially also broken in roave/backward-compatibility-check :|

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not quite sure but dont we have for the php internal stuff the other guesser?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly, but BetterReflection can also reflect all of that, heh :D

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say, follow up PR, if wanted.

$installationPath,
(new BetterReflection())->astLocator()
));

$cleanPath = preg_quote(sprintf('%s/vendor', str_replace(DIRECTORY_SEPARATOR, '/', $installationPath)), '@');
$this->pathRegex = sprintf('@^%s/(?:composer/\.\./)?([^/]+/[^/]+)/@', $cleanPath);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mentioned by @DanielBadura in #346 (comment) - hacky as heck, so we may need to check this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about this now quite some time and I didnt came up with another solution :/

}

/**
* @return Generator<string>
*/
public function __invoke(string $symbolName): Generator
{
foreach ($this->locateIdentifier($symbolName) as $reflection) {
$path = $reflection->getFileName();

if ($path === null) {
continue;
}

$matched = preg_match($this->pathRegex, $path, $captures);

if (! $matched) {
continue;
}

yield JsonLoader::getData($captures[0] . 'composer.json')['name'];
}
}

/**
* @return Generator<ReflectionClass|ReflectionFunction|ReflectionConstant>
*/
private function locateIdentifier(string $symbolName): Generator
{
$locatedIndentifiers = [
$this->sourceLocator->locateIdentifier(
new DefaultReflector($this->sourceLocator),
new Identifier($symbolName, new IdentifierType(IdentifierType::IDENTIFIER_CLASS))
),
$this->sourceLocator->locateIdentifier(
new DefaultReflector($this->sourceLocator),
new Identifier($symbolName, new IdentifierType(IdentifierType::IDENTIFIER_FUNCTION))
),
$this->sourceLocator->locateIdentifier(
new DefaultReflector($this->sourceLocator),
new Identifier($symbolName, new IdentifierType(IdentifierType::IDENTIFIER_CONSTANT))
),
];

foreach ($locatedIndentifiers as $locatedIndentifier) {
if (! ($locatedIndentifier instanceof ReflectionFunction || $locatedIndentifier instanceof ReflectionClass || $locatedIndentifier instanceof ReflectionConstant)) {
continue;
}

yield $locatedIndentifier;
}
}
}
12 changes: 7 additions & 5 deletions test/ComposerRequireCheckerTest/Cli/CheckCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,14 @@ public function testSourceFileThatUsesDevDependency(): void
'composer-json' => dirname(__DIR__, 3) . '/composer.json',
'--config-file' => $root->getChild('config.json')->url(),
]);

$this->assertNotEquals(0, $exitCode);
$this->assertMatchesRegularExpression(
'/The following 2 unknown symbols were found.*PHPUnit\\\\Framework\\\\TestCase/s',
$this->commandTester->getDisplay()
);

$display = $this->commandTester->getDisplay();
$this->assertStringContainsString('The following 2 unknown symbols were found', $display);
$this->assertStringContainsString('org\bovigo\vfs\vfsStream', $display);
$this->assertStringContainsString('mikey179/vfsstream', $display);
$this->assertStringContainsString('PHPUnit\Framework\TestCase', $display);
$this->assertStringContainsString('phpunit/phpunit', $display);
}

public function testNoUnknownSymbolsFound(): void
Expand Down
Loading