refactor(redis, discovery, cache): enhance validation, error handling, and class filtering
- Remove redundant fallback for `RedisConfig` key prefix to enforce explicit configuration. - Refine `ClassExtractor` with class name validation to exclude invalid identifiers and handle creation errors. - Improve `AttributeCache` by validating class existence before reflection, preventing unnecessary exceptions and caching empty results on failure.
This commit is contained in:
@@ -47,12 +47,27 @@ final class AttributeCache implements \App\Framework\Reflection\Contracts\Reflec
|
||||
{
|
||||
$key = "{$className->getFullyQualified()}::attributes::" . ($attributeClass ?? 'all');
|
||||
if (! isset($this->attributeCache[$key])) {
|
||||
/** @var class-string $className */
|
||||
$className = $className->getFullyQualified();
|
||||
$class = new \ReflectionClass($className);
|
||||
$this->attributeCache[$key] = $attributeClass
|
||||
? $class->getAttributes($attributeClass)
|
||||
: $class->getAttributes();
|
||||
/** @var class-string $classNameString */
|
||||
$classNameString = $className->getFullyQualified();
|
||||
|
||||
try {
|
||||
// Check if class exists before trying to reflect it
|
||||
// This prevents ReflectionException for invalid class names (e.g., property names mistaken as classes)
|
||||
if (! class_exists($classNameString) && ! interface_exists($classNameString) && ! trait_exists($classNameString)) {
|
||||
// Cache empty result to avoid repeated checks
|
||||
$this->attributeCache[$key] = [];
|
||||
return [];
|
||||
}
|
||||
|
||||
$class = new \ReflectionClass($classNameString);
|
||||
$this->attributeCache[$key] = $attributeClass
|
||||
? $class->getAttributes($attributeClass)
|
||||
: $class->getAttributes();
|
||||
} catch (\ReflectionException $e) {
|
||||
// Class doesn't exist or can't be reflected - cache empty result
|
||||
$this->attributeCache[$key] = [];
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
return $this->attributeCache[$key];
|
||||
|
||||
Reference in New Issue
Block a user