Skip to content

Commit 74c018b

Browse files
committed
Make mixins smarter and with a shorter name
To make PHPStan recognize methods when we call Validator with static and non-static rule names, I added a few methods from `Validator` to the `ChainedValidator` interface[1]. However, this didn't work so well because there could have been more methods from `Validator`. This commit will rename the mixins to better names, but it will also make the `Chain` (old `ChainedValidator` to have a `@mixin` on itself of the `Validator` class. [1]: a974c0c
1 parent 75a9b8e commit 74c018b

36 files changed

+4274
-4409
lines changed

bin/create-mixin

Lines changed: 41 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,23 @@ use Nette\PhpGenerator\InterfaceType;
99
use Nette\PhpGenerator\PhpNamespace;
1010
use Nette\PhpGenerator\Printer;
1111
use Respect\Validation\Exceptions\ValidationException;
12-
use Respect\Validation\Mixins\ChainedKey;
13-
use Respect\Validation\Mixins\ChainedLength;
14-
use Respect\Validation\Mixins\ChainedMax;
15-
use Respect\Validation\Mixins\ChainedMin;
16-
use Respect\Validation\Mixins\ChainedNot;
17-
use Respect\Validation\Mixins\ChainedNullOr;
18-
use Respect\Validation\Mixins\ChainedProperty;
19-
use Respect\Validation\Mixins\ChainedUndefOr;
20-
use Respect\Validation\Mixins\ChainedValidator;
21-
use Respect\Validation\Mixins\StaticKey;
22-
use Respect\Validation\Mixins\StaticLength;
23-
use Respect\Validation\Mixins\StaticMax;
24-
use Respect\Validation\Mixins\StaticMin;
25-
use Respect\Validation\Mixins\StaticNot;
26-
use Respect\Validation\Mixins\StaticNullOr;
27-
use Respect\Validation\Mixins\StaticProperty;
28-
use Respect\Validation\Mixins\StaticUndefOr;
12+
use Respect\Validation\Mixins\KeyChain;
13+
use Respect\Validation\Mixins\LengthChain;
14+
use Respect\Validation\Mixins\MaxChain;
15+
use Respect\Validation\Mixins\MinChain;
16+
use Respect\Validation\Mixins\NotChain;
17+
use Respect\Validation\Mixins\NullOrChain;
18+
use Respect\Validation\Mixins\PropertyChain;
19+
use Respect\Validation\Mixins\UndefOrChain;
20+
use Respect\Validation\Mixins\Chain;
21+
use Respect\Validation\Mixins\KeyBuilder;
22+
use Respect\Validation\Mixins\LengthBuilder;
23+
use Respect\Validation\Mixins\MaxBuilder;
24+
use Respect\Validation\Mixins\MinBuilder;
25+
use Respect\Validation\Mixins\NotBuilder;
26+
use Respect\Validation\Mixins\NullOrBuilder;
27+
use Respect\Validation\Mixins\PropertyBuilder;
28+
use Respect\Validation\Mixins\UndefOrBuilder;
2929
use Respect\Validation\Rules\NotUndef;
3030
use Respect\Validation\Rules\NullOr;
3131
use Respect\Validation\Rules\UndefOr;
@@ -48,8 +48,8 @@ function addMethodToInterface(
4848
}
4949

5050
$name = $prefix ? $prefix . ucfirst($originalName) : lcfirst($originalName);
51-
$method = $interfaceType->addMethod($name)->setPublic()->setReturnType(ChainedValidator::class);
52-
if (str_starts_with($interfaceType->getName(), 'Static')) {
51+
$method = $interfaceType->addMethod($name)->setPublic()->setReturnType(Chain::class);
52+
if (str_contains($interfaceType->getName(), 'Builder')) {
5353
$method->setStatic();
5454
}
5555

@@ -182,7 +182,7 @@ function overwriteFile(string $content, string $basename): void
182182
['NullOr', 'nullOr', [], ['Nullable', 'NullOr', 'Optional', 'NotOptional', 'NotUndef', 'UndefOr']],
183183
['Property', 'property', [], $structureRelatedRules],
184184
['UndefOr', 'undefOr', [], ['Nullable', 'NullOr', 'NotOptional', 'NotUndef', 'Optional', 'UndefOr', 'Attributes']],
185-
['Validator', null, [], []],
185+
['', null, [], []],
186186
];
187187

188188
$names = [];
@@ -212,49 +212,32 @@ function overwriteFile(string $content, string $basename): void
212212
foreach ($mixins as [$name, $prefix, $allowList, $denyList]) {
213213
$chainedNamespace = new PhpNamespace('Respect\\Validation\\Mixins');
214214
$chainedNamespace->addUse(Rule::class);
215-
$chainedInterface = $chainedNamespace->addInterface('Chained' . $name);
215+
$chainedInterface = $chainedNamespace->addInterface($name . 'Chain');
216216

217217
$staticNamespace = new PhpNamespace('Respect\\Validation\\Mixins');
218218
$staticNamespace->addUse(Rule::class);
219-
$staticInterface = $staticNamespace->addInterface('Static' . $name);
219+
$staticInterface = $staticNamespace->addInterface($name . 'Builder');
220220

221-
if ($name === 'Validator') {
221+
if ($name === '') {
222222
$chainedInterface->addExtend(Rule::class);
223-
$chainedInterface->addExtend(ChainedKey::class);
224-
$chainedInterface->addExtend(ChainedLength::class);
225-
$chainedInterface->addExtend(ChainedMax::class);
226-
$chainedInterface->addExtend(ChainedMin::class);
227-
$chainedInterface->addExtend(ChainedNot::class);
228-
$chainedInterface->addExtend(ChainedNullOr::class);
229-
$chainedInterface->addExtend(ChainedProperty::class);
230-
$chainedInterface->addExtend(ChainedUndefOr::class);
231-
232-
$isValid = $chainedInterface->addMethod('isValid')->setPublic()->setReturnType('bool');
233-
$isValid->addParameter('input')->setType('mixed');
234-
235-
$assert = $chainedInterface->addMethod('assert')->setPublic()->setReturnType('void');
236-
$assert->addParameter('input')->setType('mixed');
237-
$assert->addParameter('template')->setType('array|callable|string|\Throwable|null')->setDefaultValue(null);
238-
$assert->addComment(sprintf(
239-
'@param array<string, mixed>|callable(\%s): \Throwable|string|\Throwable|null $template',
240-
ValidationException::class
241-
));
242-
243-
$setTemplates = $chainedInterface->addMethod('setTemplates')->setPublic()->setReturnType(ChainedValidator::class);
244-
$setTemplates->addParameter('templates')->setType('array');
245-
$setTemplates->addComment('@param array<string, mixed> $templates');
246-
247-
$getRules = $chainedInterface->addMethod('getRules')->setPublic()->setReturnType('array');
248-
$getRules->addComment('@return array<Rule>');
249-
250-
$staticInterface->addExtend(StaticKey::class);
251-
$staticInterface->addExtend(StaticLength::class);
252-
$staticInterface->addExtend(StaticMax::class);
253-
$staticInterface->addExtend(StaticMin::class);
254-
$staticInterface->addExtend(StaticNot::class);
255-
$staticInterface->addExtend(StaticNullOr::class);
256-
$staticInterface->addExtend(StaticProperty::class);
257-
$staticInterface->addExtend(StaticUndefOr::class);
223+
$chainedInterface->addExtend(KeyChain::class);
224+
$chainedInterface->addExtend(LengthChain::class);
225+
$chainedInterface->addExtend(MaxChain::class);
226+
$chainedInterface->addExtend(MinChain::class);
227+
$chainedInterface->addExtend(NotChain::class);
228+
$chainedInterface->addExtend(NullOrChain::class);
229+
$chainedInterface->addExtend(PropertyChain::class);
230+
$chainedInterface->addExtend(UndefOrChain::class);
231+
$chainedInterface->addComment('@mixin \\' . \Respect\Validation\Validator::class);
232+
233+
$staticInterface->addExtend(KeyBuilder::class);
234+
$staticInterface->addExtend(LengthBuilder::class);
235+
$staticInterface->addExtend(MaxBuilder::class);
236+
$staticInterface->addExtend(MinBuilder::class);
237+
$staticInterface->addExtend(NotBuilder::class);
238+
$staticInterface->addExtend(NullOrBuilder::class);
239+
$staticInterface->addExtend(PropertyBuilder::class);
240+
$staticInterface->addExtend(UndefOrBuilder::class);
258241
}
259242

260243
foreach ($names as $originalName => $reflection) {

0 commit comments

Comments
 (0)