Skip to content

Commit

Permalink
minor #54 Fix EA extended inspections (ogizanagi)
Browse files Browse the repository at this point in the history
This PR was merged into the 1.x-dev branch.

Discussion
----------

Fix EA extended inspections

Commits
-------

3aa65c1 Fix EA extended inspections
  • Loading branch information
ogizanagi committed Apr 16, 2018
2 parents 5c487f4 + 3aa65c1 commit eeeaf12
Show file tree
Hide file tree
Showing 20 changed files with 64 additions and 62 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -363,16 +363,16 @@ The default implementation compares both enum type (the class) and value.

```php
<?php
Gender::get(Gender::MALE)->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
<?php
Gender::get(Gender::MALE)->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
Expand Down
6 changes: 3 additions & 3 deletions src/AutoDiscoveredValuesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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__,
Expand Down Expand Up @@ -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);
});
}

Expand Down
12 changes: 6 additions & 6 deletions src/Bridge/Faker/Provider/EnumProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
));
}

Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
));
}

Expand All @@ -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);
}

Expand All @@ -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();
Expand Down
1 change: 1 addition & 0 deletions src/Bridge/Symfony/Form/Type/EnumType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
8 changes: 4 additions & 4 deletions src/Bridge/Symfony/Validator/Constraint/Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,25 +49,25 @@ 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),
$this->class
));
}

$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'];
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"',
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Exception/ExceptionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@

namespace Elao\Enum\Exception;

interface ExceptionInterface
interface ExceptionInterface extends \Throwable
{
}
2 changes: 1 addition & 1 deletion src/Exception/InvalidValueException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
2 changes: 1 addition & 1 deletion src/FlaggedEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/ReadableEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ public function getReadable(): string
*/
public function __toString()
{
return (string) $this->getReadable();
return $this->getReadable();
}
}
2 changes: 0 additions & 2 deletions tests/Integration/Bridge/Doctrine/DBAL/Type/EnumTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down Expand Up @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}

Expand Down
Loading

0 comments on commit eeeaf12

Please sign in to comment.