-
Notifications
You must be signed in to change notification settings - Fork 72
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
base: 4.1.x
Are you sure you want to change the base?
Changes from all commits
8b97812
e8cfe80
876cc15
a092bf6
eee2904
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
"src" | ||
] | ||
}, | ||
"timeout": 30, | ||
"timeout": 60, | ||
"logs": { | ||
"text": "php://stderr" | ||
}, | ||
|
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())( | ||
$installationPath, | ||
(new BetterReflection())->astLocator() | ||
)); | ||
|
||
$cleanPath = preg_quote(sprintf('%s/vendor', str_replace(DIRECTORY_SEPARATOR, '/', $installationPath)), '@'); | ||
$this->pathRegex = sprintf('@^%s/(?:composer/\.\./)?([^/]+/[^/]+)/@', $cleanPath); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
:|There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.