Skip to content

Commit

Permalink
Improve AbstractEnumType Types | Enable PHPStan-Doctrine Usage (#196)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandruGG committed Jun 12, 2021
1 parent d65fa8f commit 8c6f3fe
Show file tree
Hide file tree
Showing 21 changed files with 62 additions and 26 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
/vendor/
.phpunit.result.cache
composer.lock
2 changes: 1 addition & 1 deletion Command/EnumDropCommentCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int

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

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

if (!empty($allMetadata)) {
Expand Down
22 changes: 14 additions & 8 deletions DBAL/Types/AbstractEnumType.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,16 @@
* @author Artem Henvald <genvaldartem@gmail.com>
* @author Ben Davies <ben.davies@gmail.com>
* @author Jaik Dean <jaik@fluoresce.co>
*
* @template T
*/
abstract class AbstractEnumType extends Type
{
/** @var string */
protected $name = '';

/**
* @var array|mixed[] Array of ENUM Values, where ENUM values are keys and their readable versions are values
* @var array<T, T> Array of ENUM Values, where ENUM values are keys and their readable versions are values
*
* @static
*/
Expand All @@ -46,6 +48,8 @@ abstract class AbstractEnumType extends Type
* {@inheritdoc}
*
* @throws InvalidArgumentException
*
* @return T|null
*/
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
Expand All @@ -62,6 +66,8 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform)

/**
* {@inheritdoc}
*
* @return T|null
*/
public function convertToPHPValue($value, AbstractPlatform $platform)
{
Expand All @@ -73,7 +79,7 @@ public function convertToPHPValue($value, AbstractPlatform $platform)
$choice = static::$choices[$value];
$choices = \array_flip(static::$choices);
if (\is_int($choices[$choice])) {
return (int) $value;
return (int) $value; // @phpstan-ignore-line T can be int
}

return $value;
Expand Down Expand Up @@ -167,7 +173,7 @@ public static function getValues(): array
*
* @static
*
* @return int|string
* @return T
*/
public static function getRandomValue()
{
Expand All @@ -192,7 +198,7 @@ public static function getReadableValues(): array
/**
* Asserts that given choice exists in the array of ENUM values.
*
* @param mixed $value ENUM value
* @param int|string $value ENUM value
*
* @throws InvalidArgumentException
*/
Expand All @@ -206,11 +212,11 @@ public static function assertValidChoice($value): void
/**
* Get value in readable format.
*
* @param mixed $value ENUM value
* @param int|string $value ENUM value
*
* @static
*
* @return mixed Value in readable format
* @return T Value in readable format
*/
public static function getReadableValue($value)
{
Expand All @@ -220,9 +226,9 @@ public static function getReadableValue($value)
}

/**
* Check if some string value exists in the array of ENUM values.
* Check if some value exists in the array of ENUM values.
*
* @param mixed $value ENUM value
* @param int|string $value ENUM value
*
* @static
*
Expand Down
2 changes: 2 additions & 0 deletions DBAL/Types/DayOfWeekFullNameType.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
* DayOfWeekFullNameType.
*
* @author Artem Henvald <genvaldartem@gmail.com>
*
* @extends AbstractEnumType<string>
*/
final class DayOfWeekFullNameType extends AbstractEnumType
{
Expand Down
2 changes: 2 additions & 0 deletions DBAL/Types/DayOfWeekShortNameType.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
* DayOfWeekShortNameType.
*
* @author Artem Henvald <genvaldartem@gmail.com>
*
* @extends AbstractEnumType<string>
*/
final class DayOfWeekShortNameType extends AbstractEnumType
{
Expand Down
2 changes: 2 additions & 0 deletions DBAL/Types/MonthFullNameType.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
* MonthFullNameType.
*
* @author Artem Henvald <genvaldartem@gmail.com>
*
* @extends AbstractEnumType<string>
*/
final class MonthFullNameType extends AbstractEnumType
{
Expand Down
2 changes: 2 additions & 0 deletions DBAL/Types/MonthShortNameType.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
* MonthShortNameType.
*
* @author Artem Henvald <genvaldartem@gmail.com>
*
* @extends AbstractEnumType<string>
*/
final class MonthShortNameType extends AbstractEnumType
{
Expand Down
4 changes: 2 additions & 2 deletions Form/EnumTypeGuesser.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function guessType(string $class, string $property): ?TypeGuess
return null;
}

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

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

/** @var AbstractEnumType $registeredEnumTypeFQCN */
/** @var AbstractEnumType<int|string> $registeredEnumTypeFQCN */
$parameters = [
'choices' => $registeredEnumTypeFQCN::getChoices(), // Get the choices from the fully qualified class name
'required' => !$metadata->isNullable($property),
Expand Down
4 changes: 4 additions & 0 deletions Tests/Fixtures/DBAL/Types/AbstractParentType.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
* AbstractParentType.
*
* @author Arturs Vonda <github@artursvonda.lv>
*
* @template T of int|string
*
* @extends AbstractEnumType<T>
*/
abstract class AbstractParentType extends AbstractEnumType
{
Expand Down
2 changes: 2 additions & 0 deletions Tests/Fixtures/DBAL/Types/BasketballPositionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
* BasketballPositionType.
*
* @author Artem Henvald <genvaldartem@gmail.com>
*
* @extends AbstractEnumType<string>
*/
final class BasketballPositionType extends AbstractEnumType
{
Expand Down
2 changes: 2 additions & 0 deletions Tests/Fixtures/DBAL/Types/InheritedType.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
* InheritedType.
*
* @author Arturs Vonda <github@artursvonda.lv>
*
* @extends AbstractParentType<string>
*/
final class InheritedType extends AbstractParentType
{
Expand Down
2 changes: 2 additions & 0 deletions Tests/Fixtures/DBAL/Types/MapLocationType.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
* MapLocationType.
*
* @author Artem Henvald <genvaldartem@gmail.com>
*
* @extends AbstractEnumType<string>
*/
final class MapLocationType extends AbstractEnumType
{
Expand Down
2 changes: 2 additions & 0 deletions Tests/Fixtures/DBAL/Types/NumericType.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
* NumericType.
*
* @author Stephan Vock <stephan.vock@gmail.com>
*
* @extends AbstractEnumType<int>
*/
final class NumericType extends AbstractEnumType
{
Expand Down
2 changes: 2 additions & 0 deletions Tests/Fixtures/DBAL/Types/StubType.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
* StubType.
*
* @author Artem Henvald <genvaldartem@gmail.com>
*
* @extends AbstractEnumType<string>
*/
final class StubType extends AbstractEnumType
{
Expand Down
2 changes: 2 additions & 0 deletions Tests/Fixtures/DBAL/Types/TaskStatusType.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
* TaskStatusType.
*
* @author Artem Henvald <genvaldartem@gmail.com>
*
* @extends AbstractEnumType<string>
*/
final class TaskStatusType extends AbstractEnumType
{
Expand Down
10 changes: 7 additions & 3 deletions Tests/Twig/Extension/ReadableEnumValueTwigExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Fresh\DoctrineEnumBundle\Exception\EnumValue\ValueIsNotFoundInAnyRegisteredEnumTypeException;
use Fresh\DoctrineEnumBundle\Tests\Fixtures\DBAL\Types\BasketballPositionType;
use Fresh\DoctrineEnumBundle\Tests\Fixtures\DBAL\Types\MapLocationType;
use Fresh\DoctrineEnumBundle\Tests\Fixtures\DBAL\Types\NumericType;
use Fresh\DoctrineEnumBundle\Twig\Extension\ReadableEnumValueTwigExtension;
use PHPUnit\Framework\TestCase;
use Twig\TwigFilter;
Expand All @@ -37,6 +38,7 @@ protected function setUp(): void
$this->readableEnumValueTwigExtension = new ReadableEnumValueTwigExtension([
'BasketballPositionType' => ['class' => BasketballPositionType::class],
'MapLocationType' => ['class' => MapLocationType::class],
'NumericType' => ['class' => NumericType::class],
]);
}

Expand All @@ -56,11 +58,11 @@ public function testGetFilters(): void
/**
* @dataProvider dataProviderForGetReadableEnumValueTest
*
* @param string|null $expectedReadableValue
* @param string|null $enumValue
* @param int|string|null $expectedReadableValue
* @param int|string|null $enumValue
* @param string|null $enumType
*/
public function testGetReadableEnumValue(?string $expectedReadableValue, ?string $enumValue, ?string $enumType): void
public function testGetReadableEnumValue($expectedReadableValue, $enumValue, ?string $enumType): void
{
self::assertEquals(
$expectedReadableValue,
Expand All @@ -75,6 +77,8 @@ public static function dataProviderForGetReadableEnumValueTest(): iterable
yield ['Center', BasketballPositionType::CENTER, 'BasketballPositionType'];
yield ['Center', MapLocationType::CENTER, 'MapLocationType'];
yield [null, null, 'MapLocationType'];
yield [1, NumericType::ONE, 'NumericType'];
yield [1, NumericType::ONE, null];
}

public function testEnumTypeIsNotRegisteredException(): void
Expand Down
4 changes: 2 additions & 2 deletions Twig/Extension/AbstractEnumTwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
*/
abstract class AbstractEnumTwigExtension extends AbstractExtension
{
/** @var string[]|AbstractEnumType[] */
/** @var string[]|AbstractEnumType<int|string>[] */
protected $registeredEnumTypes = [];

/** @var string[]|AbstractEnumType[] */
/** @var string[]|AbstractEnumType<int|string>[] */
protected $occurrences = [];

/**
Expand Down
3 changes: 2 additions & 1 deletion Twig/Extension/EnumConstantTwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace Fresh\DoctrineEnumBundle\Twig\Extension;

use Fresh\DoctrineEnumBundle\DBAL\Types\AbstractEnumType;
use Fresh\DoctrineEnumBundle\Exception\Constant\ConstantIsFoundInFewRegisteredEnumTypesException;
use Fresh\DoctrineEnumBundle\Exception\Constant\ConstantIsNotFoundInAnyRegisteredEnumTypeException;
use Fresh\DoctrineEnumBundle\Exception\EnumType\EnumTypeIsNotRegisteredException;
Expand Down Expand Up @@ -88,7 +89,7 @@ public function getEnumConstant(string $enumConstant, ?string $enumType = null):
*/
private function findOccurrences(string $enumConstant): void
{
/** @var class-string<\Fresh\DoctrineEnumBundle\DBAL\Types\AbstractEnumType> $registeredEnumType */
/** @var class-string<AbstractEnumType<int|string>> $registeredEnumType */
foreach ($this->registeredEnumTypes as $registeredEnumType) {
$reflection = new \ReflectionClass($registeredEnumType);

Expand Down
12 changes: 6 additions & 6 deletions Twig/Extension/ReadableEnumValueTwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ public function getFilters(): array
}

/**
* @param string|null $enumValue
* @param string|null $enumType
* @param int|string|null $enumValue
* @param string|null $enumType
*
* @throws EnumTypeIsNotRegisteredException
* @throws NoRegisteredEnumTypesException
* @throws ValueIsFoundInFewRegisteredEnumTypesException
* @throws ValueIsNotFoundInAnyRegisteredEnumTypeException
*
* @return string|null
* @return int|string|null
*/
public function getReadableEnumValue(?string $enumValue, ?string $enumType = null): ?string
public function getReadableEnumValue($enumValue, ?string $enumType = null)
{
if ($this->hasRegisteredEnumTypes()) {
if (null === $enumValue) {
Expand Down Expand Up @@ -91,9 +91,9 @@ public function getReadableEnumValue(?string $enumValue, ?string $enumType = nul
}

/**
* @param string $enumValue
* @param int|string $enumValue
*/
private function findOccurrences(string $enumValue): void
private function findOccurrences($enumValue): void
{
foreach ($this->registeredEnumTypes as $registeredEnumType) {
if ($registeredEnumType::isValueExist($enumValue)) {
Expand Down
4 changes: 2 additions & 2 deletions Validator/Constraints/Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*/
class Enum extends Choice
{
/** @var string|AbstractEnumType */
/** @var string|AbstractEnumType<int|string> */
public $entity;

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

if (isset($options['entity'])) {
/** @var AbstractEnumType $entity */
/** @var AbstractEnumType<int|string> $entity */
$entity = $options['entity'];

if (\is_subclass_of($entity, AbstractEnumType::class)) {
Expand Down
1 change: 0 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@ parameters:
- '%rootDir%/../../../vendor/*'
fileExtensions:
- 'php'
checkGenericClassInNonGenericObjectType: false

0 comments on commit 8c6f3fe

Please sign in to comment.