Skip to content

Commit

Permalink
minor #148 Improve PHPStan and Psalm annotations (B-Galati)
Browse files Browse the repository at this point in the history
This PR was merged into the 1.x-dev branch.

Discussion
----------

Improve PHPStan and Psalm annotations

Hello!

I just upgrade the lib and notice some false positive with PHPStan analysis due to missing annotations.

So here are my fixes. Let me know if it's correct or not :-)

Commits
-------

4006590 Improve PHPStan and Psalm annotations
  • Loading branch information
ogizanagi committed Aug 17, 2021
2 parents c533ec1 + 4006590 commit 2727f3c
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 32 deletions.
6 changes: 3 additions & 3 deletions src/AutoDiscoveredValuesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
use Elao\Enum\Exception\LogicException;

/**
* @template T of int|string
*
* Auto-discover enumerated values from public constants.
*/
trait AutoDiscoveredValuesTrait
Expand All @@ -24,12 +26,10 @@ trait AutoDiscoveredValuesTrait
private static $guessedReadables = [];

/**
* @template T of int|string
*
* @see EnumInterface::values()
*
* @return int[]|string[]
* @psalm-return list<T>
* @psalm-return T[]
*/
public static function values(): array
{
Expand Down
10 changes: 7 additions & 3 deletions src/Bridge/Doctrine/DBAL/Types/AbstractEnumType.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@
use Doctrine\DBAL\Types\Type;
use Elao\Enum\EnumInterface;

/**
* @template T of EnumInterface
*/
abstract class AbstractEnumType extends Type
{
/**
* The enum FQCN for which we should make the DBAL conversion.
*
* @return class-string<EnumInterface>
* @return string&EnumInterface
* @psalm-return class-string<T>
*/
abstract protected function getEnumClass(): string;

Expand All @@ -46,7 +50,8 @@ protected function onNullFromPhp()
/**
* {@inheritdoc}
*
* @param EnumInterface $value
* @param EnumInterface|null $value
* @psalm-param T|null $value
*/
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
Expand All @@ -66,7 +71,6 @@ public function convertToPHPValue($value, AbstractPlatform $platform)
return $this->onNullFromDatabase();
}

/** @var string|EnumInterface $class */
$class = $this->getEnumClass();

return $class::get($this->cast($value));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function convertToDatabaseValue($value)
/**
* {@inheritdoc}
*
* @phpstan-return array<TEnum>|null
* @psalm-return array<TEnum>|null
*/
public function convertToPHPValue($value)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Bridge/Doctrine/ODM/Types/AbstractEnumType.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function convertToDatabaseValue($value)
/**
* {@inheritdoc}
*
* @phpstan-return TEnum
* @psalm-return TEnum
*/
public function convertToPHPValue($value): ?EnumInterface
{
Expand Down
12 changes: 3 additions & 9 deletions src/ChoiceEnumTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
use Elao\Enum\Exception\LogicException;

/**
* @template T of int|string
*
* Discover readable enumerated values by returning the enumerated values as keys and their labels as values
* in {@link \Elao\Enum\ChoiceEnumTrait::choices()}, replacing the need to provide both:
* - {@link \Elao\Enum\ReadableEnumInterface::readables()}
Expand All @@ -25,10 +27,8 @@ trait ChoiceEnumTrait
/**
* @see EnumInterface::values()
*
* @template T of int|string
*
* @return int[]|string[]
* @psalm-return list<T>
* @psalm-return T[]
*/
public static function values(): array
{
Expand All @@ -48,10 +48,7 @@ public static function values(): array
/**
* @see ReadableEnumInterface::readables()
*
* @template T of int|string
*
* @return array<int|string, string> labels indexed by enumerated value
*
* @psalm-return array<T, string>
*/
public static function readables(): array
Expand All @@ -62,10 +59,7 @@ public static function readables(): array
}

/**
* @template T of int|string
*
* @return array<int|string, string> The enumerated values as keys and their labels as values.
*
* @psalm-return array<T, string>
*/
abstract protected static function choices(): array;
Expand Down
4 changes: 3 additions & 1 deletion src/Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ abstract class Enum implements EnumInterface
* This means you'll always get the exact same instance for a same enum value.
*
* @var EnumInterface<int|string>[]
* @psalm-var EnumInterface<T>[]
*/
private static $instances;

Expand All @@ -37,7 +38,8 @@ abstract class Enum implements EnumInterface
/**
* The constructor is private and cannot be overridden: use the static get method instead.
*
* @param mixed $value The raw value of an enumeration
* @param int|string $value The raw value of an enumeration
* @psalm-param T $value
*/
final private function __construct($value)
{
Expand Down
14 changes: 5 additions & 9 deletions src/EnumInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
interface EnumInterface
{
/**
* @template T of int|string
*
* @param int|string $value The value of a particular enumerated constant
* @psalm-param T $value
*
Expand All @@ -32,18 +30,14 @@ public static function get($value): self;
/**
* Returns any possible value for the enumeration.
*
* @template T of int|string
*
* @return list<int|string>
* @psalm-return list<T>
* @return int[]|string[]
* @psalm-return T[]
*/
public static function values(): array;

/**
* @template T of int|string
*
* @param int|string $value
* @psalm-param T $value
* @psalm-param T $value
*
* @return bool True if the value is acceptable for this enumeration
*/
Expand All @@ -68,13 +62,15 @@ public function getValue();
* Determines whether two enumerations instances should be considered the same.
*
* @param EnumInterface<int|string> $enum An enum object to compare with this instance
* @psalm-param EnumInterface<T> $enum
*/
public function equals(EnumInterface $enum): bool;

/**
* Determines if the enumeration instance value is equal to the given value.
*
* @param int|string $value
* @psalm-param T $value
*/
public function is($value): bool;
}
6 changes: 1 addition & 5 deletions src/ReadableEnumInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@

/**
* @template T of int|string
* @implements EnumInterface<T>
* @extends EnumInterface<T>
*/
interface ReadableEnumInterface extends EnumInterface, Stringable
{
/**
* Gets an array of the human representations indexed by possible values.
*
* @template T of int|string
*
* @return string[] labels indexed by enumerated value
* @psalm-return array<T, string>
*/
Expand All @@ -32,8 +30,6 @@ public static function readables(): array;
/**
* Gets the human representation for a given value.
*
* @template T of int|string
*
* @param int|string $value The value of a particular enumerated constant
* @psalm-param T $value
*
Expand Down
2 changes: 2 additions & 0 deletions src/SimpleChoiceEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
*/
abstract class SimpleChoiceEnum extends ReadableEnum
{
/* @use AutoDiscoveredValuesTrait<T> */
use AutoDiscoveredValuesTrait;
/* @use ChoiceEnumTrait<T> */
use ChoiceEnumTrait {
ChoiceEnumTrait::values insteadof AutoDiscoveredValuesTrait;
}
Expand Down

0 comments on commit 2727f3c

Please sign in to comment.