Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
"phpstan/phpstan": "^1.10",
"phpunit/phpunit": "^11.0",
"squizlabs/php_codesniffer": "^3.9",
"enlightn/security-checker": "^2.0"
"enlightn/security-checker": "^2.0",
"kariricode/validator": "^1.0"
},
"support": {
"issues": "https://github.com/KaririCode-Framework/kariricode-property-inspector/issues",
Expand Down
122 changes: 121 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 47 additions & 23 deletions src/AttributeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,49 +9,73 @@

final class AttributeAnalyzer implements AttributeAnalyzerContract
{
private array $cache = [];

public function __construct(private readonly string $attributeClass)
{
}

public function analyzeObject(object $object): array
{
try {
$results = [];
$reflection = new \ReflectionClass($object);

foreach ($reflection->getProperties() as $property) {
$propertyResult = $this->analyzeProperty($object, $property);
if (null !== $propertyResult) {
$results[$property->getName()] = $propertyResult;
}
$className = $object::class;

// Usar cache se disponível
if (!isset($this->cache[$className])) {
$this->cacheObjectMetadata($object);
}

return $results;
return $this->extractValues($object);
} catch (\ReflectionException $e) {
throw new PropertyInspectionException('Failed to analyze object: ' . $e->getMessage(), 0, $e);
} catch (\Error $e) {
throw new PropertyInspectionException('An error occurred during object analysis: ' . $e->getMessage(), 0, $e);
}
}

private function analyzeProperty(object $object, \ReflectionProperty $property): ?array
private function cacheObjectMetadata(object $object): void
{
$attributes = $property->getAttributes($this->attributeClass, \ReflectionAttribute::IS_INSTANCEOF);
if (empty($attributes)) {
return null;
$className = $object::class;
$reflection = new \ReflectionClass($object);
$cachedProperties = [];

foreach ($reflection->getProperties() as $property) {
$attributes = $property->getAttributes($this->attributeClass, \ReflectionAttribute::IS_INSTANCEOF);

if (!empty($attributes)) {
$property->setAccessible(true);
$attributeInstances = array_map(
static fn (\ReflectionAttribute $attr): object => $attr->newInstance(),
$attributes
);

$cachedProperties[$property->getName()] = [
'attributes' => $attributeInstances,
'property' => $property,
];
}
}

$property->setAccessible(true);
$propertyValue = $property->getValue($object);
$this->cache[$className] = $cachedProperties;
}

$attributeInstances = array_map(
static fn (\ReflectionAttribute $attr): object => $attr->newInstance(),
$attributes
);
private function extractValues(object $object): array
{
$results = [];
$className = $object::class;

foreach ($this->cache[$className] as $propertyName => $data) {
$results[$propertyName] = [
'value' => $data['property']->getValue($object),
'attributes' => $data['attributes'],
];
}

return [
'value' => $propertyValue,
'attributes' => $attributeInstances,
];
return $results;
}

public function clearCache(): void
{
$this->cache = [];
}
}
Loading
Loading