diff --git a/README.md b/README.md index 8a19bb55..9fdb65e6 100644 --- a/README.md +++ b/README.md @@ -363,16 +363,16 @@ The default implementation compares both enum type (the class) and value. ```php equals(Gender::get(Gender::FEMALE)) // False -Gender::get(Gender::MALE)->equals(Gender::get(Gender::MALE)) // True +Gender::get(Gender::MALE)->equals(Gender::get(Gender::FEMALE)); // False +Gender::get(Gender::MALE)->equals(Gender::get(Gender::MALE)); // True ``` Lastly, you can simply compare an instance with a value by using the `EnumInterface::is($value)`: ```php is(Gender::FEMALE) // False -Gender::get(Gender::MALE)->is(Gender::MALE) // True +Gender::get(Gender::MALE)->is(Gender::FEMALE); // False +Gender::get(Gender::MALE)->is(Gender::MALE); // True ``` ## Shortcuts diff --git a/src/AutoDiscoveredValuesTrait.php b/src/AutoDiscoveredValuesTrait.php index dd88cc16..4f23973e 100644 --- a/src/AutoDiscoveredValuesTrait.php +++ b/src/AutoDiscoveredValuesTrait.php @@ -38,7 +38,7 @@ public static function values(): array */ protected static function choices(): array { - if (!in_array(ChoiceEnumTrait::class, class_uses(self::class, false), true)) { + if (!\in_array(ChoiceEnumTrait::class, class_uses(self::class, false), true)) { throw new LogicException(sprintf( 'Method "%s" is only meant to be used when using the "%s" trait which is not used in "%s"', __METHOD__, @@ -69,11 +69,11 @@ private static function autodiscoveredValues(): array if (is_a($enumType, FlaggedEnum::class, true)) { $values = array_filter($values, function ($v) { - return is_int($v) && 0 === ($v & $v - 1) && $v > 0; + return \is_int($v) && 0 === ($v & $v - 1) && $v > 0; }); } else { $values = array_filter($values, function ($v) { - return is_int($v) || is_string($v); + return \is_int($v) || \is_string($v); }); } diff --git a/src/Bridge/Faker/Provider/EnumProvider.php b/src/Bridge/Faker/Provider/EnumProvider.php index 514ae35f..2f71b4e9 100644 --- a/src/Bridge/Faker/Provider/EnumProvider.php +++ b/src/Bridge/Faker/Provider/EnumProvider.php @@ -55,23 +55,23 @@ public function enum(string $enumValueShortcut): EnumInterface { list($enumClassOrAlias, $constants) = explode('::', $enumValueShortcut); - /** @var EnumInterface $class */ + /** @var EnumInterface|string $class */ $class = $this->enumMapping[$enumClassOrAlias] ?? $enumClassOrAlias; $this->ensureEnumClass($class); $constants = explode('|', $constants); // Flagged Enum if $constants count is greater than one: - if (count($constants) > 1) { + if (\count($constants) > 1) { if (!is_a($class, FlaggedEnum::class, true)) { throw new InvalidArgumentException("$class is not a valid FlaggedEnum"); } $value = 0; foreach ($constants as $constant) { - $value |= constant($class . '::' . $constant); + $value |= \constant($class . '::' . $constant); } } else { - $value = constant($class . '::' . current($constants)); + $value = \constant($class . '::' . current($constants)); } return $class::get($value); @@ -86,12 +86,12 @@ public function enum(string $enumValueShortcut): EnumInterface */ public function randomEnum(string $enumClassOrAlias): EnumInterface { - /** @var EnumInterface $class */ + /** @var EnumInterface|string $class */ $class = $this->enumMapping[$enumClassOrAlias] ?? $enumClassOrAlias; $this->ensureEnumClass($class); $instances = $class::instances(); - $randomRank = mt_rand(0, count($instances) - 1); + $randomRank = random_int(0, \count($instances) - 1); return $instances[$randomRank]; } diff --git a/src/Bridge/Symfony/Form/DataTransformer/AbstractFlagEnumTransformer.php b/src/Bridge/Symfony/Form/DataTransformer/AbstractFlagEnumTransformer.php index 63a9e240..cb3f2e2e 100644 --- a/src/Bridge/Symfony/Form/DataTransformer/AbstractFlagEnumTransformer.php +++ b/src/Bridge/Symfony/Form/DataTransformer/AbstractFlagEnumTransformer.php @@ -34,11 +34,11 @@ public function __construct(string $enumClass) protected function createEnum(int $value): FlaggedEnum { - return call_user_func([$this->enumClass, 'get'], $value); + return \call_user_func([$this->enumClass, 'get'], $value); } protected function isAcceptableValueForEnum(int $value): bool { - return call_user_func([$this->enumClass, 'accepts'], $value); + return \call_user_func([$this->enumClass, 'accepts'], $value); } } diff --git a/src/Bridge/Symfony/Form/DataTransformer/BitmaskToBitFlagsValueTransformer.php b/src/Bridge/Symfony/Form/DataTransformer/BitmaskToBitFlagsValueTransformer.php index 62c71963..7a67f2bb 100644 --- a/src/Bridge/Symfony/Form/DataTransformer/BitmaskToBitFlagsValueTransformer.php +++ b/src/Bridge/Symfony/Form/DataTransformer/BitmaskToBitFlagsValueTransformer.php @@ -30,10 +30,10 @@ public function transform($bitmask) return null; } - if (!is_int($bitmask)) { + if (!\is_int($bitmask)) { throw new TransformationFailedException(sprintf( 'Expected integer. Got "%s".', - is_object($bitmask) ? get_class($bitmask) : gettype($bitmask) + \is_object($bitmask) ? \get_class($bitmask) : \gettype($bitmask) )); } @@ -63,23 +63,23 @@ public function reverseTransform($flags) return null; } - if (!is_array($flags)) { + if (!\is_array($flags)) { throw new TransformationFailedException(sprintf( 'Expected array. Got "%s".', - is_object($flags) ? get_class($flags) : gettype($flags) + \is_object($flags) ? \get_class($flags) : \gettype($flags) )); } - if (0 === count($flags)) { + if (0 === \count($flags)) { return FlaggedEnum::NONE; } $bitmask = 0; foreach ($flags as $flag) { - if (!is_int($flag)) { + if (!\is_int($flag)) { throw new TransformationFailedException(sprintf( 'Expected array of integers. Got a "%s" inside.', - is_object($flag) ? get_class($flag) : gettype($flag) + \is_object($flag) ? \get_class($flag) : \gettype($flag) )); } $bitmask |= $flag; diff --git a/src/Bridge/Symfony/Form/DataTransformer/ScalarToEnumTransformer.php b/src/Bridge/Symfony/Form/DataTransformer/ScalarToEnumTransformer.php index e6bbbf6e..3e2602eb 100644 --- a/src/Bridge/Symfony/Form/DataTransformer/ScalarToEnumTransformer.php +++ b/src/Bridge/Symfony/Form/DataTransformer/ScalarToEnumTransformer.php @@ -53,7 +53,7 @@ public function transform($value) throw new TransformationFailedException(sprintf( 'Expected instance of "%s". Got "%s".', $this->enumClass, - is_object($value) ? get_class($value) : gettype($value) + \is_object($value) ? \get_class($value) : \gettype($value) )); } diff --git a/src/Bridge/Symfony/Form/DataTransformer/SingleToCollectionFlagEnumTransformer.php b/src/Bridge/Symfony/Form/DataTransformer/SingleToCollectionFlagEnumTransformer.php index 0ac1b097..e4620a23 100644 --- a/src/Bridge/Symfony/Form/DataTransformer/SingleToCollectionFlagEnumTransformer.php +++ b/src/Bridge/Symfony/Form/DataTransformer/SingleToCollectionFlagEnumTransformer.php @@ -34,7 +34,7 @@ public function transform($value) throw new TransformationFailedException(sprintf( 'Expected instance of "%s". Got "%s".', $this->enumClass, - is_object($value) ? get_class($value) : gettype($value) + \is_object($value) ? \get_class($value) : \gettype($value) )); } @@ -58,14 +58,14 @@ public function reverseTransform($values) return null; } - if (!is_array($values)) { + if (!\is_array($values)) { throw new TransformationFailedException(sprintf( 'Expected array. Got "%s".', - is_object($values) ? get_class($values) : gettype($values) + \is_object($values) ? \get_class($values) : \gettype($values) )); } - if (0 === count($values)) { + if (0 === \count($values)) { return $this->createEnum(FlaggedEnum::NONE); } @@ -75,7 +75,7 @@ public function reverseTransform($values) throw new TransformationFailedException(sprintf( 'Expected array of "%s". Got a "%s" inside.', $this->enumClass, - is_object($value) ? get_class($value) : gettype($value) + \is_object($value) ? \get_class($value) : \gettype($value) )); } $rawValue |= $value->getValue(); diff --git a/src/Bridge/Symfony/Form/Type/EnumType.php b/src/Bridge/Symfony/Form/Type/EnumType.php index 5a3a5ed7..f8d05031 100644 --- a/src/Bridge/Symfony/Form/Type/EnumType.php +++ b/src/Bridge/Symfony/Form/Type/EnumType.php @@ -28,6 +28,7 @@ public function configureOptions(OptionsResolver $resolver) $resolver ->setDefaults([ 'choices' => function (Options $options) { + /** @var EnumInterface|ReadableEnumInterface|string $enumClass */ $enumClass = $options['enum_class']; if (!$options['as_value']) { diff --git a/src/Bridge/Symfony/Serializer/Normalizer/EnumNormalizer.php b/src/Bridge/Symfony/Serializer/Normalizer/EnumNormalizer.php index 4de790ef..53ce52bc 100644 --- a/src/Bridge/Symfony/Serializer/Normalizer/EnumNormalizer.php +++ b/src/Bridge/Symfony/Serializer/Normalizer/EnumNormalizer.php @@ -42,7 +42,7 @@ public function supportsNormalization($data, $format = null) public function denormalize($data, $class, $format = null, array $context = []) { try { - return call_user_func([$class, 'get'], $data); + return \call_user_func([$class, 'get'], $data); } catch (InvalidValueException $e) { throw new UnexpectedValueException($e->getMessage()); } diff --git a/src/Bridge/Symfony/Validator/Constraint/Enum.php b/src/Bridge/Symfony/Validator/Constraint/Enum.php index afa3acae..0e186221 100644 --- a/src/Bridge/Symfony/Validator/Constraint/Enum.php +++ b/src/Bridge/Symfony/Validator/Constraint/Enum.php @@ -49,10 +49,10 @@ public function __construct($options) } // Normalize choices - if (is_array($this->choices)) { + if (\is_array($this->choices)) { $choices = []; foreach ($this->choices as $choiceValue) { - if (false === call_user_func([$this->class, 'accepts'], $choiceValue)) { + if (false === \call_user_func([$this->class, 'accepts'], $choiceValue)) { throw new ConstraintDefinitionException(sprintf( 'Choice %s is not a valid value for enum type "%s".', json_encode($choiceValue), @@ -60,14 +60,14 @@ public function __construct($options) )); } - $choices[] = $this->asValue ? $choiceValue : call_user_func([$this->class, 'get'], $choiceValue); + $choices[] = $this->asValue ? $choiceValue : \call_user_func([$this->class, 'get'], $choiceValue); } $this->choices = $choices; } // only set the callback if no choice list set - if (!is_array($this->choices)) { + if (!\is_array($this->choices)) { $this->callback = $this->asValue ? [$this->class, 'values'] : [$this->class, 'instances']; } } diff --git a/src/Enum.php b/src/Enum.php index 9801764a..9317a4f0 100644 --- a/src/Enum.php +++ b/src/Enum.php @@ -83,7 +83,7 @@ public static function get($value): EnumInterface */ public static function __callStatic($name, $arguments = []): EnumInterface { - $value = @constant('static::' . $name); + $value = @\constant('static::' . $name); if (null === $value) { throw new \BadMethodCallException(sprintf( 'No constant named "%s" exists in class "%s"', @@ -100,7 +100,7 @@ public static function __callStatic($name, $arguments = []): EnumInterface */ public static function accepts($value): bool { - return in_array($value, static::values(), true); + return \in_array($value, static::values(), true); } /** @@ -134,7 +134,7 @@ public function getValue() */ public function equals(EnumInterface $enum): bool { - return get_class($this) === get_class($enum) && $this->value === $enum->getValue(); + return \get_class($this) === \get_class($enum) && $this->value === $enum->getValue(); } /** diff --git a/src/Exception/ExceptionInterface.php b/src/Exception/ExceptionInterface.php index 6d365a9a..3d631a0d 100644 --- a/src/Exception/ExceptionInterface.php +++ b/src/Exception/ExceptionInterface.php @@ -10,6 +10,6 @@ namespace Elao\Enum\Exception; -interface ExceptionInterface +interface ExceptionInterface extends \Throwable { } diff --git a/src/Exception/InvalidValueException.php b/src/Exception/InvalidValueException.php index 1abd0abf..b58676e7 100644 --- a/src/Exception/InvalidValueException.php +++ b/src/Exception/InvalidValueException.php @@ -13,7 +13,7 @@ /** * Exception used when providing an invalid value for a given enumeration class. */ -class InvalidValueException extends InvalidArgumentException implements ExceptionInterface +class InvalidValueException extends InvalidArgumentException { public function __construct($value, $class) { diff --git a/src/FlaggedEnum.php b/src/FlaggedEnum.php index 53a3a209..aac8c3b9 100644 --- a/src/FlaggedEnum.php +++ b/src/FlaggedEnum.php @@ -31,7 +31,7 @@ abstract class FlaggedEnum extends ReadableEnum */ public static function accepts($value): bool { - if (!is_int($value)) { + if (!\is_int($value)) { return false; } diff --git a/src/ReadableEnum.php b/src/ReadableEnum.php index 2d6401c6..e1932f5e 100644 --- a/src/ReadableEnum.php +++ b/src/ReadableEnum.php @@ -40,6 +40,6 @@ public function getReadable(): string */ public function __toString() { - return (string) $this->getReadable(); + return $this->getReadable(); } } diff --git a/tests/Integration/Bridge/Doctrine/DBAL/Type/EnumTypeTest.php b/tests/Integration/Bridge/Doctrine/DBAL/Type/EnumTypeTest.php index 62992046..61c21440 100644 --- a/tests/Integration/Bridge/Doctrine/DBAL/Type/EnumTypeTest.php +++ b/tests/Integration/Bridge/Doctrine/DBAL/Type/EnumTypeTest.php @@ -46,7 +46,6 @@ public function testEnumType() $user = $this->em->find(User::class, $uuid); - $this->assertInstanceOf(Gender::class, $user->getGender()); $this->assertTrue($user->getGender()->is(Gender::MALE)); } @@ -74,7 +73,6 @@ public function testEnumTypeOnNullFromDatabase() $user = $this->em->find(User::class, $uuid); - $this->assertInstanceOf(Gender::class, $user->getGender()); $this->assertTrue($user->getGender()->is(Gender::UNKNOW)); } } diff --git a/tests/Unit/Bridge/Symfony/Serializer/Normalizer/EnumNormalizerTest.php b/tests/Unit/Bridge/Symfony/Serializer/Normalizer/EnumNormalizerTest.php index a4c2a0e9..2c3274bf 100644 --- a/tests/Unit/Bridge/Symfony/Serializer/Normalizer/EnumNormalizerTest.php +++ b/tests/Unit/Bridge/Symfony/Serializer/Normalizer/EnumNormalizerTest.php @@ -36,14 +36,14 @@ public function testsNormalize() public function testSupportsDenormalization() { - $this->assertTrue($this->normalizer->supportsDenormalization((string) Gender::MALE, Gender::class)); + $this->assertTrue($this->normalizer->supportsDenormalization(Gender::MALE, Gender::class)); } public function testsDenormalize() { $this->assertSame( Gender::get(Gender::MALE), - $this->normalizer->denormalize((string) Gender::MALE, Gender::class) + $this->normalizer->denormalize(Gender::MALE, Gender::class) ); } diff --git a/tests/Unit/Bridge/Symfony/Validator/Constraint/ConstraintValidatorTestCase.php b/tests/Unit/Bridge/Symfony/Validator/Constraint/ConstraintValidatorTestCase.php index 3ecd8904..f6181497 100644 --- a/tests/Unit/Bridge/Symfony/Validator/Constraint/ConstraintValidatorTestCase.php +++ b/tests/Unit/Bridge/Symfony/Validator/Constraint/ConstraintValidatorTestCase.php @@ -12,14 +12,18 @@ use PHPUnit\Framework\Assert; use PHPUnit\Framework\TestCase; +use Symfony\Component\Translation\TranslatorInterface; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\Constraints\NotNull; +use Symfony\Component\Validator\Constraints\Valid; use Symfony\Component\Validator\ConstraintValidatorInterface; use Symfony\Component\Validator\ConstraintViolation; use Symfony\Component\Validator\Context\ExecutionContext; use Symfony\Component\Validator\Context\ExecutionContextInterface; use Symfony\Component\Validator\Mapping\ClassMetadata; use Symfony\Component\Validator\Mapping\PropertyMetadata; +use Symfony\Component\Validator\Validator\ContextualValidatorInterface; +use Symfony\Component\Validator\Validator\ValidatorInterface; /** * A test case to ease testing Constraint Validators. @@ -105,9 +109,9 @@ protected function restoreDefaultTimezone() protected function createContext() { - $translator = $this->getMockBuilder('Symfony\Component\Translation\TranslatorInterface')->getMock(); - $validator = $this->getMockBuilder('Symfony\Component\Validator\Validator\ValidatorInterface')->getMock(); - $contextualValidator = $this->getMockBuilder('Symfony\Component\Validator\Validator\ContextualValidatorInterface')->getMock(); + $translator = $this->getMockBuilder(TranslatorInterface::class)->getMock(); + $validator = $this->getMockBuilder(ValidatorInterface::class)->getMock(); + $contextualValidator = $this->getMockBuilder(ContextualValidatorInterface::class)->getMock(); $context = new ExecutionContext($validator, $this->root, $translator); $context->setGroup($this->group); @@ -131,8 +135,8 @@ protected function setGroup($group) protected function setObject($object) { $this->object = $object; - $this->metadata = is_object($object) - ? new ClassMetadata(get_class($object)) + $this->metadata = \is_object($object) + ? new ClassMetadata(\get_class($object)) : null; $this->context->setNode($this->value, $this->object, $this->metadata, $this->propertyPath); @@ -141,8 +145,8 @@ protected function setObject($object) protected function setProperty($object, $property) { $this->object = $object; - $this->metadata = is_object($object) - ? new PropertyMetadata(get_class($object), $property) + $this->metadata = \is_object($object) + ? new PropertyMetadata(\get_class($object), $property) : null; $this->context->setNode($this->value, $this->object, $this->metadata, $this->propertyPath); @@ -185,7 +189,7 @@ protected function expectValidateAt($i, $propertyPath, $value, $group) ->will($this->returnValue($validator)); $validator->expects($this->at(2 * $i + 1)) ->method('validate') - ->with($value, $this->logicalOr(null, [], $this->isInstanceOf('\Symfony\Component\Validator\Constraints\Valid')), $group); + ->with($value, $this->logicalOr(null, [], $this->isInstanceOf(Valid::class)), $group); } protected function expectValidateValueAt($i, $propertyPath, $value, $constraints, $group = null) @@ -202,7 +206,7 @@ protected function expectValidateValueAt($i, $propertyPath, $value, $constraints protected function assertNoViolation() { - $this->assertSame(0, $violationsCount = count($this->context->getViolations()), sprintf('0 violation expected. Got %u.', $violationsCount)); + $this->assertSame(0, $violationsCount = \count($this->context->getViolations()), sprintf('0 violation expected. Got %u.', $violationsCount)); } /** @@ -325,7 +329,7 @@ public function assertRaised() $violations = iterator_to_array($this->context->getViolations()); - Assert::assertSame($expectedCount = count($expected), $violationsCount = count($violations), sprintf('%u violation(s) expected. Got %u.', $expectedCount, $violationsCount)); + Assert::assertSame($expectedCount = \count($expected), $violationsCount = \count($violations), sprintf('%u violation(s) expected. Got %u.', $expectedCount, $violationsCount)); reset($violations); diff --git a/tests/Unit/Bridge/Symfony/VarDumper/Caster/EnumCasterTest.php b/tests/Unit/Bridge/Symfony/VarDumper/Caster/EnumCasterTest.php index 6bb4dd68..59dcee6e 100644 --- a/tests/Unit/Bridge/Symfony/VarDumper/Caster/EnumCasterTest.php +++ b/tests/Unit/Bridge/Symfony/VarDumper/Caster/EnumCasterTest.php @@ -159,7 +159,7 @@ private function dumpAsHtml($value): string // Once symfony/var-dumper < 3.1 support is dropped, the light array flag can be used // and StringMatches assertions removed in favor of Identical/Equals assertions - $flags = defined(AbstractDumper::class . '::DUMP_LIGHT_ARRAY') ? AbstractDumper::DUMP_LIGHT_ARRAY : null; + $flags = \defined(AbstractDumper::class . '::DUMP_LIGHT_ARRAY') ? AbstractDumper::DUMP_LIGHT_ARRAY : null; $dumper = new HtmlDumper(null, null, $flags); $configure($dumper); diff --git a/tests/Unit/FlaggedEnumTest.php b/tests/Unit/FlaggedEnumTest.php index 27b27827..2a82a48c 100644 --- a/tests/Unit/FlaggedEnumTest.php +++ b/tests/Unit/FlaggedEnumTest.php @@ -63,14 +63,13 @@ public function testThrowExceptionWhenBitmaskIsInvalid() public function testSameEnumValueActsAsSingleton() { - $this->assertTrue(Permissions::get(Permissions::NONE) === Permissions::get(Permissions::NONE)); - $this->assertTrue(Permissions::get(Permissions::READ) === Permissions::get(Permissions::READ)); + $this->assertSame(Permissions::get(Permissions::NONE), Permissions::get(Permissions::NONE)); + $this->assertSame(Permissions::get(Permissions::READ), Permissions::get(Permissions::READ)); $all = Permissions::get(Permissions::ALL); - $this->assertTrue($all === Permissions::get(Permissions::READ | Permissions::WRITE | Permissions::EXECUTE)); - $this->assertTrue( - $all->withoutFlags(Permissions::READ) === Permissions::get( - Permissions::WRITE | Permissions::EXECUTE - ) + $this->assertSame($all, Permissions::get(Permissions::READ | Permissions::WRITE | Permissions::EXECUTE)); + $this->assertSame( + $all->withoutFlags(Permissions::READ), + Permissions::get(Permissions::WRITE | Permissions::EXECUTE) ); }