Skip to content

Commit

Permalink
Improve the type descriptor for SimpleArrayType
Browse files Browse the repository at this point in the history
When converting database values to PHP, this type always produces a `list<string>`
  • Loading branch information
stof committed Apr 5, 2023
1 parent 62bd362 commit aa11767
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/Type/Doctrine/Descriptors/SimpleArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace PHPStan\Type\Doctrine\Descriptors;

use PHPStan\Type\Accessory\AccessoryArrayListType;
use PHPStan\Type\ArrayType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\MixedType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
Expand All @@ -17,12 +19,12 @@ public function getType(): string

public function getWritableToPropertyType(): Type
{
return new ArrayType(new MixedType(), new MixedType());
return AccessoryArrayListType::intersectWith(new ArrayType(new IntegerType(), new StringType()));
}

public function getWritableToDatabaseType(): Type
{
return new ArrayType(new MixedType(), new MixedType());
return new ArrayType(new MixedType(), new StringType());
}

public function getDatabaseInternalType(): Type
Expand Down
10 changes: 10 additions & 0 deletions tests/Rules/Doctrine/ORM/EntityColumnRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use PHPStan\Type\Doctrine\Descriptors\JsonType;
use PHPStan\Type\Doctrine\Descriptors\Ramsey\UuidTypeDescriptor;
use PHPStan\Type\Doctrine\Descriptors\ReflectionDescriptor;
use PHPStan\Type\Doctrine\Descriptors\SimpleArrayType;
use PHPStan\Type\Doctrine\Descriptors\StringType;
use PHPStan\Type\Doctrine\ObjectMetadataResolver;
use Ramsey\Uuid\Doctrine\UuidType;
Expand Down Expand Up @@ -68,6 +69,7 @@ protected function getRule(): Rule
new JsonType(),
new IntegerType(),
new StringType(),
new SimpleArrayType(),
new UuidTypeDescriptor(UuidType::class),
new ReflectionDescriptor(CarbonImmutableType::class, $this->createBroker()),
new ReflectionDescriptor(CarbonType::class, $this->createBroker()),
Expand Down Expand Up @@ -156,6 +158,10 @@ public function testRule(?string $objectManagerLoader): void
'Property PHPStan\Rules\Doctrine\ORM\MyBrokenEntity::$incompatibleJsonValueObject type mapping mismatch: property can contain PHPStan\Rules\Doctrine\ORM\EmptyObject but database expects array|bool|float|int|JsonSerializable|stdClass|string|null.',
156,
],
[
'Property PHPStan\Rules\Doctrine\ORM\MyBrokenEntity::$invalidSimpleArray type mapping mismatch: property can contain array but database expects array<string>.',
162,
],
]);
}

Expand Down Expand Up @@ -211,6 +217,10 @@ public function testRuleWithAllowedNullableProperty(?string $objectManagerLoader
'Property PHPStan\Rules\Doctrine\ORM\MyBrokenEntity::$incompatibleJsonValueObject type mapping mismatch: property can contain PHPStan\Rules\Doctrine\ORM\EmptyObject but database expects array|bool|float|int|JsonSerializable|stdClass|string|null.',
156,
],
[
'Property PHPStan\Rules\Doctrine\ORM\MyBrokenEntity::$invalidSimpleArray type mapping mismatch: property can contain array but database expects array<string>.',
162,
],
]);
}

Expand Down
12 changes: 12 additions & 0 deletions tests/Rules/Doctrine/ORM/data/MyBrokenEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,16 @@ class MyBrokenEntity extends MyBrokenSuperclass
* @var EmptyObject
*/
private $incompatibleJsonValueObject;

/**
* @ORM\Column(type="simple_array")
* @var int[]
*/
private $invalidSimpleArray;

/**
* @ORM\Column(type="simple_array")
* @var list<string>
*/
private $validSimpleArray;
}

0 comments on commit aa11767

Please sign in to comment.