Skip to content

Commit adab5ba

Browse files
authored
Merge pull request #14 from KaririCode-Framework/develop
ptimize performance and memory usage
2 parents 7e70c17 + 971f07f commit adab5ba

12 files changed

+1952
-226
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@
4444
"phpstan/phpstan": "^1.10",
4545
"phpunit/phpunit": "^11.0",
4646
"squizlabs/php_codesniffer": "^3.9",
47-
"enlightn/security-checker": "^2.0"
47+
"enlightn/security-checker": "^2.0",
48+
"kariricode/validator": "^1.0"
4849
},
4950
"support": {
5051
"issues": "https://github.com/KaririCode-Framework/kariricode-property-inspector/issues",

composer.lock

Lines changed: 121 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/AttributeAnalyzer.php

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,49 +9,73 @@
99

1010
final class AttributeAnalyzer implements AttributeAnalyzerContract
1111
{
12+
private array $cache = [];
13+
1214
public function __construct(private readonly string $attributeClass)
1315
{
1416
}
1517

1618
public function analyzeObject(object $object): array
1719
{
1820
try {
19-
$results = [];
20-
$reflection = new \ReflectionClass($object);
21-
22-
foreach ($reflection->getProperties() as $property) {
23-
$propertyResult = $this->analyzeProperty($object, $property);
24-
if (null !== $propertyResult) {
25-
$results[$property->getName()] = $propertyResult;
26-
}
21+
$className = $object::class;
22+
23+
// Usar cache se disponível
24+
if (!isset($this->cache[$className])) {
25+
$this->cacheObjectMetadata($object);
2726
}
2827

29-
return $results;
28+
return $this->extractValues($object);
3029
} catch (\ReflectionException $e) {
3130
throw new PropertyInspectionException('Failed to analyze object: ' . $e->getMessage(), 0, $e);
3231
} catch (\Error $e) {
3332
throw new PropertyInspectionException('An error occurred during object analysis: ' . $e->getMessage(), 0, $e);
3433
}
3534
}
3635

37-
private function analyzeProperty(object $object, \ReflectionProperty $property): ?array
36+
private function cacheObjectMetadata(object $object): void
3837
{
39-
$attributes = $property->getAttributes($this->attributeClass, \ReflectionAttribute::IS_INSTANCEOF);
40-
if (empty($attributes)) {
41-
return null;
38+
$className = $object::class;
39+
$reflection = new \ReflectionClass($object);
40+
$cachedProperties = [];
41+
42+
foreach ($reflection->getProperties() as $property) {
43+
$attributes = $property->getAttributes($this->attributeClass, \ReflectionAttribute::IS_INSTANCEOF);
44+
45+
if (!empty($attributes)) {
46+
$property->setAccessible(true);
47+
$attributeInstances = array_map(
48+
static fn (\ReflectionAttribute $attr): object => $attr->newInstance(),
49+
$attributes
50+
);
51+
52+
$cachedProperties[$property->getName()] = [
53+
'attributes' => $attributeInstances,
54+
'property' => $property,
55+
];
56+
}
4257
}
4358

44-
$property->setAccessible(true);
45-
$propertyValue = $property->getValue($object);
59+
$this->cache[$className] = $cachedProperties;
60+
}
4661

47-
$attributeInstances = array_map(
48-
static fn (\ReflectionAttribute $attr): object => $attr->newInstance(),
49-
$attributes
50-
);
62+
private function extractValues(object $object): array
63+
{
64+
$results = [];
65+
$className = $object::class;
66+
67+
foreach ($this->cache[$className] as $propertyName => $data) {
68+
$results[$propertyName] = [
69+
'value' => $data['property']->getValue($object),
70+
'attributes' => $data['attributes'],
71+
];
72+
}
5173

52-
return [
53-
'value' => $propertyValue,
54-
'attributes' => $attributeInstances,
55-
];
74+
return $results;
75+
}
76+
77+
public function clearCache(): void
78+
{
79+
$this->cache = [];
5680
}
5781
}

0 commit comments

Comments
 (0)