-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[TypeInfo] Add ArrayShapeType
class
#59827
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+241
−3
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
src/Symfony/Component/TypeInfo/Tests/Type/ArrayShapeTypeTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\TypeInfo\Tests\Type; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\TypeInfo\Type; | ||
use Symfony\Component\TypeInfo\Type\ArrayShapeType; | ||
|
||
class ArrayShapeTypeTest extends TestCase | ||
{ | ||
public function testGetCollectionKeyType() | ||
{ | ||
$type = new ArrayShapeType([ | ||
1 => ['type' => Type::bool(), 'optional' => false], | ||
]); | ||
$this->assertEquals(Type::int(), $type->getCollectionKeyType()); | ||
|
||
$type = new ArrayShapeType([ | ||
'foo' => ['type' => Type::bool(), 'optional' => false], | ||
]); | ||
$this->assertEquals(Type::string(), $type->getCollectionKeyType()); | ||
|
||
$type = new ArrayShapeType([ | ||
1 => ['type' => Type::bool(), 'optional' => false], | ||
'foo' => ['type' => Type::bool(), 'optional' => false], | ||
]); | ||
$this->assertEquals(Type::union(Type::int(), Type::string()), $type->getCollectionKeyType()); | ||
} | ||
|
||
public function testGetCollectionValueType() | ||
{ | ||
$type = new ArrayShapeType([ | ||
1 => ['type' => Type::bool(), 'optional' => false], | ||
]); | ||
$this->assertEquals(Type::bool(), $type->getCollectionValueType()); | ||
|
||
$type = new ArrayShapeType([ | ||
'foo' => ['type' => Type::bool(), 'optional' => false], | ||
'bar' => ['type' => Type::int(), 'optional' => false], | ||
]); | ||
$this->assertEquals(Type::union(Type::int(), Type::bool()), $type->getCollectionValueType()); | ||
|
||
$type = new ArrayShapeType([ | ||
'foo' => ['type' => Type::bool(), 'optional' => false], | ||
'bar' => ['type' => Type::nullable(Type::string()), 'optional' => false], | ||
]); | ||
$this->assertEquals(Type::nullable(Type::union(Type::bool(), Type::string())), $type->getCollectionValueType()); | ||
|
||
$type = new ArrayShapeType([ | ||
'foo' => ['type' => Type::true(), 'optional' => false], | ||
'bar' => ['type' => Type::false(), 'optional' => false], | ||
]); | ||
$this->assertEquals(Type::bool(), $type->getCollectionValueType()); | ||
} | ||
|
||
public function testAccepts() | ||
{ | ||
$type = new ArrayShapeType([ | ||
'foo' => ['type' => Type::bool(), 'optional' => false], | ||
'bar' => ['type' => Type::string(), 'optional' => true], | ||
]); | ||
|
||
$this->assertFalse($type->accepts('string')); | ||
$this->assertFalse($type->accepts([])); | ||
$this->assertFalse($type->accepts(['foo' => 'string'])); | ||
$this->assertFalse($type->accepts(['foo' => true, 'other' => 'string'])); | ||
|
||
$this->assertTrue($type->accepts(['foo' => true])); | ||
$this->assertTrue($type->accepts(['foo' => true, 'bar' => 'string'])); | ||
} | ||
|
||
public function testToString() | ||
{ | ||
$type = new ArrayShapeType([1 => ['type' => Type::bool(), 'optional' => false]]); | ||
$this->assertSame('array{1: bool}', (string) $type); | ||
|
||
$type = new ArrayShapeType([ | ||
2 => ['type' => Type::int(), 'optional' => true], | ||
1 => ['type' => Type::bool(), 'optional' => false], | ||
]); | ||
$this->assertSame('array{1: bool, 2?: int}', (string) $type); | ||
|
||
$type = new ArrayShapeType([ | ||
'foo' => ['type' => Type::bool(), 'optional' => false], | ||
'bar' => ['type' => Type::string(), 'optional' => true], | ||
]); | ||
$this->assertSame("array{'bar'?: string, 'foo': bool}", (string) $type); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\TypeInfo\Type; | ||
|
||
use Symfony\Component\TypeInfo\Type; | ||
use Symfony\Component\TypeInfo\TypeIdentifier; | ||
|
||
/** | ||
* Represents the exact shape of an array. | ||
* | ||
* @author Mathias Arlaud <mathias.arlaud@gmail.com> | ||
* | ||
* @extends CollectionType<GenericType<BuiltinType<TypeIdentifier::ARRAY>>> | ||
*/ | ||
final class ArrayShapeType extends CollectionType | ||
{ | ||
/** | ||
* @var array<array{type: Type, optional: bool}> | ||
*/ | ||
private readonly array $shape; | ||
|
||
/** | ||
* @param array<array{type: Type, optional: bool}> $shape | ||
*/ | ||
public function __construct(array $shape) | ||
{ | ||
$keyTypes = []; | ||
$valueTypes = []; | ||
|
||
foreach ($shape as $k => $v) { | ||
$keyTypes[] = self::fromValue($k); | ||
$valueTypes[] = $v['type']; | ||
} | ||
|
||
if ($keyTypes) { | ||
$keyTypes = array_values(array_unique($keyTypes)); | ||
$keyType = \count($keyTypes) > 1 ? self::union(...$keyTypes) : $keyTypes[0]; | ||
} else { | ||
$keyType = Type::union(Type::int(), Type::string()); | ||
} | ||
|
||
$valueType = $valueTypes ? CollectionType::mergeCollectionValueTypes($valueTypes) : Type::mixed(); | ||
|
||
parent::__construct(self::generic(self::builtin(TypeIdentifier::ARRAY), $keyType, $valueType)); | ||
|
||
$sortedShape = $shape; | ||
ksort($sortedShape); | ||
|
||
$this->shape = $sortedShape; | ||
} | ||
|
||
/** | ||
* @return array<array{type: Type, optional: bool}> | ||
*/ | ||
public function getShape(): array | ||
{ | ||
return $this->shape; | ||
} | ||
|
||
public function accepts(mixed $value): bool | ||
{ | ||
if (!\is_array($value)) { | ||
return false; | ||
} | ||
|
||
foreach ($this->shape as $key => $shapeValue) { | ||
if (!($shapeValue['optional'] ?? false) && !\array_key_exists($key, $value)) { | ||
return false; | ||
} | ||
} | ||
|
||
foreach ($value as $key => $itemValue) { | ||
$valueType = $this->shape[$key]['type'] ?? false; | ||
if (!$valueType) { | ||
return false; | ||
} | ||
|
||
if (!$valueType->accepts($itemValue)) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
$items = []; | ||
|
||
foreach ($this->shape as $key => $value) { | ||
$itemKey = \is_int($key) ? (string) $key : \sprintf("'%s'", $key); | ||
if ($value['optional'] ?? false) { | ||
$itemKey = \sprintf('%s?', $itemKey); | ||
} | ||
|
||
$items[] = \sprintf('%s: %s', $itemKey, $value['type']); | ||
} | ||
|
||
return \sprintf('array{%s}', implode(', ', $items)); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For extra keys, the behavior is different between PHPStan (allow) and psalm (restrict). You implemented the strict version:
false
is returned when an extra key is present. This is consistent with psalm and PHPStorm 👍🏻There is a syntax using
...
to allow extra keys (demo); but this is not supported by PHPStorm and PHP_CodeSniffer.This feature can wait for a subsequent PR.