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 2 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.

3 changes: 3 additions & 0 deletions src/ComposerRequireChecker/Cli/CheckCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use ComposerRequireChecker\DefinedSymbolsLocator\LocateDefinedSymbolsFromComposerRuntimeApi;
use ComposerRequireChecker\DefinedSymbolsLocator\LocateDefinedSymbolsFromExtensions;
use ComposerRequireChecker\DependencyGuesser\DependencyGuesser;
use ComposerRequireChecker\DependencyGuesser\GuessFromInstalledComposerPackages;
use ComposerRequireChecker\Exception\InvalidJson;
use ComposerRequireChecker\Exception\NotReadable;
use ComposerRequireChecker\FileLocator\LocateComposerPackageDirectDependenciesSourceFiles;
Expand Down Expand Up @@ -213,6 +214,8 @@ public function __invoke(string $string): void
}

$guesser = new DependencyGuesser($options);
$guesser->addGuesser(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 @@ -26,4 +26,9 @@ public function __invoke(string $symbolName): Generator
yield from $guesser($symbolName);
}
}

public function addGuesser(Guesser $guesser): void
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's avoid introducing mutability here: instead, let's use a new constructor argument?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Optional or not: the BC break from using roave/better-reflection by default will probably be notable anyway, so we can potentially break BC here, and release in 5.0.0

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Could you explain why adding the dependency would lead to a BC break?

Yeah, would go for the argument too.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Potentially because scanning behavior changes => new symbols detected (or missed) => CI failure for consumers that upgrade.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe I misunderstand: But we are not detecting here more / new symbols, we just give here a hint which dependency should be installed to get rid of the warning, no?

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 removed the mutability but still i dont get why we already break BC with using better-reflection. Maybe you could explain it more?

{
$this->guessers[] = $guesser;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace ComposerRequireChecker\DependencyGuesser;

use Roave\BetterReflection\BetterReflection;
use Roave\BetterReflection\Identifier\Identifier;
use Roave\BetterReflection\Identifier\IdentifierType;
use Roave\BetterReflection\Reflection\ReflectionClass;
use Roave\BetterReflection\Reflector\DefaultReflector;
use Roave\BetterReflection\SourceLocator\Type\Composer\Factory\MakeLocatorForInstalledJson;
use Generator;
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
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure for this regex tbh. Is there a way to get the name from the composer.json maybe?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Getting the name from composer.json would be best, since the path will break anyway, due to people doing ugly things like changing the location of the vendor dir 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is there already a way to get the name from the composer.json?

Copy link
Collaborator

Choose a reason for hiding this comment

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

each composer.json must have a "name" property, in order to be valid.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So i reused now the regex but to get the composer.json and load it via the JsonLoader already present in this lib.

Im not sure if this is now really better since we are still using a regex but also have now more I/O which impacts performance. But since this is a CI tool i think the performance aspect could be overlooked.

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
{
$reflection = $this->sourceLocator->locateIdentifier(
new DefaultReflector($this->sourceLocator),
new Identifier($symbolName, new IdentifierType(IdentifierType::IDENTIFIER_CLASS))
DanielBadura marked this conversation as resolved.
Show resolved Hide resolved
);

if (!($reflection instanceof ReflectionClass)) {
return;
}

$path = $reflection->getFileName();

if (preg_match($this->pathRegex, $path, $captures)) {
yield $captures[1];
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace ComposerRequireCheckerTest\DependencyGuesser;

use ComposerRequireChecker\DependencyGuesser\GuessFromInstalledComposerPackages;
use PHPUnit\Framework\TestCase;

final class GuessFromInstalledComposerPackagesTest extends TestCase
{
private GuessFromInstalledComposerPackages $guesser;

protected function setUp(): void
{
$this->guesser = new GuessFromInstalledComposerPackages(dirname(__DIR__, 3));
}

public function testGuessVendorClass(): void
{
$result = ($this->guesser)(TestCase::class);

self::assertNotEmpty($result);
self::assertContains('phpunit/phpunit', $result);
}

public function testDoNotGuessVendorFunction(): void
{
$result = iterator_to_array(($this->guesser)('DeepCopy\deep_copy'));

self::assertEmpty($result);
}


public function testDoNotGuessClassFromProject(): void
{
$result = iterator_to_array(($this->guesser)(GuessFromInstalledComposerPackages::class));

self::assertEmpty($result);
}
}