Skip to content

Commit c65652d

Browse files
committed
Rework exceptions
1 parent 5f6ed75 commit c65652d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+238
-260
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
],
1313
"require": {
1414
"symplify/easy-coding-standard": "^7.2.3",
15-
"wol-soft/php-json-schema-model-generator-production": "^0.11.0",
15+
"wol-soft/php-json-schema-model-generator-production": "dev-ReworkExceptions",
1616
"wol-soft/php-micro-template": "^1.3.1",
1717

1818
"php": ">=7.2",

src/Model/RenderJob.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
use PHPModelGenerator\Exception\RenderException;
1111
use PHPModelGenerator\Utils\RenderHelper;
1212

13+
/**
14+
* Class RenderJob
15+
*
16+
* @package PHPModelGenerator\Model
17+
*/
1318
class RenderJob
1419
{
1520
/** @var Schema */
@@ -128,7 +133,7 @@ protected function renderClass(GeneratorConfiguration $generatorConfiguration):
128133
'Model.phptpl',
129134
[
130135
'namespace' => $namespace,
131-
'use' => empty($use) ? '' : 'use ' . join(";\nuse ", array_unique($use)) . ';',
136+
'use' => 'use ' . join(";\nuse ", array_unique($use)) . ';',
132137
'class' => $this->className,
133138
'baseValidators' => $this->schema->getBaseValidators(),
134139
'properties' => $this->schema->getProperties(),

src/Model/Validator/AbstractPropertyValidator.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,24 @@
1616
abstract class AbstractPropertyValidator implements PropertyValidatorInterface
1717
{
1818
/** @var string */
19-
protected $exceptionMessage;
19+
protected $exceptionClass;
20+
/** @var array */
21+
protected $exceptionParams;
2022

2123
/**
22-
* Get the message of the exception which is thrown if the validation fails
23-
*
24-
* @return string
24+
* @inheritDoc
25+
*/
26+
public function getExceptionClass(): string
27+
{
28+
return $this->exceptionClass;
29+
}
30+
31+
/**
32+
* @inheritDoc
2533
*/
26-
public function getExceptionMessage(): string
34+
public function getExceptionParams(): array
2735
{
28-
return $this->exceptionMessage;
36+
return $this->exceptionParams;
2937
}
3038

3139
/**

src/Model/Validator/AdditionalItemsValidator.php

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44

55
namespace PHPModelGenerator\Model\Validator;
66

7-
use PHPMicroTemplate\Exception\FileSystemException;
8-
use PHPMicroTemplate\Exception\SyntaxErrorException;
9-
use PHPMicroTemplate\Exception\UndefinedSymbolException;
7+
use PHPModelGenerator\Exception\Arrays\InvalidAdditionalItemsException;
108
use PHPModelGenerator\Exception\SchemaException;
119
use PHPModelGenerator\Model\Schema;
1210
use PHPModelGenerator\SchemaProcessor\SchemaProcessor;
@@ -23,8 +21,7 @@ class AdditionalItemsValidator extends AdditionalPropertiesValidator
2321
protected const PROPERTIES_KEY = 'items';
2422
protected const ADDITIONAL_PROPERTIES_KEY = 'additionalItems';
2523

26-
/** @var string */
27-
private $propertyName;
24+
protected const EXCEPTION_CLASS = InvalidAdditionalItemsException::class;
2825

2926
/**
3027
* AdditionalItemsValidator constructor.
@@ -34,37 +31,14 @@ class AdditionalItemsValidator extends AdditionalPropertiesValidator
3431
* @param array $propertiesStructure
3532
* @param string $propertyName
3633
*
37-
* @throws FileSystemException
3834
* @throws SchemaException
39-
* @throws SyntaxErrorException
40-
* @throws UndefinedSymbolException
4135
*/
4236
public function __construct(
4337
SchemaProcessor $schemaProcessor,
4438
Schema $schema,
4539
array $propertiesStructure,
4640
string $propertyName
4741
) {
48-
$this->propertyName = $propertyName;
49-
50-
parent::__construct($schemaProcessor, $schema, $propertiesStructure);
51-
}
52-
53-
/**
54-
* Initialize all variables which are required to execute a property names validator
55-
*
56-
* @return string
57-
*/
58-
public function getValidatorSetUp(): string
59-
{
60-
return '
61-
$properties = $value;
62-
$invalidProperties = [];
63-
';
64-
}
65-
66-
protected function getErrorMessage(): string
67-
{
68-
return "Tuple array {$this->propertyName} contains invalid additional items.";
42+
parent::__construct($schemaProcessor, $schema, $propertiesStructure, $propertyName);
6943
}
7044
}

src/Model/Validator/AdditionalPropertiesValidator.php

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44

55
namespace PHPModelGenerator\Model\Validator;
66

7-
use PHPMicroTemplate\Exception\FileSystemException;
8-
use PHPMicroTemplate\Exception\SyntaxErrorException;
9-
use PHPMicroTemplate\Exception\UndefinedSymbolException;
7+
use PHPModelGenerator\Exception\Generic\InvalidAdditionalPropertiesException;
108
use PHPModelGenerator\Exception\SchemaException;
119
use PHPModelGenerator\Model\Schema;
1210
use PHPModelGenerator\PropertyProcessor\PropertyMetaDataCollection;
@@ -27,19 +25,24 @@ class AdditionalPropertiesValidator extends PropertyTemplateValidator
2725
protected const PROPERTIES_KEY = 'properties';
2826
protected const ADDITIONAL_PROPERTIES_KEY = 'additionalProperties';
2927

28+
protected const EXCEPTION_CLASS = InvalidAdditionalPropertiesException::class;
29+
3030
/**
3131
* AdditionalPropertiesValidator constructor.
3232
*
3333
* @param SchemaProcessor $schemaProcessor
34-
* @param Schema $schema
35-
* @param array $propertiesStructure
34+
* @param Schema $schema
35+
* @param array $propertiesStructure
36+
* @param string|null $propertyName
3637
*
37-
* @throws FileSystemException
3838
* @throws SchemaException
39-
* @throws SyntaxErrorException
40-
* @throws UndefinedSymbolException
4139
*/
42-
public function __construct(SchemaProcessor $schemaProcessor, Schema $schema, array $propertiesStructure) {
40+
public function __construct(
41+
SchemaProcessor $schemaProcessor,
42+
Schema $schema,
43+
array $propertiesStructure,
44+
?string $propertyName = null
45+
) {
4346
$propertyFactory = new PropertyFactory(new PropertyProcessorFactory());
4447

4548
$validationProperty = $propertyFactory->create(
@@ -53,10 +56,6 @@ public function __construct(SchemaProcessor $schemaProcessor, Schema $schema, ar
5356
$this->removeRequiredPropertyValidator($validationProperty);
5457

5558
parent::__construct(
56-
$this->getRenderer()->renderTemplate(
57-
DIRECTORY_SEPARATOR . 'Exception' . DIRECTORY_SEPARATOR . 'InvalidPropertiesException.phptpl',
58-
['error' => $this->getErrorMessage(), 'property' => static::PROPERTY_NAME]
59-
),
6059
DIRECTORY_SEPARATOR . 'Validator' . DIRECTORY_SEPARATOR . 'AdditionalProperties.phptpl',
6160
[
6261
'validationProperty' => $validationProperty,
@@ -67,7 +66,9 @@ public function __construct(SchemaProcessor $schemaProcessor, Schema $schema, ar
6766
),
6867
'generatorConfiguration' => $schemaProcessor->getGeneratorConfiguration(),
6968
'viewHelper' => new RenderHelper($schemaProcessor->getGeneratorConfiguration()),
70-
]
69+
],
70+
static::EXCEPTION_CLASS,
71+
[$propertyName ?? $schema->getClassName(), '&$invalidProperties']
7172
);
7273
}
7374

@@ -79,13 +80,8 @@ public function __construct(SchemaProcessor $schemaProcessor, Schema $schema, ar
7980
public function getValidatorSetUp(): string
8081
{
8182
return '
82-
$properties = $modelData;
83+
$properties = $value;
8384
$invalidProperties = [];
8485
';
8586
}
86-
87-
protected function getErrorMessage(): string
88-
{
89-
return 'Provided JSON contains invalid additional properties.';
90-
}
9187
}

src/Model/Validator/ArrayItemValidator.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PHPMicroTemplate\Exception\FileSystemException;
88
use PHPMicroTemplate\Exception\SyntaxErrorException;
99
use PHPMicroTemplate\Exception\UndefinedSymbolException;
10+
use PHPModelGenerator\Exception\Arrays\InvalidItemException;
1011
use PHPModelGenerator\Exception\SchemaException;
1112
use PHPModelGenerator\Model\Property\PropertyInterface;
1213
use PHPModelGenerator\Model\Schema;
@@ -61,17 +62,15 @@ public function __construct(
6162
$property->addTypeHintDecorator(new ArrayTypeHintDecorator($nestedProperty));
6263

6364
parent::__construct(
64-
$this->getRenderer()->renderTemplate(
65-
DIRECTORY_SEPARATOR . 'Exception' . DIRECTORY_SEPARATOR . 'InvalidArrayItemsException.phptpl',
66-
['propertyName' => $property->getName(), 'suffix' => $this->variableSuffix]
67-
),
6865
DIRECTORY_SEPARATOR . 'Validator' . DIRECTORY_SEPARATOR . 'ArrayItem.phptpl',
6966
[
7067
'nestedProperty' => $nestedProperty,
7168
'viewHelper' => new RenderHelper($schemaProcessor->getGeneratorConfiguration()),
7269
'generatorConfiguration' => $schemaProcessor->getGeneratorConfiguration(),
7370
'suffix' => $this->variableSuffix,
74-
]
71+
],
72+
InvalidItemException::class,
73+
[$property->getName(), "&\$invalidItems{$this->variableSuffix}"]
7574
);
7675
}
7776

src/Model/Validator/ArrayTupleValidator.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44

55
namespace PHPModelGenerator\Model\Validator;
66

7-
use PHPMicroTemplate\Exception\FileSystemException;
8-
use PHPMicroTemplate\Exception\SyntaxErrorException;
9-
use PHPMicroTemplate\Exception\UndefinedSymbolException;
7+
use PHPModelGenerator\Exception\Arrays\InvalidTupleException;
108
use PHPModelGenerator\Exception\SchemaException;
119
use PHPModelGenerator\Model\Schema;
1210
use PHPModelGenerator\PropertyProcessor\PropertyMetaDataCollection;
@@ -31,9 +29,6 @@ class ArrayTupleValidator extends PropertyTemplateValidator
3129
* @param string $propertyName
3230
*
3331
* @throws SchemaException
34-
* @throws FileSystemException
35-
* @throws SyntaxErrorException
36-
* @throws UndefinedSymbolException
3732
*/
3833
public function __construct(
3934
SchemaProcessor $schemaProcessor,
@@ -60,16 +55,14 @@ public function __construct(
6055
}
6156

6257
parent::__construct(
63-
$this->getRenderer()->renderTemplate(
64-
DIRECTORY_SEPARATOR . 'Exception' . DIRECTORY_SEPARATOR . 'InvalidArrayTuplesException.phptpl',
65-
['propertyName' => $propertyName]
66-
),
6758
DIRECTORY_SEPARATOR . 'Validator' . DIRECTORY_SEPARATOR . 'ArrayTuple.phptpl',
6859
[
6960
'tupleProperties' => $tupleProperties,
7061
'viewHelper' => new RenderHelper($schemaProcessor->getGeneratorConfiguration()),
7162
'generatorConfiguration' => $schemaProcessor->getGeneratorConfiguration(),
72-
]
63+
],
64+
InvalidTupleException::class,
65+
[$propertyName, '&$invalidTuples']
7366
);
7467
}
7568

src/Model/Validator/ComposedPropertyValidator.php

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

55
namespace PHPModelGenerator\Model\Validator;
66

7-
use PHPMicroTemplate\Exception\FileSystemException;
8-
use PHPMicroTemplate\Exception\SyntaxErrorException;
9-
use PHPMicroTemplate\Exception\UndefinedSymbolException;
7+
use PHPModelGenerator\Exception\ComposedValue\InvalidComposedValueException;
108
use PHPModelGenerator\Model\Property\CompositionPropertyDecorator;
119
use PHPModelGenerator\Model\Property\PropertyInterface;
1210

@@ -24,10 +22,6 @@ class ComposedPropertyValidator extends AbstractComposedPropertyValidator
2422
* @param CompositionPropertyDecorator[] $composedProperties
2523
* @param string $composedProcessor
2624
* @param array $validatorVariables
27-
*
28-
* @throws FileSystemException
29-
* @throws SyntaxErrorException
30-
* @throws UndefinedSymbolException
3125
*/
3226
public function __construct(
3327
PropertyInterface $property,
@@ -36,15 +30,10 @@ public function __construct(
3630
array $validatorVariables
3731
) {
3832
parent::__construct(
39-
$this->getRenderer()->renderTemplate(
40-
DIRECTORY_SEPARATOR . 'Exception' . DIRECTORY_SEPARATOR . 'ComposedValueException.phptpl',
41-
[
42-
'propertyName' => $property->getName(),
43-
'composedErrorMessage' => $validatorVariables['composedErrorMessage'],
44-
]
45-
),
4633
DIRECTORY_SEPARATOR . 'Validator' . DIRECTORY_SEPARATOR . 'ComposedItem.phptpl',
47-
$validatorVariables
34+
$validatorVariables,
35+
$this->getExceptionByProcessor($composedProcessor),
36+
[$property->getName(), '&$succeededCompositionElements', '&$compositionErrorCollection']
4837
);
4938

5039
$this->composedProcessor = $composedProcessor;
@@ -59,9 +48,28 @@ public function __construct(
5948
public function getValidatorSetUp(): string
6049
{
6150
return '
62-
$i = 0;
6351
$succeededCompositionElements = 0;
6452
$compositionErrorCollection = [];
6553
';
6654
}
55+
56+
/**
57+
* Parse the composition type (allOf, anyOf, ...) from the given processor and get the corresponding exception class
58+
*
59+
* @param string $composedProcessor
60+
*
61+
* @return string
62+
*/
63+
private function getExceptionByProcessor(string $composedProcessor): string
64+
{
65+
return str_replace(
66+
DIRECTORY_SEPARATOR,
67+
'\\',
68+
dirname(str_replace('\\', DIRECTORY_SEPARATOR, InvalidComposedValueException::class))
69+
) . '\\' . str_replace(
70+
'Processor',
71+
'',
72+
substr($composedProcessor, strrpos($composedProcessor, '\\') + 1)
73+
) . 'Exception';
74+
}
6775
}

src/Model/Validator/EnumValidator.php

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

55
namespace PHPModelGenerator\Model\Validator;
66

7+
use PHPModelGenerator\Exception\Generic\EnumException;
78
use PHPModelGenerator\Model\Property\PropertyInterface;
89

910
/**
@@ -26,7 +27,8 @@ public function __construct(PropertyInterface $property, array $allowedValues)
2627
'!in_array($value, ' .
2728
preg_replace('(\d+\s=>)', '', var_export($allowedValues, true)) .
2829
', true)',
29-
"Invalid value for {$property->getName()} declined by enum constraint"
30+
EnumException::class,
31+
[$property->getName(), $allowedValues]
3032
);
3133
}
3234
}

src/Model/Validator/InstanceOfValidator.php

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

55
namespace PHPModelGenerator\Model\Validator;
66

7+
use PHPModelGenerator\Exception\Generic\InvalidInstanceOfException;
78
use PHPModelGenerator\Model\Property\PropertyInterface;
89

910
/**
@@ -22,11 +23,8 @@ public function __construct(PropertyInterface $property)
2223
{
2324
parent::__construct(
2425
sprintf('is_object($value) && !($value instanceof %s)', $property->getType()),
25-
sprintf(
26-
'Invalid class for %s. Requires %s, got " . get_class($value) . "',
27-
$property->getName(),
28-
$property->getType()
29-
)
26+
InvalidInstanceOfException::class,
27+
[$property->getName(), $property->getType()]
3028
);
3129
}
3230
}

0 commit comments

Comments
 (0)