Skip to content

Commit

Permalink
Make mixins smarter and with a shorter name
Browse files Browse the repository at this point in the history
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
  • Loading branch information
henriquemoody committed Dec 18, 2024
1 parent 75a9b8e commit 74c018b
Show file tree
Hide file tree
Showing 36 changed files with 4,274 additions and 4,409 deletions.
99 changes: 41 additions & 58 deletions bin/create-mixin
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ use Nette\PhpGenerator\InterfaceType;
use Nette\PhpGenerator\PhpNamespace;
use Nette\PhpGenerator\Printer;
use Respect\Validation\Exceptions\ValidationException;
use Respect\Validation\Mixins\ChainedKey;
use Respect\Validation\Mixins\ChainedLength;
use Respect\Validation\Mixins\ChainedMax;
use Respect\Validation\Mixins\ChainedMin;
use Respect\Validation\Mixins\ChainedNot;
use Respect\Validation\Mixins\ChainedNullOr;
use Respect\Validation\Mixins\ChainedProperty;
use Respect\Validation\Mixins\ChainedUndefOr;
use Respect\Validation\Mixins\ChainedValidator;
use Respect\Validation\Mixins\StaticKey;
use Respect\Validation\Mixins\StaticLength;
use Respect\Validation\Mixins\StaticMax;
use Respect\Validation\Mixins\StaticMin;
use Respect\Validation\Mixins\StaticNot;
use Respect\Validation\Mixins\StaticNullOr;
use Respect\Validation\Mixins\StaticProperty;
use Respect\Validation\Mixins\StaticUndefOr;
use Respect\Validation\Mixins\KeyChain;
use Respect\Validation\Mixins\LengthChain;
use Respect\Validation\Mixins\MaxChain;
use Respect\Validation\Mixins\MinChain;
use Respect\Validation\Mixins\NotChain;
use Respect\Validation\Mixins\NullOrChain;
use Respect\Validation\Mixins\PropertyChain;
use Respect\Validation\Mixins\UndefOrChain;
use Respect\Validation\Mixins\Chain;
use Respect\Validation\Mixins\KeyBuilder;
use Respect\Validation\Mixins\LengthBuilder;
use Respect\Validation\Mixins\MaxBuilder;
use Respect\Validation\Mixins\MinBuilder;
use Respect\Validation\Mixins\NotBuilder;
use Respect\Validation\Mixins\NullOrBuilder;
use Respect\Validation\Mixins\PropertyBuilder;
use Respect\Validation\Mixins\UndefOrBuilder;
use Respect\Validation\Rules\NotUndef;
use Respect\Validation\Rules\NullOr;
use Respect\Validation\Rules\UndefOr;
Expand All @@ -48,8 +48,8 @@ function addMethodToInterface(
}

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

Expand Down Expand Up @@ -182,7 +182,7 @@ function overwriteFile(string $content, string $basename): void
['NullOr', 'nullOr', [], ['Nullable', 'NullOr', 'Optional', 'NotOptional', 'NotUndef', 'UndefOr']],
['Property', 'property', [], $structureRelatedRules],
['UndefOr', 'undefOr', [], ['Nullable', 'NullOr', 'NotOptional', 'NotUndef', 'Optional', 'UndefOr', 'Attributes']],
['Validator', null, [], []],
['', null, [], []],
];

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

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

if ($name === 'Validator') {
if ($name === '') {
$chainedInterface->addExtend(Rule::class);
$chainedInterface->addExtend(ChainedKey::class);
$chainedInterface->addExtend(ChainedLength::class);
$chainedInterface->addExtend(ChainedMax::class);
$chainedInterface->addExtend(ChainedMin::class);
$chainedInterface->addExtend(ChainedNot::class);
$chainedInterface->addExtend(ChainedNullOr::class);
$chainedInterface->addExtend(ChainedProperty::class);
$chainedInterface->addExtend(ChainedUndefOr::class);

$isValid = $chainedInterface->addMethod('isValid')->setPublic()->setReturnType('bool');
$isValid->addParameter('input')->setType('mixed');

$assert = $chainedInterface->addMethod('assert')->setPublic()->setReturnType('void');
$assert->addParameter('input')->setType('mixed');
$assert->addParameter('template')->setType('array|callable|string|\Throwable|null')->setDefaultValue(null);
$assert->addComment(sprintf(
'@param array<string, mixed>|callable(\%s): \Throwable|string|\Throwable|null $template',
ValidationException::class
));

$setTemplates = $chainedInterface->addMethod('setTemplates')->setPublic()->setReturnType(ChainedValidator::class);
$setTemplates->addParameter('templates')->setType('array');
$setTemplates->addComment('@param array<string, mixed> $templates');

$getRules = $chainedInterface->addMethod('getRules')->setPublic()->setReturnType('array');
$getRules->addComment('@return array<Rule>');

$staticInterface->addExtend(StaticKey::class);
$staticInterface->addExtend(StaticLength::class);
$staticInterface->addExtend(StaticMax::class);
$staticInterface->addExtend(StaticMin::class);
$staticInterface->addExtend(StaticNot::class);
$staticInterface->addExtend(StaticNullOr::class);
$staticInterface->addExtend(StaticProperty::class);
$staticInterface->addExtend(StaticUndefOr::class);
$chainedInterface->addExtend(KeyChain::class);
$chainedInterface->addExtend(LengthChain::class);
$chainedInterface->addExtend(MaxChain::class);
$chainedInterface->addExtend(MinChain::class);
$chainedInterface->addExtend(NotChain::class);
$chainedInterface->addExtend(NullOrChain::class);
$chainedInterface->addExtend(PropertyChain::class);
$chainedInterface->addExtend(UndefOrChain::class);
$chainedInterface->addComment('@mixin \\' . \Respect\Validation\Validator::class);

$staticInterface->addExtend(KeyBuilder::class);
$staticInterface->addExtend(LengthBuilder::class);
$staticInterface->addExtend(MaxBuilder::class);
$staticInterface->addExtend(MinBuilder::class);
$staticInterface->addExtend(NotBuilder::class);
$staticInterface->addExtend(NullOrBuilder::class);
$staticInterface->addExtend(PropertyBuilder::class);
$staticInterface->addExtend(UndefOrBuilder::class);
}

foreach ($names as $originalName => $reflection) {
Expand Down
Loading

0 comments on commit 74c018b

Please sign in to comment.