-
Notifications
You must be signed in to change notification settings - Fork 133
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
Using generators to get a list of reflections #1476
base: 6.52.x
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
|
||
namespace Roave\BetterReflection\Reflector; | ||
|
||
use Generator; | ||
use Roave\BetterReflection\Identifier\Identifier; | ||
use Roave\BetterReflection\Identifier\IdentifierType; | ||
use Roave\BetterReflection\Reflection\ReflectionClass; | ||
|
@@ -43,11 +44,11 @@ public function reflectClass(string $identifierName): ReflectionClass | |
/** | ||
* Get all the classes available in the scope specified by the SourceLocator. | ||
* | ||
* @return list<ReflectionClass> | ||
* @return Generator<ReflectionClass> | ||
*/ | ||
public function reflectAllClasses(): iterable | ||
public function reflectAllClasses(): Generator | ||
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. We don't need to change the native type here: |
||
{ | ||
/** @var list<ReflectionClass> $allClasses */ | ||
/** @var Generator<ReflectionClass> $allClasses */ | ||
$allClasses = $this->sourceLocator->locateIdentifiersByType( | ||
$this, | ||
new IdentifierType(IdentifierType::IDENTIFIER_CLASS), | ||
|
@@ -79,11 +80,11 @@ public function reflectFunction(string $identifierName): ReflectionFunction | |
/** | ||
* Get all the functions available in the scope specified by the SourceLocator. | ||
* | ||
* @return list<ReflectionFunction> | ||
* @return Generator<ReflectionFunction> | ||
*/ | ||
public function reflectAllFunctions(): iterable | ||
public function reflectAllFunctions(): Generator | ||
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. Same as comment above: the native type can stay |
||
{ | ||
/** @var list<ReflectionFunction> $allFunctions */ | ||
/** @var Generator<ReflectionFunction> $allFunctions */ | ||
$allFunctions = $this->sourceLocator->locateIdentifiersByType( | ||
$this, | ||
new IdentifierType(IdentifierType::IDENTIFIER_FUNCTION), | ||
|
@@ -115,11 +116,11 @@ public function reflectConstant(string $identifierName): ReflectionConstant | |
/** | ||
* Get all the constants available in the scope specified by the SourceLocator. | ||
* | ||
* @return list<ReflectionConstant> | ||
* @return Generator<ReflectionConstant> | ||
*/ | ||
public function reflectAllConstants(): iterable | ||
public function reflectAllConstants(): Generator | ||
Comment on lines
+119
to
+121
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. Same as comment above: the native type can stay |
||
{ | ||
/** @var list<ReflectionConstant> $allConstants */ | ||
/** @var Generator<ReflectionConstant> $allConstants */ | ||
$allConstants = $this->sourceLocator->locateIdentifiersByType( | ||
$this, | ||
new IdentifierType(IdentifierType::IDENTIFIER_CONSTANT), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
|
||
namespace Roave\BetterReflection\Reflector; | ||
|
||
use Generator; | ||
use Roave\BetterReflection\Reflection\ReflectionClass; | ||
use Roave\BetterReflection\Reflection\ReflectionConstant; | ||
use Roave\BetterReflection\Reflection\ReflectionFunction; | ||
|
@@ -21,9 +22,9 @@ public function reflectClass(string $identifierName): ReflectionClass; | |
/** | ||
* Get all the classes available in the scope specified by the SourceLocator. | ||
* | ||
* @return list<ReflectionClass> | ||
* @return Generator<ReflectionClass> | ||
*/ | ||
public function reflectAllClasses(): iterable; | ||
public function reflectAllClasses(): Generator; | ||
Comment on lines
-24
to
+27
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. This interface should be rolled back, and the |
||
|
||
/** | ||
* Create a ReflectionFunction for the specified $functionName. | ||
|
@@ -35,9 +36,9 @@ public function reflectFunction(string $identifierName): ReflectionFunction; | |
/** | ||
* Get all the functions available in the scope specified by the SourceLocator. | ||
* | ||
* @return list<ReflectionFunction> | ||
* @return Generator<ReflectionFunction> | ||
*/ | ||
public function reflectAllFunctions(): iterable; | ||
public function reflectAllFunctions(): Generator; | ||
|
||
/** | ||
* Create a ReflectionConstant for the specified $constantName. | ||
|
@@ -49,7 +50,7 @@ public function reflectConstant(string $identifierName): ReflectionConstant; | |
/** | ||
* Get all the constants available in the scope specified by the SourceLocator. | ||
* | ||
* @return list<ReflectionConstant> | ||
* @return Generator<ReflectionConstant> | ||
*/ | ||
public function reflectAllConstants(): iterable; | ||
public function reflectAllConstants(): Generator; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
|
||
namespace Roave\BetterReflection\SourceLocator\Type; | ||
|
||
use Generator; | ||
use Roave\BetterReflection\Identifier\Identifier; | ||
use Roave\BetterReflection\Identifier\IdentifierType; | ||
use Roave\BetterReflection\Reflection\Reflection; | ||
|
@@ -54,15 +55,15 @@ public function locateIdentifier(Reflector $reflector, Identifier $identifier): | |
* | ||
* @throws ParseToAstFailure | ||
*/ | ||
final public function locateIdentifiersByType(Reflector $reflector, IdentifierType $identifierType): array | ||
final public function locateIdentifiersByType(Reflector $reflector, IdentifierType $identifierType): Generator | ||
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'd suggest declaring We can |
||
{ | ||
$locatedSource = $this->createLocatedSource(new Identifier(Identifier::WILDCARD, $identifierType)); | ||
|
||
if (! $locatedSource) { | ||
return []; | ||
return; | ||
} | ||
|
||
return $this->astLocator->findReflectionsOfType( | ||
yield from $this->astLocator->findReflectionsOfType( | ||
$reflector, | ||
$locatedSource, | ||
$identifierType, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,14 +4,12 @@ | |
|
||
namespace Roave\BetterReflection\SourceLocator\Type; | ||
|
||
use Generator; | ||
use Roave\BetterReflection\Identifier\Identifier; | ||
use Roave\BetterReflection\Identifier\IdentifierType; | ||
use Roave\BetterReflection\Reflection\Reflection; | ||
use Roave\BetterReflection\Reflector\Reflector; | ||
|
||
use function array_map; | ||
use function array_merge; | ||
|
||
class AggregateSourceLocator implements SourceLocator | ||
{ | ||
/** @param list<SourceLocator> $sourceLocators */ | ||
|
@@ -32,14 +30,10 @@ public function locateIdentifier(Reflector $reflector, Identifier $identifier): | |
return null; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function locateIdentifiersByType(Reflector $reflector, IdentifierType $identifierType): array | ||
public function locateIdentifiersByType(Reflector $reflector, IdentifierType $identifierType): Generator | ||
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. The implementation looks good, but the return type should probably be |
||
{ | ||
return array_merge( | ||
[], | ||
...array_map(static fn (SourceLocator $sourceLocator): array => $sourceLocator->locateIdentifiersByType($reflector, $identifierType), $this->sourceLocators), | ||
); | ||
foreach ($this->sourceLocators as $sourceLocator) { | ||
yield from $sourceLocator->locateIdentifiersByType($reflector, $identifierType); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
|
||
namespace Roave\BetterReflection\SourceLocator\Type; | ||
|
||
use Generator; | ||
use PhpParser\Node; | ||
use PhpParser\Node\Stmt\Class_; | ||
use PhpParser\NodeTraverser; | ||
|
@@ -25,7 +26,6 @@ | |
use Roave\BetterReflection\SourceLocator\Located\AnonymousLocatedSource; | ||
use Roave\BetterReflection\Util\FileHelper; | ||
|
||
use function array_filter; | ||
use function assert; | ||
use function file_get_contents; | ||
use function str_contains; | ||
|
@@ -55,9 +55,14 @@ public function locateIdentifier(Reflector $reflector, Identifier $identifier): | |
* | ||
* @throws ParseToAstFailure | ||
*/ | ||
public function locateIdentifiersByType(Reflector $reflector, IdentifierType $identifierType): array | ||
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. The implementation of this class can be rolled back |
||
public function locateIdentifiersByType(Reflector $reflector, IdentifierType $identifierType): Generator | ||
{ | ||
return array_filter([$this->getReflectionClass($reflector, $identifierType)]); | ||
$reflection = $this->getReflectionClass($reflector, $identifierType); | ||
if (! $reflection) { | ||
return; | ||
} | ||
|
||
yield $reflection; | ||
} | ||
|
||
private function getReflectionClass(Reflector $reflector, IdentifierType $identifierType): ReflectionClass|null | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
namespace Roave\BetterReflection\SourceLocator\Type; | ||
|
||
use Closure; | ||
use Generator; | ||
use PhpParser\Node; | ||
use PhpParser\Node\Stmt\Namespace_; | ||
use PhpParser\NodeTraverser; | ||
|
@@ -26,7 +27,6 @@ | |
use Roave\BetterReflection\SourceLocator\Located\AnonymousLocatedSource; | ||
use Roave\BetterReflection\Util\FileHelper; | ||
|
||
use function array_filter; | ||
use function assert; | ||
use function file_get_contents; | ||
use function str_contains; | ||
|
@@ -56,9 +56,14 @@ public function locateIdentifier(Reflector $reflector, Identifier $identifier): | |
* | ||
* @throws ParseToAstFailure | ||
*/ | ||
public function locateIdentifiersByType(Reflector $reflector, IdentifierType $identifierType): array | ||
public function locateIdentifiersByType(Reflector $reflector, IdentifierType $identifierType): Generator | ||
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. The implementation of this class can be rolled back |
||
{ | ||
return array_filter([$this->getReflectionFunction($reflector, $identifierType)]); | ||
$reflection = $this->getReflectionFunction($reflector, $identifierType); | ||
if (! $reflection) { | ||
return; | ||
} | ||
|
||
yield $reflection; | ||
} | ||
|
||
private function getReflectionFunction(Reflector $reflector, IdentifierType $identifierType): ReflectionFunction|null | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
|
||
namespace Roave\BetterReflection\SourceLocator\Type\Composer; | ||
|
||
use Generator; | ||
use Roave\BetterReflection\Identifier\Identifier; | ||
use Roave\BetterReflection\Identifier\IdentifierType; | ||
use Roave\BetterReflection\Reflection\Reflection; | ||
|
@@ -54,11 +55,11 @@ public function locateIdentifier(Reflector $reflector, Identifier $identifier): | |
/** | ||
* Find all identifiers of a type | ||
* | ||
* @return list<Reflection> | ||
* @return Generator<Reflection> | ||
*/ | ||
public function locateIdentifiersByType(Reflector $reflector, IdentifierType $identifierType): array | ||
public function locateIdentifiersByType(Reflector $reflector, IdentifierType $identifierType): Generator | ||
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. The implementation of this class can be rolled back, except for the return type being |
||
{ | ||
return (new DirectoriesSourceLocator( | ||
yield from (new DirectoriesSourceLocator( | ||
$this->mapping->directories(), | ||
$this->astLocator, | ||
))->locateIdentifiersByType($reflector, $identifierType); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
|
||
namespace Roave\BetterReflection\SourceLocator\Type; | ||
|
||
use Generator; | ||
use RecursiveDirectoryIterator; | ||
use RecursiveIteratorIterator; | ||
use Roave\BetterReflection\Identifier\Identifier; | ||
|
@@ -55,11 +56,8 @@ public function locateIdentifier(Reflector $reflector, Identifier $identifier): | |
return $this->aggregateSourceLocator->locateIdentifier($reflector, $identifier); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function locateIdentifiersByType(Reflector $reflector, IdentifierType $identifierType): array | ||
public function locateIdentifiersByType(Reflector $reflector, IdentifierType $identifierType): Generator | ||
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. The implementation of this class can be rolled back, except for the return type being |
||
{ | ||
return $this->aggregateSourceLocator->locateIdentifiersByType($reflector, $identifierType); | ||
yield from $this->aggregateSourceLocator->locateIdentifiersByType($reflector, $identifierType); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
|
||
namespace Roave\BetterReflection\SourceLocator\Type; | ||
|
||
use Generator; | ||
use Iterator; | ||
use Roave\BetterReflection\Identifier\Identifier; | ||
use Roave\BetterReflection\Identifier\IdentifierType; | ||
|
@@ -83,8 +84,8 @@ public function locateIdentifier(Reflector $reflector, Identifier $identifier): | |
* | ||
* @throws InvalidFileLocation | ||
*/ | ||
public function locateIdentifiersByType(Reflector $reflector, IdentifierType $identifierType): array | ||
public function locateIdentifiersByType(Reflector $reflector, IdentifierType $identifierType): Generator | ||
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. The implementation of this class can be rolled back, except for the return type being |
||
{ | ||
return $this->getAggregatedSourceLocator()->locateIdentifiersByType($reflector, $identifierType); | ||
yield from $this->getAggregatedSourceLocator()->locateIdentifiersByType($reflector, $identifierType); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
|
||
namespace Roave\BetterReflection\SourceLocator\Type; | ||
|
||
use Generator; | ||
use Roave\BetterReflection\Identifier\Identifier; | ||
use Roave\BetterReflection\Identifier\IdentifierType; | ||
use Roave\BetterReflection\Reflection\Reflection; | ||
|
@@ -37,17 +38,23 @@ public function locateIdentifier(Reflector $reflector, Identifier $identifier): | |
= $this->wrappedSourceLocator->locateIdentifier($reflector, $identifier); | ||
} | ||
|
||
/** @return list<Reflection> */ | ||
public function locateIdentifiersByType(Reflector $reflector, IdentifierType $identifierType): array | ||
/** @return Generator<Reflection> */ | ||
public function locateIdentifiersByType(Reflector $reflector, IdentifierType $identifierType): Generator | ||
{ | ||
$cacheKey = sprintf('%s_%s', $this->reflectorCacheKey($reflector), $this->identifierTypeToCacheKey($identifierType)); | ||
|
||
if (array_key_exists($cacheKey, $this->cacheByIdentifierTypeKeyAndOid)) { | ||
return $this->cacheByIdentifierTypeKeyAndOid[$cacheKey]; | ||
yield from $this->cacheByIdentifierTypeKeyAndOid[$cacheKey]; | ||
|
||
return; | ||
} | ||
|
||
$items = []; | ||
foreach ($this->wrappedSourceLocator->locateIdentifiersByType($reflector, $identifierType) as $item) { | ||
yield $items[] = $item; | ||
Comment on lines
+52
to
+54
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. The original implementation caches items before returning them: the new implementation caches items after returning them. I'm unsure about approach here, but it is a subtle change that can lead to uncached results if the consumer of this iterator crashes |
||
} | ||
|
||
return $this->cacheByIdentifierTypeKeyAndOid[$cacheKey] | ||
= $this->wrappedSourceLocator->locateIdentifiersByType($reflector, $identifierType); | ||
$this->cacheByIdentifierTypeKeyAndOid[$cacheKey] = $items; | ||
} | ||
|
||
private function reflectorCacheKey(Reflector $reflector): string | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
|
||
namespace Roave\BetterReflection\SourceLocator\Type; | ||
|
||
use Generator; | ||
use Roave\BetterReflection\Identifier\Identifier; | ||
use Roave\BetterReflection\Identifier\IdentifierType; | ||
use Roave\BetterReflection\Reflection\Reflection; | ||
|
@@ -26,7 +27,7 @@ public function locateIdentifier(Reflector $reflector, Identifier $identifier): | |
/** | ||
* Find all identifiers of a type | ||
* | ||
* @return list<Reflection> | ||
* @return Generator<Reflection> | ||
*/ | ||
public function locateIdentifiersByType(Reflector $reflector, IdentifierType $identifierType): array; | ||
public function locateIdentifiersByType(Reflector $reflector, IdentifierType $identifierType): Generator; | ||
Comment on lines
+30
to
+32
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. We should move to |
||
} |
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.
We should roll back to
iterable<int, ReflectionClass>
hereThere 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 change is the actual BC break, since
list<T>
is obviously an extremely small type