Skip to content

Commit 16b32fc

Browse files
authored
Deprecated returning ReflectionClass from ClassLocator::locateClass() (#18)
Closes #16
1 parent e3bc2d8 commit 16b32fc

File tree

4 files changed

+56
-10
lines changed

4 files changed

+56
-10
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
- Implemented `getType()`, `getReturnType()`, `getTentativeReturnType()`, `hasTentativeReturnType()`, `hasReturnType()`.
1111
- Deprecated `FileResource::changeDetector()`.
12+
- Introduced new parameter `TyphoonReflector::build($fallbackToNativeReflection = true)`.
13+
- Deprecated `NativeReflectionFileLocator`.
14+
- Deprecated `NativeReflectionLocator`.
15+
- Deprecated returning `ReflectionClass` from `ClassLocator::locateClass()`.

ClassLocator/NativeReflectionFileLocator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
/**
1111
* @api
12+
* @deprecated use TyphoonReflector::build($fallbackToNativeReflection) instead
1213
*/
1314
final class NativeReflectionFileLocator implements ClassLocator
1415
{

ClassLocator/NativeReflectionLocator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
/**
1010
* @api
11+
* @deprecated use TyphoonReflector::build($fallbackToNativeReflection) instead
1112
*/
1213
final class NativeReflectionLocator implements ClassLocator
1314
{

TyphoonReflector.php

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
use Typhoon\Reflection\Cache\InMemoryCache;
1111
use Typhoon\Reflection\ClassLocator\ClassLocators;
1212
use Typhoon\Reflection\ClassLocator\ComposerClassLocator;
13-
use Typhoon\Reflection\ClassLocator\NativeReflectionFileLocator;
14-
use Typhoon\Reflection\ClassLocator\NativeReflectionLocator;
1513
use Typhoon\Reflection\ClassLocator\PhpStormStubsClassLocator;
1614
use Typhoon\Reflection\ClassReflection\ClassReflector;
1715
use Typhoon\Reflection\Exception\ClassDoesNotExist;
@@ -36,13 +34,15 @@ private function __construct(
3634
private readonly NativeReflector $nativeReflector,
3735
private readonly ClassLocator $classLocator,
3836
private readonly MetadataStorage $metadataStorage,
37+
private readonly bool $fallbackToNativeReflection,
3938
) {}
4039

4140
public static function build(
4241
?ClassLocator $classLocator = null,
4342
CacheInterface $cache = new InMemoryCache(),
4443
TagPrioritizer $tagPrioritizer = new PrefixBasedTagPrioritizer(),
4544
?PhpParser $phpParser = null,
45+
bool $fallbackToNativeReflection = true,
4646
): self {
4747
return new self(
4848
phpParserReflector: new PhpParserReflector(
@@ -52,6 +52,7 @@ public static function build(
5252
nativeReflector: new NativeReflector(),
5353
classLocator: $classLocator ?? self::defaultClassLocator(),
5454
metadataStorage: new MetadataStorage($cache),
55+
fallbackToNativeReflection: $fallbackToNativeReflection,
5556
);
5657
}
5758

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

70-
$classLocators[] = new NativeReflectionFileLocator();
71-
$classLocators[] = new NativeReflectionLocator();
72-
7371
return new ClassLocators($classLocators);
7472
}
7573

@@ -139,20 +137,62 @@ private function reflectClassMetadata(string $name): ClassMetadata
139137
return $metadata;
140138
}
141139

142-
$location = $this->classLocator->locateClass($name)
143-
?? throw new ClassDoesNotExist($name);
140+
$resource = $this->locateClass($name);
144141

145-
if ($location instanceof \ReflectionClass) {
146-
$metadata = $this->nativeReflector->reflectClass($location);
142+
if ($resource instanceof \ReflectionClass) {
143+
$metadata = $this->nativeReflector->reflectClass($resource);
147144
$this->metadataStorage->save($metadata);
148145

149146
return $metadata;
150147
}
151148

152-
$this->phpParserReflector->reflectFile($location, new WeakClassExistenceChecker($this), $this->metadataStorage);
149+
$this->phpParserReflector->reflectFile($resource, new WeakClassExistenceChecker($this), $this->metadataStorage);
153150
$metadata = $this->metadataStorage->get(ClassMetadata::class, $name);
154151
$this->metadataStorage->commit();
155152

156153
return $metadata ?? throw new ClassDoesNotExist($name);
157154
}
155+
156+
/**
157+
* @param non-empty-string $name
158+
*/
159+
private function locateClass(string $name): FileResource|\ReflectionClass
160+
{
161+
$resource = $this->classLocator->locateClass($name);
162+
163+
if ($resource instanceof FileResource) {
164+
return $resource;
165+
}
166+
167+
if ($resource instanceof \ReflectionClass) {
168+
trigger_deprecation(
169+
'typhoon/reflection',
170+
'0.3.1',
171+
'Returning %s from %s is deprecated, use %s::build($fallbackToNativeReflection) instead.',
172+
\ReflectionClass::class,
173+
ClassLocator::class,
174+
self::class,
175+
);
176+
177+
return $resource;
178+
}
179+
180+
if (!$this->fallbackToNativeReflection) {
181+
throw new ClassDoesNotExist($name);
182+
}
183+
184+
try {
185+
$reflectionClass = new \ReflectionClass($name);
186+
} catch (\ReflectionException) {
187+
throw new ClassDoesNotExist($name);
188+
}
189+
190+
$file = $reflectionClass->getFileName();
191+
192+
if ($file !== false) {
193+
return new FileResource($file, $reflectionClass->getExtensionName());
194+
}
195+
196+
return $reflectionClass;
197+
}
158198
}

0 commit comments

Comments
 (0)