Skip to content

Commit

Permalink
Deprecated returning ReflectionClass from ClassLocator::locateClass() (
Browse files Browse the repository at this point in the history
…#18)

Closes #16
  • Loading branch information
vudaltsov authored Mar 9, 2024
1 parent e3bc2d8 commit 16b32fc
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Implemented `getType()`, `getReturnType()`, `getTentativeReturnType()`, `hasTentativeReturnType()`, `hasReturnType()`.
- Deprecated `FileResource::changeDetector()`.
- Introduced new parameter `TyphoonReflector::build($fallbackToNativeReflection = true)`.
- Deprecated `NativeReflectionFileLocator`.
- Deprecated `NativeReflectionLocator`.
- Deprecated returning `ReflectionClass` from `ClassLocator::locateClass()`.
1 change: 1 addition & 0 deletions ClassLocator/NativeReflectionFileLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

/**
* @api
* @deprecated use TyphoonReflector::build($fallbackToNativeReflection) instead
*/
final class NativeReflectionFileLocator implements ClassLocator
{
Expand Down
1 change: 1 addition & 0 deletions ClassLocator/NativeReflectionLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/**
* @api
* @deprecated use TyphoonReflector::build($fallbackToNativeReflection) instead
*/
final class NativeReflectionLocator implements ClassLocator
{
Expand Down
60 changes: 50 additions & 10 deletions TyphoonReflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
use Typhoon\Reflection\Cache\InMemoryCache;
use Typhoon\Reflection\ClassLocator\ClassLocators;
use Typhoon\Reflection\ClassLocator\ComposerClassLocator;
use Typhoon\Reflection\ClassLocator\NativeReflectionFileLocator;
use Typhoon\Reflection\ClassLocator\NativeReflectionLocator;
use Typhoon\Reflection\ClassLocator\PhpStormStubsClassLocator;
use Typhoon\Reflection\ClassReflection\ClassReflector;
use Typhoon\Reflection\Exception\ClassDoesNotExist;
Expand All @@ -36,13 +34,15 @@ private function __construct(
private readonly NativeReflector $nativeReflector,
private readonly ClassLocator $classLocator,
private readonly MetadataStorage $metadataStorage,
private readonly bool $fallbackToNativeReflection,
) {}

public static function build(
?ClassLocator $classLocator = null,
CacheInterface $cache = new InMemoryCache(),
TagPrioritizer $tagPrioritizer = new PrefixBasedTagPrioritizer(),
?PhpParser $phpParser = null,
bool $fallbackToNativeReflection = true,
): self {
return new self(
phpParserReflector: new PhpParserReflector(
Expand All @@ -52,6 +52,7 @@ public static function build(
nativeReflector: new NativeReflector(),
classLocator: $classLocator ?? self::defaultClassLocator(),
metadataStorage: new MetadataStorage($cache),
fallbackToNativeReflection: $fallbackToNativeReflection,
);
}

Expand All @@ -67,9 +68,6 @@ public static function defaultClassLocator(): ClassLocator
$classLocators[] = new ComposerClassLocator();
}

$classLocators[] = new NativeReflectionFileLocator();
$classLocators[] = new NativeReflectionLocator();

return new ClassLocators($classLocators);
}

Expand Down Expand Up @@ -139,20 +137,62 @@ private function reflectClassMetadata(string $name): ClassMetadata
return $metadata;
}

$location = $this->classLocator->locateClass($name)
?? throw new ClassDoesNotExist($name);
$resource = $this->locateClass($name);

if ($location instanceof \ReflectionClass) {
$metadata = $this->nativeReflector->reflectClass($location);
if ($resource instanceof \ReflectionClass) {
$metadata = $this->nativeReflector->reflectClass($resource);
$this->metadataStorage->save($metadata);

return $metadata;
}

$this->phpParserReflector->reflectFile($location, new WeakClassExistenceChecker($this), $this->metadataStorage);
$this->phpParserReflector->reflectFile($resource, new WeakClassExistenceChecker($this), $this->metadataStorage);
$metadata = $this->metadataStorage->get(ClassMetadata::class, $name);
$this->metadataStorage->commit();

return $metadata ?? throw new ClassDoesNotExist($name);
}

/**
* @param non-empty-string $name
*/
private function locateClass(string $name): FileResource|\ReflectionClass
{
$resource = $this->classLocator->locateClass($name);

if ($resource instanceof FileResource) {
return $resource;
}

if ($resource instanceof \ReflectionClass) {
trigger_deprecation(
'typhoon/reflection',
'0.3.1',
'Returning %s from %s is deprecated, use %s::build($fallbackToNativeReflection) instead.',
\ReflectionClass::class,
ClassLocator::class,
self::class,
);

return $resource;
}

if (!$this->fallbackToNativeReflection) {
throw new ClassDoesNotExist($name);
}

try {
$reflectionClass = new \ReflectionClass($name);
} catch (\ReflectionException) {
throw new ClassDoesNotExist($name);
}

$file = $reflectionClass->getFileName();

if ($file !== false) {
return new FileResource($file, $reflectionClass->getExtensionName());
}

return $reflectionClass;
}
}

0 comments on commit 16b32fc

Please sign in to comment.