Skip to content

Commit aaaa39c

Browse files
committed
Apply Coderabbitai Nitpick comments.
1 parent d98d249 commit aaaa39c

2 files changed

Lines changed: 93 additions & 3 deletions

File tree

src/Reflector.php

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,21 @@ final class Reflector
4747
*/
4848
private static array $reflectionClassCache = [];
4949

50+
/**
51+
* Returns the current number of cached reflection classes.
52+
*
53+
* Usage example:
54+
* ```php
55+
* $cacheSize = \PHPForge\Helper\Reflector::cacheSize();
56+
* ```
57+
*
58+
* @return int Number of cached reflection classes.
59+
*/
60+
public static function cacheSize(): int
61+
{
62+
return count(self::$reflectionClassCache);
63+
}
64+
5065
/**
5166
* Returns class attributes, optionally filtered by attribute class.
5267
*
@@ -74,6 +89,19 @@ public static function classAttributes(object|string $class, string|null $attrib
7489
return $reflectionClass->getAttributes($attribute, $flags);
7590
}
7691

92+
/**
93+
* Clears the internal reflection class cache.
94+
*
95+
* Usage example:
96+
* ```php
97+
* \PHPForge\Helper\Reflector::clearCache();
98+
* ```
99+
*/
100+
public static function clearCache(): void
101+
{
102+
self::$reflectionClassCache = [];
103+
}
104+
77105
/**
78106
* Returns the first matching instantiated property attribute.
79107
*
@@ -135,11 +163,11 @@ public static function properties(object|string $class, int|null $filter = null)
135163
{
136164
$reflectionClass = self::reflectionClass($class);
137165

138-
if ($filter === null) {
139-
return $reflectionClass->getProperties();
166+
if ($filter !== null) {
167+
return $reflectionClass->getProperties($filter);
140168
}
141169

142-
return $reflectionClass->getProperties($filter);
170+
return $reflectionClass->getProperties();
143171
}
144172

145173
/**

tests/ReflectorTest.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,27 @@
3131
*/
3232
final class ReflectorTest extends TestCase
3333
{
34+
public function testCacheSizeReturnsNumberOfCachedReflectionClasses(): void
35+
{
36+
Reflector::clearCache();
37+
38+
Reflector::shortName(ReflectionFixture::class);
39+
40+
self::assertSame(
41+
1,
42+
Reflector::cacheSize(),
43+
'Should return one cached class after first reflection lookup.',
44+
);
45+
46+
Reflector::shortName(stdClass::class);
47+
48+
self::assertSame(
49+
2,
50+
Reflector::cacheSize(),
51+
'Should increase cache size when reflecting a different class.',
52+
);
53+
}
54+
3455
public function testClassAttributesReturnsAllClassAttributes(): void
3556
{
3657
$attributes = Reflector::classAttributes(ReflectionFixture::class);
@@ -70,6 +91,26 @@ public function testClassAttributesWithFilterReturnsMatchingAttributesOnly(): vo
7091
);
7192
}
7293

94+
public function testClearCacheEmptiesReflectionClassCache(): void
95+
{
96+
Reflector::clearCache();
97+
Reflector::shortName(ReflectionFixture::class);
98+
99+
self::assertGreaterThan(
100+
0,
101+
Reflector::cacheSize(),
102+
'Should populate cache after reflection usage.',
103+
);
104+
105+
Reflector::clearCache();
106+
107+
self::assertSame(
108+
0,
109+
Reflector::cacheSize(),
110+
'Should clear all cached reflection classes.',
111+
);
112+
}
113+
73114
public function testFirstPropertyAttributeReturnsFirstMatchingAttributeInstance(): void
74115
{
75116
$attribute = Reflector::firstPropertyAttribute(ReflectionFixture::class, 'name', Label::class);
@@ -105,6 +146,27 @@ public function testHasPropertyReturnsExpectedResult(): void
105146
);
106147
}
107148

149+
public function testPropertiesReturnsAllPropertiesWhenFilterIsNull(): void
150+
{
151+
$allProperties = Reflector::properties(ReflectionFixture::class);
152+
$allPropertyNames = [];
153+
154+
foreach ($allProperties as $property) {
155+
$allPropertyNames[] = $property->getName();
156+
}
157+
158+
self::assertContains(
159+
'name',
160+
$allPropertyNames,
161+
'Should include public properties when no filter is provided.',
162+
);
163+
self::assertContains(
164+
'hidden',
165+
$allPropertyNames,
166+
'Should include private properties when no filter is provided.',
167+
);
168+
}
169+
108170
public function testPropertiesReturnsFilteredListWhenVisibilityFilterIsProvided(): void
109171
{
110172
$publicProperties = Reflector::properties(ReflectionFixture::class, ReflectionProperty::IS_PUBLIC);

0 commit comments

Comments
 (0)