Skip to content

Commit

Permalink
Merge pull request #33 from kynx/explode-visitor-arrays
Browse files Browse the repository at this point in the history
ExplodeVisitor widens type to include arrays
  • Loading branch information
kynx authored Feb 28, 2024
2 parents c82cc8d + e67c679 commit dc55ba6
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 25 deletions.
6 changes: 6 additions & 0 deletions src/Validator/ExplodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ public function visit(ValidatorInterface $validator, Union $previous): Union
$delimiter = $validator->getValueDelimiter();
$validator = $validator->getValidator();

if (! $previous->hasArray()) {
$previous = $previous->getBuilder()
->addType(new TArray([Type::getArrayKey(), new Union([new TString()])]))
->freeze();
}

$narrow = array_merge(
$this->visitArrays($validator, $previous),
$this->visitKeyedArrays($validator, $previous),
Expand Down
2 changes: 1 addition & 1 deletion test/Form/FormElementSmokeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public static function defaultElementProvider(): array
'multicheckbox' => [
MultiCheckbox::class,
[[[], false], [null, false], ['', false], [' ', false]],
"test: non-empty-string", // no haystack by default
"test: array<array-key, string>|non-empty-string", // no haystack by default
],
'number' => [
Number::class,
Expand Down
9 changes: 3 additions & 6 deletions test/Validator/ExplodeVisitorFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Psalm\Type\Atomic\TNever;
use Psalm\Type\Atomic\TNumericString;
use Psalm\Type\Atomic\TString;
use Psalm\Type\Union;
use Psr\Container\ContainerInterface;
Expand All @@ -33,10 +32,9 @@ public function testInvokeReturnsConfiguredInstance(): void
$factory = new ExplodeVisitorFactory();
$instance = $factory($container);

$expected = new Union([new TNumericString()]);
$validator = new Explode(['validator' => new Digits(), 'valueDelimiter' => null]);
$actual = $instance->visit($validator, new Union([new TString()]));
self::assertEquals($expected, $actual);
self::assertArrayHasKey('numeric-string', $actual->getAtomicTypes());
}

public function testInvokeExcludesExplodeVisitor(): void
Expand All @@ -55,12 +53,11 @@ public function testInvokeExcludesExplodeVisitor(): void
$factory = new ExplodeVisitorFactory();
$instance = $factory($container);

$expected = new Union([new TNever()]);
$validator = self::createMock(ValidatorInterface::class);
$validator->expects(self::never())
->method('isValid');
$actual = $instance->visit(new Explode(['validator' => $validator]), $expected);
self::assertEquals($expected, $actual);
$actual = $instance->visit(new Explode(['validator' => $validator]), new Union([new TNever()]));
self::assertArrayHasKey('never', $actual->getAtomicTypes());
}

private function getConfig(array $validatorVisitors): array
Expand Down
45 changes: 27 additions & 18 deletions test/Validator/ExplodeVisitorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@
use Laminas\Validator\Digits;
use Laminas\Validator\Explode;
use PHPUnit\Framework\Attributes\CoversClass;
use Psalm\Type;
use Psalm\Type\Atomic\TArray;
use Psalm\Type\Atomic\TBool;
use Psalm\Type\Atomic\TFloat;
use Psalm\Type\Atomic\TGenericObject;
use Psalm\Type\Atomic\TInt;
use Psalm\Type\Atomic\TKeyedArray;
use Psalm\Type\Atomic\TNamedObject;
use Psalm\Type\Atomic\TNonEmptyString;
use Psalm\Type\Atomic\TNull;
use Psalm\Type\Atomic\TNumericString;
use Psalm\Type\Atomic\TString;
use Psalm\Type\Union;
Expand All @@ -29,21 +34,21 @@ public static function visitProvider(): array
$validator = new Explode(['validator' => $digits]);

return [
// 'no validator' => [
// new Explode(),
// [new TBool()],
// [new TBool()],
// ],
// 'array' => [
// $validator,
// [new TArray([new Union([new TInt()]), new Union([new TString()])])],
// [new TArray([new Union([new TInt()]), new Union([new TNumericString()])])],
// ],
// 'keyed array' => [
// $validator,
// [new TKeyedArray(['a' => new Union([new TString(), new TNull()])])],
// [new TKeyedArray(['a' => new Union([new TNumericString()])])],
// ],
'no validator' => [
new Explode(),
[new TBool()],
[new TBool(), new TArray([Type::getArrayKey(), new Union([new TString()])])],
],
'array' => [
$validator,
[new TArray([new Union([new TInt()]), new Union([new TString()])])],
[new TArray([new Union([new TInt()]), new Union([new TNumericString()])])],
],
'keyed array' => [
$validator,
[new TKeyedArray(['a' => new Union([new TString(), new TNull()])])],
[new TKeyedArray(['a' => new Union([new TNumericString()])])],
],
'traversable' => [
$validator,
[new TNamedObject(Traversable::class)],
Expand All @@ -55,22 +60,26 @@ public static function visitProvider(): array
new TInt(),
]),
]),
new TArray([Type::getArrayKey(), new Union([new TNumericString()])]),
],
],
'generic traversable' => [
$validator,
[new TGenericObject(Traversable::class, [new Union([new TString()])])],
[new TGenericObject(Traversable::class, [new Union([new TNumericString()])])],
[
new TGenericObject(Traversable::class, [new Union([new TNumericString()])]),
new TArray([Type::getArrayKey(), new Union([new TNumericString()])]),
],
],
'no delimiter' => [
new Explode(['validator' => $digits, 'valueDelimiter' => null]),
[new TString()],
[new TNumericString()],
[new TNumericString(), new TArray([Type::getArrayKey(), new Union([new TNumericString()])])],
],
'string' => [
$validator,
[new TNonEmptyString()],
[new TNonEmptyString()],
[new TNonEmptyString(), new TArray([Type::getArrayKey(), new Union([new TNumericString()])])],
],
];
}
Expand Down

0 comments on commit dc55ba6

Please sign in to comment.