Skip to content

Commit

Permalink
Merge pull request #860 from kukulich/cleanup
Browse files Browse the repository at this point in the history
Small code improvements
  • Loading branch information
Ocramius committed Nov 16, 2021
2 parents a43cb08 + eae18f5 commit b0ceb7c
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 13 deletions.
15 changes: 9 additions & 6 deletions src/SourceLocator/SourceStubber/ReflectionSourceStubber.php
Expand Up @@ -76,21 +76,20 @@ public function __construct()
*/
public function generateClassStub(string $className): ?StubData
{
/** phpcs:disable SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly.ReferenceViaFullyQualifiedName */
$isEnum = function_exists('enum_exists') && \enum_exists($className, false);
/** phpcs:enable */

if (
! (
class_exists($className, false)
|| interface_exists($className, false)
|| trait_exists($className, false)
|| $isEnum
)
) {
return null;
}

/** phpcs:disable SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly.ReferenceViaFullyQualifiedName */
$isEnum = function_exists('enum_exists') && \enum_exists($className, false);
/** phpcs:enable */

$classReflection = $isEnum ? new CoreReflectionEnum($className) : new CoreReflectionClass($className);
$classNode = $this->createClass($classReflection);

Expand Down Expand Up @@ -649,7 +648,11 @@ private function generateStubInNamespace(Node $node, string $namespaceName): str

private function generateStub(Node $node): string
{
return "<?php\n\n" . $this->prettyPrinter->prettyPrint([$node]) . ($node instanceof Node\Expr\FuncCall ? ';' : '') . "\n";
return sprintf(
"<?php\n\n%s%s\n",
$this->prettyPrinter->prettyPrint([$node]),
($node instanceof Node\Expr\FuncCall ? ';' : ''),
);
}

private function createStubData(string $stub, ?string $extensionName): StubData
Expand Down
4 changes: 0 additions & 4 deletions src/SourceLocator/Type/AutoloadSourceLocator.php
Expand Up @@ -158,11 +158,7 @@ private function locateClassByName(string $className): ?array
class_exists($className, false)
|| interface_exists($className, false)
|| trait_exists($className, false)
/** phpcs:disable SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly.ReferenceViaFullyQualifiedName */
|| (function_exists('enum_exists') && \enum_exists($className, false))
/** phpcs:enable */
) {
/** @psalm-suppress ArgumentTypeCoercion */
$classReflection = new ReflectionClass($className);

$filename = $classReflection->getFileName();
Expand Down
2 changes: 1 addition & 1 deletion src/SourceLocator/Type/MemoizingSourceLocator.php
Expand Up @@ -27,7 +27,7 @@ public function __construct(private SourceLocator $wrappedSourceLocator)

public function locateIdentifier(Reflector $reflector, Identifier $identifier): ?Reflection
{
$cacheKey = $this->reflectorCacheKey($reflector) . '_' . $this->identifierToCacheKey($identifier);
$cacheKey = sprintf('%s_%s', $this->reflectorCacheKey($reflector), $this->identifierToCacheKey($identifier));

if (array_key_exists($cacheKey, $this->cacheByIdentifierKeyAndOid)) {
return $this->cacheByIdentifierKeyAndOid[$cacheKey];
Expand Down
Expand Up @@ -557,7 +557,14 @@ public function testNoStubForFunctionThatDoesNotExist(): void

public function testStubForConstantThatExists(): void
{
self::assertInstanceOf(StubData::class, $this->sourceStubber->generateConstantStub('PHP_VERSION_ID'));
$stubData = $this->sourceStubber->generateConstantStub('PHP_VERSION_ID');

self::assertInstanceOf(StubData::class, $stubData);
self::assertStringMatchesFormat(
"%Adefine('PHP_VERSION_ID',%w%d);",
$stubData->getStub(),
);
self::assertSame('Core', $stubData->getExtensionName());
}

public function testNoStubForConstantThatDoesNotExist(): void
Expand Down
Expand Up @@ -510,7 +510,7 @@ public function testCanStubConstant(): void

self::assertNotNull($stubData);
self::assertStringMatchesFormat(
"%Adefine('E_ALL',%A",
"%Adefine('E_ALL',%w%d);",
$stubData->getStub(),
);
self::assertSame('Core', $stubData->getExtensionName());
Expand Down
21 changes: 21 additions & 0 deletions test/unit/SourceLocator/Type/EvaledCodeSourceLocatorTest.php
Expand Up @@ -18,6 +18,7 @@
use Roave\BetterReflection\SourceLocator\Type\EvaledCodeSourceLocator;
use Roave\BetterReflectionTest\BetterReflectionSingleton;

use function sprintf;
use function uniqid;

/**
Expand Down Expand Up @@ -99,6 +100,26 @@ public function testCanReflectEvaledTrait(): void
self::assertStringMatchesFormat('%Atrait%A' . $traitName . '%A', $reflection->getLocatedSource()->getSource());
}

/**
* @requires PHP >= 8.1
*/
public function testCanReflectEvaledEnum(): void
{
$enumName = uniqid('foo', false);

eval(sprintf('enum %s {case ENUM_CASE;}', $enumName));

$locator = new EvaledCodeSourceLocator($this->astLocator, $this->sourceStubber);

$reflection = $locator->locateIdentifier(
$this->getMockReflector(),
new Identifier($enumName, new IdentifierType(IdentifierType::IDENTIFIER_CLASS)),
);

self::assertInstanceOf(EvaledLocatedSource::class, $reflection->getLocatedSource());
self::assertStringMatchesFormat('%Aenum%A' . $enumName . '%A', $reflection->getLocatedSource()->getSource());
}

public function testCanReflectEvaledLocatedSourceClass(): void
{
$reflector = new DefaultReflector(new EvaledCodeSourceLocator($this->astLocator, $this->sourceStubber));
Expand Down

0 comments on commit b0ceb7c

Please sign in to comment.