Failed Conditions
Pull Request — master (#6)
by Laurens
01:41
created

src/AbstractEnum.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Werkspot\Enum;
6
7
use BadMethodCallException;
8
use Doctrine\Common\Inflector\Inflector;
9
use InvalidArgumentException;
10
use ReflectionClass;
11
use Werkspot\Enum\Util\ClassNameConverter;
12
13
abstract class AbstractEnum
14
{
15
    protected $value;
16
17 15
    protected function __construct($value)
18
    {
19 15
        if (!$this->isValid($value)) {
20 4
            $message = 'Value [%s] is not matching any valid value of class "%s". Valid values are [%s].';
21
22 4
            throw new InvalidArgumentException(sprintf(
23 4
                $message,
24 4
                $value,
25 4
                $this->getClassName(),
26 4
                self::getValidOptionsAsString()
27
            ));
28
        }
29
30 11
        $this->value = $value;
31 11
    }
32
33
    /**
34
     * @return static
35
     */
36 13
    public static function get($value): self
37
    {
38 13
        return new static($value);
39
    }
40
41 3 View Code Duplication
    public static function __callStatic(string $methodName, array $arguments): self
1 ignored issue
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
42
    {
43 3
        foreach (self::getConstants() as $option => $value) {
44 3
            $expectedMethodName = lcfirst(Inflector::classify(strtolower($option)));
45 3
            if ($expectedMethodName === $methodName) {
46 2
                return new static($value);
47
            }
48
        }
49
50 1
        throw new BadMethodCallException(sprintf('%s::%s() does not exist', static::class, $methodName));
51
    }
52
53 3 View Code Duplication
    public function __call(string $methodName, array $arguments)
1 ignored issue
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
54
    {
55 3
        foreach (self::getConstants() as $option => $value) {
56 3
            $isaMethodName = 'is' . ucfirst(Inflector::classify(strtolower($option)));
57 3
            if ($isaMethodName === $methodName) {
58 2
                return $this->equals(static::get($value));
1 ignored issue
show
static::get($value) is of type this<Werkspot\Enum\AbstractEnum>, but the function expects a object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
59
            }
60
        }
61 1
        throw new BadMethodCallException(sprintf('%s::%s() does not exist', static::class, $methodName));
62
    }
63
64
65
    /**
66
     * @return mixed
67
     */
68 8
    public function getValue()
69
    {
70 8
        return $this->value;
71
    }
72
73 6
    public function equals(self $other): bool
74
    {
75 6
        return get_class($other) === get_class($this) && $other->getValue() === $this->value;
76
    }
77
78 3
    public function __toString(): string
79
    {
80 3
        return (string) $this->value;
81
    }
82
83 15
    protected function isValid($value): bool
84
    {
85 15
        return in_array($value, self::getValidOptions(), true);
86
    }
87
88 16
    public static function getValidOptions(): array
89
    {
90 16
        return array_values(self::getConstants());
91
    }
92
93 4
    protected function getClassName(): string
94
    {
95 4
        return ClassNameConverter::stripNameSpace(static::class);
96
    }
97
98 17
    private static function getConstants(): array
99
    {
100 17
        return (new ReflectionClass(static::class))->getConstants();
101
    }
102
103 4
    private static function getValidOptionsAsString(): string
104
    {
105 4
        return implode(
106 4
            ', ',
107 4
            array_map(function ($option) {
108 4
                return var_export($option, true);
109 4
            }, self::getValidOptions())
110
        );
111
    }
112
}
113