Skip to content

Commit af0dfec

Browse files
committed
refactor(exception): improve error handling with KaririCode Exception pattern
- Add standardized error codes in range 2500-2599 (Runtime Errors) - Implement static factory methods for exception creation - Remove direct exception instantiation - Add proper error slugs for better error identification - Update tests to verify exception handling: * Reflection analysis errors (2501) * Reflection inspection errors (2502) * General analysis errors (2503) * General inspection errors (2504) * Critical inspection errors (2505) - Add comprehensive tests for cache clearing functionality - Fix test assertions to match actual error handling behavior
1 parent acf8cd0 commit af0dfec

File tree

8 files changed

+314
-103
lines changed

8 files changed

+314
-103
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
],
2727
"require": {
2828
"php": "^8.3",
29-
"kariricode/contract": "^2.7"
29+
"kariricode/contract": "^2.7",
30+
"kariricode/exception": "^1.2"
3031
},
3132
"autoload": {
3233
"psr-4": {

composer.lock

Lines changed: 58 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: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,15 @@ public function analyzeObject(object $object): array
2020
try {
2121
$className = $object::class;
2222

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

2827
return $this->extractValues($object);
2928
} catch (\ReflectionException $e) {
30-
throw new PropertyInspectionException('Failed to analyze object: ' . $e->getMessage(), 0, $e);
29+
throw PropertyInspectionException::failedToAnalyzeObjectReflection($e);
3130
} catch (\Error $e) {
32-
throw new PropertyInspectionException('An error occurred during object analysis: ' . $e->getMessage(), 0, $e);
31+
throw PropertyInspectionException::failedToAnalyzeObjectError($e);
3332
}
3433
}
3534

src/Exception/PropertyInspectionException.php

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,63 @@
44

55
namespace KaririCode\PropertyInspector\Exception;
66

7-
class PropertyInspectionException extends \RuntimeException
7+
use KaririCode\Exception\AbstractException;
8+
9+
final class PropertyInspectionException extends AbstractException
810
{
11+
private const CODE_REFLECTION_ANALYSIS_ERROR = 2501;
12+
private const CODE_REFLECTION_INSPECTION_ERROR = 2502;
13+
private const CODE_GENERAL_ANALYSIS_ERROR = 2503;
14+
private const CODE_GENERAL_INSPECTION_ERROR = 2504;
15+
private const CODE_CRITICAL_INSPECTION_ERROR = 2505;
16+
17+
public static function failedToAnalyzeObjectReflection(\ReflectionException $e): self
18+
{
19+
return self::createException(
20+
self::CODE_REFLECTION_ANALYSIS_ERROR,
21+
'REFLECTION_ANALYSIS_ERROR',
22+
"Failed to analyze object using reflection: {$e->getMessage()}",
23+
$e
24+
);
25+
}
26+
27+
public static function failedToAnalyzeObjectError(\Error $e): self
28+
{
29+
return self::createException(
30+
self::CODE_GENERAL_ANALYSIS_ERROR,
31+
'GENERAL_ANALYSIS_ERROR',
32+
"An error occurred during object analysis: {$e->getMessage()}",
33+
$e
34+
);
35+
}
36+
37+
public static function failedToInspectObjectReflection(\ReflectionException $e): self
38+
{
39+
return self::createException(
40+
self::CODE_REFLECTION_INSPECTION_ERROR,
41+
'REFLECTION_INSPECTION_ERROR',
42+
"Failed to inspect object using reflection: {$e->getMessage()}",
43+
$e
44+
);
45+
}
46+
47+
public static function failedToInspectObjectException(\Exception $e): self
48+
{
49+
return self::createException(
50+
self::CODE_GENERAL_INSPECTION_ERROR,
51+
'GENERAL_INSPECTION_ERROR',
52+
"An exception occurred during object inspection: {$e->getMessage()}",
53+
$e
54+
);
55+
}
56+
57+
public static function failedToInspectObjectError(\Error $e): self
58+
{
59+
return self::createException(
60+
self::CODE_CRITICAL_INSPECTION_ERROR,
61+
'CRITICAL_INSPECTION_ERROR',
62+
"A critical error occurred during object inspection: {$e->getMessage()}",
63+
$e
64+
);
65+
}
966
}

src/Utility/PropertyInspector.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ public function inspect(object $object, PropertyAttributeHandler $handler): Prop
2727

2828
return $handler;
2929
} catch (\ReflectionException $e) {
30-
throw new PropertyInspectionException('Failed to analyze object: ' . $e->getMessage(), 0, $e);
30+
throw PropertyInspectionException::failedToInspectObjectReflection($e);
3131
} catch (\Exception $e) {
32-
throw new PropertyInspectionException('An exception occurred during object analysis: ' . $e->getMessage(), 0, $e);
32+
throw PropertyInspectionException::failedToInspectObjectException($e);
3333
} catch (\Error $e) {
34-
throw new PropertyInspectionException('An error occurred during object analysis: ' . $e->getMessage(), 0, $e);
34+
throw PropertyInspectionException::failedToInspectObjectError($e);
3535
}
3636
}
3737
}

tests/AttributeAnalyzerTest.php

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace KaririCode\PropertyInspector\Tests;
66

7-
use Attribute;
87
use KaririCode\PropertyInspector\AttributeAnalyzer;
98
use KaririCode\PropertyInspector\Contract\AttributeAnalyzer as AttributeAnalyzerContract;
109
use KaririCode\PropertyInspector\Exception\PropertyInspectionException;
@@ -63,60 +62,63 @@ public function testAnalyzeObjectWithPrivateProperty(): void
6362

6463
public function testReflectionExceptionThrownDuringAnalyzeObject(): void
6564
{
66-
// Define a fake attribute class for testing
6765
$attributeClass = 'FakeAttributeClass';
68-
69-
// Create the AttributeAnalyzer with the fake attribute class
7066
$analyzer = new AttributeAnalyzer($attributeClass);
71-
72-
// Simulate an object that will trigger a ReflectionException
7367
$object = new class {
7468
private $inaccessibleProperty;
7569

7670
public function __construct()
7771
{
78-
// Simulating an inaccessible property that will cause ReflectionException
7972
$this->inaccessibleProperty = null;
8073
}
8174
};
8275

83-
// We expect a PropertyInspectionException due to ReflectionException
8476
$this->expectException(PropertyInspectionException::class);
77+
$this->expectExceptionCode(2503); // CODE_GENERAL_ANALYSIS_ERROR
8578
$this->expectExceptionMessage('An error occurred during object analysis: Class "FakeAttributeClass" not found');
8679

87-
// Execute the analyzeObject method, which should trigger the exception
8880
$analyzer->analyzeObject($object);
8981
}
9082

9183
public function testErrorThrownDuringAnalyzeProperty(): void
9284
{
93-
// Define a fake attribute class for testing
9485
$attributeClass = 'FakeAttributeClass';
95-
96-
// Create the AttributeAnalyzer with the fake attribute class
9786
$analyzer = new AttributeAnalyzer($attributeClass);
98-
99-
// Simulate an object that will trigger an Error during property analysis
10087
$object = new class {
10188
private $errorProperty;
10289

10390
public function __construct()
10491
{
105-
// Simulating an error in the property that will cause an Error during reflection
10692
$this->errorProperty = null;
10793
}
10894
};
10995

110-
// Mock Reflection to throw an error during attribute analysis
111-
$reflectionPropertyMock = $this->createMock(\ReflectionProperty::class);
112-
$reflectionPropertyMock->method('getAttributes')
113-
->willThrowException(new \Error('Simulated Error'));
114-
115-
// We expect a PropertyInspectionException due to the Error
11696
$this->expectException(PropertyInspectionException::class);
97+
$this->expectExceptionCode(2503); // CODE_GENERAL_ANALYSIS_ERROR
11798
$this->expectExceptionMessage('An error occurred during object analysis: Class "FakeAttributeClass" not found');
11899

119-
// Execute the analyzeObject method, which should trigger the exception
120100
$analyzer->analyzeObject($object);
121101
}
102+
103+
public function testClearCacheInvocation(): void
104+
{
105+
$object = new class {
106+
#[TestAttribute]
107+
public string $testProperty = 'test value';
108+
};
109+
110+
$this->analyzer->analyzeObject($object);
111+
112+
$reflection = new \ReflectionClass(AttributeAnalyzer::class);
113+
$cacheProperty = $reflection->getProperty('cache');
114+
$cacheProperty->setAccessible(true);
115+
116+
$this->assertNotEmpty($cacheProperty->getValue($this->analyzer));
117+
$this->analyzer->clearCache();
118+
119+
$this->assertEmpty($cacheProperty->getValue($this->analyzer));
120+
121+
$result = $this->analyzer->analyzeObject($object);
122+
$this->assertNotEmpty($result);
123+
}
122124
}

0 commit comments

Comments
 (0)