Skip to content

Commit c3379f4

Browse files
committed
fix: Corrected exceptions. Advice is now required
1 parent 9d98025 commit c3379f4

File tree

11 files changed

+69
-21
lines changed

11 files changed

+69
-21
lines changed

src/Blueprint/Application/Exception/BlueprintException.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44

55
namespace PBaszak\UltraMapper\Blueprint\Application\Exception;
66

7-
class BlueprintException extends \LogicException
7+
use PBaszak\UltraMapper\Shared\Application\Exception\UltraMapperException;
8+
9+
class BlueprintException extends UltraMapperException
810
{
911
public function __construct(
1012
string $message,
13+
string $advice,
1114
int $code,
1215
?\Throwable $previous = null
1316
) {
14-
parent::__construct($message, $code, $previous);
17+
parent::__construct($message, $advice, $code, $previous);
1518
}
1619
}

src/Blueprint/Application/Exception/ClassNotFoundException.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44

55
namespace PBaszak\UltraMapper\Blueprint\Application\Exception;
66

7-
class ClassNotFoundException extends \LogicException
7+
use PBaszak\UltraMapper\Shared\Application\Exception\UltraMapperException;
8+
9+
class ClassNotFoundException extends UltraMapperException
810
{
911
public function __construct(
1012
string $message,
13+
string $advice,
1114
int $code,
1215
?\Throwable $previous = null
1316
) {
14-
parent::__construct($message, $code, $previous);
17+
parent::__construct($message, $advice, $code, $previous);
1518
}
1619
}

src/Blueprint/Application/Model/Assets/AttributeBlueprint.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function getReflection(): \ReflectionAttribute
7070
{
7171
$attr = $this->parent->getReflection()->getAttributes($this->class);
7272
if (0 === count($attr)) {
73-
throw new BlueprintException(sprintf('Attribute %s not found on %s.', $this->class, $this->parent->getReflection()->getName()), 5921);
73+
throw new BlueprintException(sprintf('Attribute %s not found on %s.', $this->class, $this->parent->getReflection()->getName()), 'This should not happen. Please report this issue.', 5921);
7474
}
7575
if (1 === count($attr)) {
7676
return $attr[0];
@@ -81,7 +81,7 @@ public function getReflection(): \ReflectionAttribute
8181
}
8282
}
8383

84-
throw new BlueprintException(sprintf('Attribute %s not found on %s.', $this->class, $this->parent->getReflection()->getName()), 5924);
84+
throw new BlueprintException(sprintf('Attribute %s not found on %s.', $this->class, $this->parent->getReflection()->getName()), 'This should not happen. Please report this issue.', 5924);
8585
}
8686

8787
public function hasDeclarationFile(): bool

src/Blueprint/Application/Model/Assets/ClassBlueprint.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ class ClassBlueprint implements Normalizable
3939
public static function create(string $class, ?PropertyBlueprint $parent, ?Blueprint $blueprint = null): self
4040
{
4141
if (__CLASS__ === $class) {
42-
throw new BlueprintException('Unable to create a Blueprint for the ClassBlueprint class. This would create an infinite loop.', 5922);
42+
throw new BlueprintException('Unable to create a Blueprint for the ClassBlueprint class. This would create an infinite loop.', 'Please do not use the ClassBlueprint class as a parameter for the create method.', 5922);
4343
}
4444

4545
try {
4646
$reflection = new \ReflectionClass($class);
4747
/* @phpstan-ignore-next-line */
4848
} catch (\ReflectionException $e) {
49-
throw new ClassNotFoundException(sprintf('Class %s not found. %s', $class, $e->getMessage()), 5931, $e);
49+
throw new ClassNotFoundException(sprintf('Class %s not found. %s', $class, $e->getMessage()), 'Check if the class exists, has correct namespace and filename, and is properly autoloaded.', 5931, $e);
5050
}
5151
$instance = new self();
5252
$instance->blueprintName = $reflection->isAnonymous() ? md5($reflection->getName()) : strtolower(str_replace('\\', '_', $reflection->getName()));

src/Blueprint/Application/Model/Assets/ParameterBlueprint.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function getReflection(): \ReflectionParameter
5454
}
5555
}
5656

57-
throw new BlueprintException('Parameter not found in the reflection method.', 5923);
57+
throw new BlueprintException('Parameter not found in the reflection method.', 'This should not happen. Please report this issue.', 5923);
5858
}
5959

6060
public function normalize(): array

src/Blueprint/Domain/Resolver/TypeResolver.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function process(): self
6464
$this->processReflectionType($this->reflection->getType());
6565
$this->processPhpDocumentorReflectionType(
6666
$docBlockTypeRef,
67-
$this->reflection->getDeclaringClass() ?? throw new ClassNotFoundException('Class not found for '.$this->reflection->getName().' property.', 5932)
67+
$this->reflection->getDeclaringClass() ?? throw new ClassNotFoundException('Class not found for '.$this->reflection->getName().' property.', 'It shouldn\'t happen. Possible reason: '.$this->reflection instanceof \ReflectionParameter ? 'ReflectionParameter without ReflectionClass. Method: '.$this->reflection->getDeclaringFunction()->getName() : 'I\'s impossible :).', 5932)
6868
);
6969

7070
$this->setTypeFromPHPReflection($this->reflection);
@@ -274,7 +274,7 @@ private function processPhpDocumentorReflectionType(?PhpDocumentorReflectionType
274274
}
275275

276276
if (null === $class) {
277-
throw new ClassNotFoundException('Class not found for '.(string) $reflection.'.', 5933);
277+
throw new ClassNotFoundException('Class not found for '.(string) $reflection.'.', 'The type is recognized as `Collection` but the class is not found. File: '.$classReflection->getFileName().'. '.$this->reflection instanceof \ReflectionProperty ? 'Property: ' : 'Parameter: '.$this->reflection->getName().'.', 5933);
278278
}
279279

280280
$this->addType($class);
@@ -355,7 +355,7 @@ private function addCorrectInnerType(string $keyType, PhpDocumentorReflectionTyp
355355
}
356356

357357
if (null === $class) {
358-
throw new ClassNotFoundException('Class not found for '.(string) $itemType.'.', 5934);
358+
throw new ClassNotFoundException('Class not found for '.(string) $itemType.'.', $itemType->getFqsen()?->__toString() ? 'Private method `'.__CLASS__.'::getCorrectClassName()` returned `null` for value: '.var_export($itemType->getFqsen()->__toString(), true).'.' : 'Possible required fix in the library. Please report this issue.', 5934);
359359
}
360360

361361
$this->addInnerType($class, $keyType);

src/Mapper/Domain/Exception/PropertyNotMatchedException.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
namespace PBaszak\UltraMapper\Mapper\Domain\Exception;
66

7-
class PropertyNotMatchedException extends \RuntimeException
7+
use PBaszak\UltraMapper\Shared\Application\Exception\UltraMapperException;
8+
9+
class PropertyNotMatchedException extends UltraMapperException
810
{
911
public function __construct(
1012
string $originPropertyPath,
@@ -14,11 +16,11 @@ public function __construct(
1416
) {
1517
parent::__construct(
1618
sprintf(
17-
'Property "%s" not matched. %s %s',
19+
'Property "%s" not matched. %s',
1820
$originPropertyPath,
1921
$message,
20-
$advice,
2122
),
23+
$advice,
2224
$code,
2325
);
2426
}

src/Mapper/Domain/Exception/Value/InvalidValueException.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
namespace PBaszak\UltraMapper\Mapper\Domain\Exception\Value;
66

7-
class InvalidValueException extends \RuntimeException
7+
use PBaszak\UltraMapper\Shared\Application\Exception\UltraMapperException;
8+
9+
class InvalidValueException extends UltraMapperException
810
{
911
public function __construct(
1012
string $sourcePropertyPath,
@@ -16,13 +18,13 @@ public function __construct(
1618
) {
1719
parent::__construct(
1820
sprintf(
19-
'Invalid value for property "%s". Property source: "%s". Value: `%s`. %s %s',
21+
'Invalid value for property "%s". Property source: "%s". Value: `%s`. %s',
2022
$targetPropertyPath,
2123
$sourcePropertyPath,
2224
var_export($value, true),
2325
$message,
24-
$advice,
2526
),
27+
$advice,
2628
$code,
2729
);
2830
}

src/Mapper/Domain/Exception/Value/NotExistsValueException.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
namespace PBaszak\UltraMapper\Mapper\Domain\Exception\Value;
66

7-
class NotExistsValueException extends \RuntimeException
7+
use PBaszak\UltraMapper\Shared\Application\Exception\UltraMapperException;
8+
9+
class NotExistsValueException extends UltraMapperException
810
{
911
public function __construct(
1012
string $sourcePropertyPath,
@@ -15,12 +17,12 @@ public function __construct(
1517
) {
1618
parent::__construct(
1719
sprintf(
18-
'Not exists value for property "%s". Property source: "%s". %s %s',
20+
'Not exists value for property "%s". Property source: "%s". %s',
1921
$sourcePropertyPath,
2022
$targetPropertyPath,
2123
$message,
22-
$advice,
2324
),
25+
$advice,
2426
$code,
2527
);
2628
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PBaszak\UltraMapper\Shared\Application\Exception;
6+
7+
class UltraMapperException extends \Exception
8+
{
9+
public function __construct(
10+
string $message,
11+
string $advice,
12+
int $code = 0,
13+
?\Throwable $previous = null
14+
) {
15+
parent::__construct(
16+
sprintf('%s. %s.', $this->format($message), $this->format($advice)),
17+
$code,
18+
$previous
19+
);
20+
}
21+
22+
protected function format(string $string): string
23+
{
24+
return ucfirst(trim(rtrim($string, '.')));
25+
}
26+
}

tools/phpstan/fpm-baseline.neon

+10
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ parameters:
2020
count: 1
2121
path: ../../src/Blueprint/Application/Model/Blueprint.php
2222

23+
-
24+
message: "#^Call to an undefined method ReflectionParameter\\|ReflectionProperty\\:\\:getDeclaringFunction\\(\\)\\.$#"
25+
count: 1
26+
path: ../../src/Blueprint/Domain/Resolver/TypeResolver.php
27+
2328
-
2429
message: "#^Method PBaszak\\\\UltraMapper\\\\Blueprint\\\\Domain\\\\Resolver\\\\TypeResolver\\:\\:getCorrectClassName\\(\\) should return class\\-string\\|null but returns string\\.$#"
2530
count: 3
@@ -35,6 +40,11 @@ parameters:
3540
count: 1
3641
path: ../../src/Blueprint/Domain/Resolver/TypeResolver.php
3742

43+
-
44+
message: "#^Ternary operator condition is always true\\.$#"
45+
count: 2
46+
path: ../../src/Blueprint/Domain/Resolver/TypeResolver.php
47+
3848
-
3949
message: "#^PHPDoc tag @throws with type PBaszak\\\\UltraMapper\\\\Mapper\\\\Application\\\\Exception\\\\AttributeException is not subtype of Throwable$#"
4050
count: 1

0 commit comments

Comments
 (0)