Skip to content

Commit 8c6f3fe

Browse files
authored
Improve AbstractEnumType Types | Enable PHPStan-Doctrine Usage (#196)
1 parent d65fa8f commit 8c6f3fe

21 files changed

+62
-26
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
/vendor/
2+
.phpunit.result.cache
3+
composer.lock

Command/EnumDropCommentCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
159159

160160
$io->title(\sprintf('Dropping comments for <info>%s</info> type...', $this->enumType));
161161

162-
/** @var \Doctrine\ORM\Mapping\ClassMetadata[] $allMetadata */
162+
/** @var \Doctrine\ORM\Mapping\ClassMetadata<object>[] $allMetadata */
163163
$allMetadata = $this->em->getMetadataFactory()->getAllMetadata();
164164

165165
if (!empty($allMetadata)) {

DBAL/Types/AbstractEnumType.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,16 @@
2929
* @author Artem Henvald <genvaldartem@gmail.com>
3030
* @author Ben Davies <ben.davies@gmail.com>
3131
* @author Jaik Dean <jaik@fluoresce.co>
32+
*
33+
* @template T
3234
*/
3335
abstract class AbstractEnumType extends Type
3436
{
3537
/** @var string */
3638
protected $name = '';
3739

3840
/**
39-
* @var array|mixed[] Array of ENUM Values, where ENUM values are keys and their readable versions are values
41+
* @var array<T, T> Array of ENUM Values, where ENUM values are keys and their readable versions are values
4042
*
4143
* @static
4244
*/
@@ -46,6 +48,8 @@ abstract class AbstractEnumType extends Type
4648
* {@inheritdoc}
4749
*
4850
* @throws InvalidArgumentException
51+
*
52+
* @return T|null
4953
*/
5054
public function convertToDatabaseValue($value, AbstractPlatform $platform)
5155
{
@@ -62,6 +66,8 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform)
6266

6367
/**
6468
* {@inheritdoc}
69+
*
70+
* @return T|null
6571
*/
6672
public function convertToPHPValue($value, AbstractPlatform $platform)
6773
{
@@ -73,7 +79,7 @@ public function convertToPHPValue($value, AbstractPlatform $platform)
7379
$choice = static::$choices[$value];
7480
$choices = \array_flip(static::$choices);
7581
if (\is_int($choices[$choice])) {
76-
return (int) $value;
82+
return (int) $value; // @phpstan-ignore-line T can be int
7783
}
7884

7985
return $value;
@@ -167,7 +173,7 @@ public static function getValues(): array
167173
*
168174
* @static
169175
*
170-
* @return int|string
176+
* @return T
171177
*/
172178
public static function getRandomValue()
173179
{
@@ -192,7 +198,7 @@ public static function getReadableValues(): array
192198
/**
193199
* Asserts that given choice exists in the array of ENUM values.
194200
*
195-
* @param mixed $value ENUM value
201+
* @param int|string $value ENUM value
196202
*
197203
* @throws InvalidArgumentException
198204
*/
@@ -206,11 +212,11 @@ public static function assertValidChoice($value): void
206212
/**
207213
* Get value in readable format.
208214
*
209-
* @param mixed $value ENUM value
215+
* @param int|string $value ENUM value
210216
*
211217
* @static
212218
*
213-
* @return mixed Value in readable format
219+
* @return T Value in readable format
214220
*/
215221
public static function getReadableValue($value)
216222
{
@@ -220,9 +226,9 @@ public static function getReadableValue($value)
220226
}
221227

222228
/**
223-
* Check if some string value exists in the array of ENUM values.
229+
* Check if some value exists in the array of ENUM values.
224230
*
225-
* @param mixed $value ENUM value
231+
* @param int|string $value ENUM value
226232
*
227233
* @static
228234
*

DBAL/Types/DayOfWeekFullNameType.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
* DayOfWeekFullNameType.
1717
*
1818
* @author Artem Henvald <genvaldartem@gmail.com>
19+
*
20+
* @extends AbstractEnumType<string>
1921
*/
2022
final class DayOfWeekFullNameType extends AbstractEnumType
2123
{

DBAL/Types/DayOfWeekShortNameType.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
* DayOfWeekShortNameType.
1717
*
1818
* @author Artem Henvald <genvaldartem@gmail.com>
19+
*
20+
* @extends AbstractEnumType<string>
1921
*/
2022
final class DayOfWeekShortNameType extends AbstractEnumType
2123
{

DBAL/Types/MonthFullNameType.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
* MonthFullNameType.
1717
*
1818
* @author Artem Henvald <genvaldartem@gmail.com>
19+
*
20+
* @extends AbstractEnumType<string>
1921
*/
2022
final class MonthFullNameType extends AbstractEnumType
2123
{

DBAL/Types/MonthShortNameType.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
* MonthShortNameType.
1717
*
1818
* @author Artem Henvald <genvaldartem@gmail.com>
19+
*
20+
* @extends AbstractEnumType<string>
1921
*/
2022
final class MonthShortNameType extends AbstractEnumType
2123
{

Form/EnumTypeGuesser.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function guessType(string $class, string $property): ?TypeGuess
6161
return null;
6262
}
6363

64-
/** @var \Doctrine\ORM\Mapping\ClassMetadataInfo $metadata */
64+
/** @var \Doctrine\ORM\Mapping\ClassMetadataInfo<object> $metadata */
6565
[$metadata] = $classMetadata;
6666
$fieldType = $metadata->getTypeOfField($property);
6767

@@ -86,7 +86,7 @@ public function guessType(string $class, string $property): ?TypeGuess
8686
return null;
8787
}
8888

89-
/** @var AbstractEnumType $registeredEnumTypeFQCN */
89+
/** @var AbstractEnumType<int|string> $registeredEnumTypeFQCN */
9090
$parameters = [
9191
'choices' => $registeredEnumTypeFQCN::getChoices(), // Get the choices from the fully qualified class name
9292
'required' => !$metadata->isNullable($property),

Tests/Fixtures/DBAL/Types/AbstractParentType.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
* AbstractParentType.
1919
*
2020
* @author Arturs Vonda <github@artursvonda.lv>
21+
*
22+
* @template T of int|string
23+
*
24+
* @extends AbstractEnumType<T>
2125
*/
2226
abstract class AbstractParentType extends AbstractEnumType
2327
{

Tests/Fixtures/DBAL/Types/BasketballPositionType.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
* BasketballPositionType.
1919
*
2020
* @author Artem Henvald <genvaldartem@gmail.com>
21+
*
22+
* @extends AbstractEnumType<string>
2123
*/
2224
final class BasketballPositionType extends AbstractEnumType
2325
{

Tests/Fixtures/DBAL/Types/InheritedType.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
* InheritedType.
1717
*
1818
* @author Arturs Vonda <github@artursvonda.lv>
19+
*
20+
* @extends AbstractParentType<string>
1921
*/
2022
final class InheritedType extends AbstractParentType
2123
{

Tests/Fixtures/DBAL/Types/MapLocationType.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
* MapLocationType.
1919
*
2020
* @author Artem Henvald <genvaldartem@gmail.com>
21+
*
22+
* @extends AbstractEnumType<string>
2123
*/
2224
final class MapLocationType extends AbstractEnumType
2325
{

Tests/Fixtures/DBAL/Types/NumericType.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
* NumericType.
1919
*
2020
* @author Stephan Vock <stephan.vock@gmail.com>
21+
*
22+
* @extends AbstractEnumType<int>
2123
*/
2224
final class NumericType extends AbstractEnumType
2325
{

Tests/Fixtures/DBAL/Types/StubType.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
* StubType.
1919
*
2020
* @author Artem Henvald <genvaldartem@gmail.com>
21+
*
22+
* @extends AbstractEnumType<string>
2123
*/
2224
final class StubType extends AbstractEnumType
2325
{

Tests/Fixtures/DBAL/Types/TaskStatusType.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
* TaskStatusType.
1919
*
2020
* @author Artem Henvald <genvaldartem@gmail.com>
21+
*
22+
* @extends AbstractEnumType<string>
2123
*/
2224
final class TaskStatusType extends AbstractEnumType
2325
{

Tests/Twig/Extension/ReadableEnumValueTwigExtensionTest.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Fresh\DoctrineEnumBundle\Exception\EnumValue\ValueIsNotFoundInAnyRegisteredEnumTypeException;
1919
use Fresh\DoctrineEnumBundle\Tests\Fixtures\DBAL\Types\BasketballPositionType;
2020
use Fresh\DoctrineEnumBundle\Tests\Fixtures\DBAL\Types\MapLocationType;
21+
use Fresh\DoctrineEnumBundle\Tests\Fixtures\DBAL\Types\NumericType;
2122
use Fresh\DoctrineEnumBundle\Twig\Extension\ReadableEnumValueTwigExtension;
2223
use PHPUnit\Framework\TestCase;
2324
use Twig\TwigFilter;
@@ -37,6 +38,7 @@ protected function setUp(): void
3738
$this->readableEnumValueTwigExtension = new ReadableEnumValueTwigExtension([
3839
'BasketballPositionType' => ['class' => BasketballPositionType::class],
3940
'MapLocationType' => ['class' => MapLocationType::class],
41+
'NumericType' => ['class' => NumericType::class],
4042
]);
4143
}
4244

@@ -56,11 +58,11 @@ public function testGetFilters(): void
5658
/**
5759
* @dataProvider dataProviderForGetReadableEnumValueTest
5860
*
59-
* @param string|null $expectedReadableValue
60-
* @param string|null $enumValue
61+
* @param int|string|null $expectedReadableValue
62+
* @param int|string|null $enumValue
6163
* @param string|null $enumType
6264
*/
63-
public function testGetReadableEnumValue(?string $expectedReadableValue, ?string $enumValue, ?string $enumType): void
65+
public function testGetReadableEnumValue($expectedReadableValue, $enumValue, ?string $enumType): void
6466
{
6567
self::assertEquals(
6668
$expectedReadableValue,
@@ -75,6 +77,8 @@ public static function dataProviderForGetReadableEnumValueTest(): iterable
7577
yield ['Center', BasketballPositionType::CENTER, 'BasketballPositionType'];
7678
yield ['Center', MapLocationType::CENTER, 'MapLocationType'];
7779
yield [null, null, 'MapLocationType'];
80+
yield [1, NumericType::ONE, 'NumericType'];
81+
yield [1, NumericType::ONE, null];
7882
}
7983

8084
public function testEnumTypeIsNotRegisteredException(): void

Twig/Extension/AbstractEnumTwigExtension.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424
*/
2525
abstract class AbstractEnumTwigExtension extends AbstractExtension
2626
{
27-
/** @var string[]|AbstractEnumType[] */
27+
/** @var string[]|AbstractEnumType<int|string>[] */
2828
protected $registeredEnumTypes = [];
2929

30-
/** @var string[]|AbstractEnumType[] */
30+
/** @var string[]|AbstractEnumType<int|string>[] */
3131
protected $occurrences = [];
3232

3333
/**

Twig/Extension/EnumConstantTwigExtension.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
namespace Fresh\DoctrineEnumBundle\Twig\Extension;
1414

15+
use Fresh\DoctrineEnumBundle\DBAL\Types\AbstractEnumType;
1516
use Fresh\DoctrineEnumBundle\Exception\Constant\ConstantIsFoundInFewRegisteredEnumTypesException;
1617
use Fresh\DoctrineEnumBundle\Exception\Constant\ConstantIsNotFoundInAnyRegisteredEnumTypeException;
1718
use Fresh\DoctrineEnumBundle\Exception\EnumType\EnumTypeIsNotRegisteredException;
@@ -88,7 +89,7 @@ public function getEnumConstant(string $enumConstant, ?string $enumType = null):
8889
*/
8990
private function findOccurrences(string $enumConstant): void
9091
{
91-
/** @var class-string<\Fresh\DoctrineEnumBundle\DBAL\Types\AbstractEnumType> $registeredEnumType */
92+
/** @var class-string<AbstractEnumType<int|string>> $registeredEnumType */
9293
foreach ($this->registeredEnumTypes as $registeredEnumType) {
9394
$reflection = new \ReflectionClass($registeredEnumType);
9495

Twig/Extension/ReadableEnumValueTwigExtension.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,17 @@ public function getFilters(): array
3535
}
3636

3737
/**
38-
* @param string|null $enumValue
39-
* @param string|null $enumType
38+
* @param int|string|null $enumValue
39+
* @param string|null $enumType
4040
*
4141
* @throws EnumTypeIsNotRegisteredException
4242
* @throws NoRegisteredEnumTypesException
4343
* @throws ValueIsFoundInFewRegisteredEnumTypesException
4444
* @throws ValueIsNotFoundInAnyRegisteredEnumTypeException
4545
*
46-
* @return string|null
46+
* @return int|string|null
4747
*/
48-
public function getReadableEnumValue(?string $enumValue, ?string $enumType = null): ?string
48+
public function getReadableEnumValue($enumValue, ?string $enumType = null)
4949
{
5050
if ($this->hasRegisteredEnumTypes()) {
5151
if (null === $enumValue) {
@@ -91,9 +91,9 @@ public function getReadableEnumValue(?string $enumValue, ?string $enumType = nul
9191
}
9292

9393
/**
94-
* @param string $enumValue
94+
* @param int|string $enumValue
9595
*/
96-
private function findOccurrences(string $enumValue): void
96+
private function findOccurrences($enumValue): void
9797
{
9898
foreach ($this->registeredEnumTypes as $registeredEnumType) {
9999
if ($registeredEnumType::isValueExist($enumValue)) {

Validator/Constraints/Enum.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
*/
2525
class Enum extends Choice
2626
{
27-
/** @var string|AbstractEnumType */
27+
/** @var string|AbstractEnumType<int|string> */
2828
public $entity;
2929

3030
/**
@@ -35,7 +35,7 @@ public function __construct($options = null)
3535
$this->strict = true;
3636

3737
if (isset($options['entity'])) {
38-
/** @var AbstractEnumType $entity */
38+
/** @var AbstractEnumType<int|string> $entity */
3939
$entity = $options['entity'];
4040

4141
if (\is_subclass_of($entity, AbstractEnumType::class)) {

phpstan.neon

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,3 @@ parameters:
1111
- '%rootDir%/../../../vendor/*'
1212
fileExtensions:
1313
- 'php'
14-
checkGenericClassInNonGenericObjectType: false

0 commit comments

Comments
 (0)