-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Guess dependency of class symbols using installed packages.
Use BetterReflection. Do not work with functions nor constants. Do not support https://getcomposer.org/doc/06-config.md#vendor-dir
- Loading branch information
1 parent
f490adf
commit 61009af
Showing
5 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/ComposerRequireChecker/DependencyGuesser/GuessFromComposerInstalledJson.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?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\ClassReflector; | ||
use Roave\BetterReflection\SourceLocator\Type\Composer\Factory\MakeLocatorForInstalledJson; | ||
use Roave\BetterReflection\SourceLocator\Type\MemoizingSourceLocator; | ||
use Roave\BetterReflection\SourceLocator\Type\SourceLocator; | ||
|
||
final class GuessFromComposerInstalledJson implements GuesserInterface | ||
{ | ||
/** | ||
* @var SourceLocator | ||
*/ | ||
private $sourceLocator; | ||
|
||
/** | ||
* @var ClassReflector | ||
*/ | ||
private $reflector; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $pathRegex; | ||
|
||
public function __construct(string $projectPath) | ||
{ | ||
$this->sourceLocator = new MemoizingSourceLocator( | ||
(new MakeLocatorForInstalledJson())( | ||
$projectPath, | ||
(new BetterReflection())->astLocator() | ||
) | ||
); | ||
|
||
$this->reflector = new ClassReflector($this->sourceLocator); | ||
|
||
// @todo: support https://getcomposer.org/doc/06-config.md#vendor-dir; useless as BetterReflection does not, at this moment. | ||
$cleanPath = preg_quote(str_replace(DIRECTORY_SEPARATOR, '/', $projectPath) . '/' . 'vendor', '@'); | ||
|
||
$this->pathRegex = '@^' . $cleanPath . '/(?:composer/\.\./)?([^/]+/[^/]+)/@'; | ||
} | ||
|
||
public function __invoke(string $symbolName): \Generator | ||
{ | ||
$reflection = $this->sourceLocator->locateIdentifier($this->reflector, new Identifier($symbolName, new IdentifierType(IdentifierType::IDENTIFIER_CLASS))); | ||
|
||
if (!($reflection instanceof ReflectionClass)) { | ||
return; | ||
} | ||
|
||
$path = $reflection->getFileName(); | ||
|
||
if (preg_match($this->pathRegex, $path, $captures)) { | ||
yield $captures[1]; | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
test/ComposerRequireCheckerTest/DependencyGuesser/GuessFromComposerInstalledJsonTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace ComposerRequireCheckerTest\DependencyGuesser; | ||
|
||
use ComposerRequireChecker\DependencyGuesser\GuessFromComposerInstalledJson; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class GuessFromComposerInstalledJsonTest extends TestCase | ||
{ | ||
/** | ||
* @var GuessFromComposerInstalledJson | ||
*/ | ||
private $guesser; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->guesser = new GuessFromComposerInstalledJson(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)(GuessFromComposerInstalledJson::class)); | ||
|
||
self::assertEmpty($result); | ||
} | ||
} |