Test Failed
Push — 7.x ( bea7e1...2b11c5 )
by Adrien
08:51
created

SplReflectionEnumUnitCase   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 19
c 1
b 0
f 1
dl 0
loc 62
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getEnum() 0 3 1
A getValue() 0 20 3
A __construct() 0 7 2
1
<?php
2
3
/**
4
 * Part of SplTypes package.
5
 *
6
 * (c) Adrien Loyant <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Ducks\Component\SplTypes\Reflection;
15
16
use Ducks\Component\SplTypes\SplUnitEnum;
17
18
class SplReflectionEnumUnitCase extends \ReflectionClassConstant
19
{
20
    private static array $instances = [];
21
22
    /**
23
     * Instantiates a SplReflectionEnumUnitCase object
24
     *
25
     * @param object|string $class An enum instance or a name.
26
     * @param string $constant An enum constant name.
27
     *
28
     * @throws ReflectionException if $class is not a SplReflectionEnumUnitCase
29
     *
30
     * @link https://www.php.net/manual/en/reflectionenumunitcase.construct.php
31
     */
32
    public function __construct($class, string $constant)
33
    {
34
        parent::__construct($class, $constant);
35
36
        if (!$this->getEnum()->hasCase($constant)) {
37
            throw new \ReflectionException(
38
                'Enum case ' . $this->class . '::' . $this->name . ' is not a case'
39
            );
40
        }
41
    }
42
43
    /**
44
     * Gets the reflection of the enum of this case
45
     *
46
     * @return ReflectionEnum instance describing the Enum this case belongs to.
0 ignored issues
show
Bug introduced by
The type Ducks\Component\SplTypes\Reflection\ReflectionEnum was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
47
     *
48
     * @link https://www.php.net/manual/en/reflectionenumunitcase.getenum.php
49
     */
50
    public function getEnum(): SplReflectionEnum
51
    {
52
        return new SplReflectionEnum($this->class);
0 ignored issues
show
Bug Best Practice introduced by
The expression return new Ducks\Compone...ctionEnum($this->class) returns the type Ducks\Component\SplTypes...ction\SplReflectionEnum which is incompatible with the documented return type Ducks\Component\SplTypes\Reflection\ReflectionEnum.
Loading history...
53
    }
54
55
    /**
56
     * Gets the enum case object described by this reflection object
57
     *
58
     * @return SplUnitEnum The enum case object described by this reflection object.
59
     */
60
    public function getValue(): SplUnitEnum
61
    {
62
        if (!isset(self::$instances[$this->class][$this->name])) {
63
            $class = $this->getDeclaringClass();
64
            $instance = $class->newInstanceWithoutConstructor();
65
66
            $object = $class->getConstructor();
67
            $object->setAccessible(true);
68
            $object->invoke($instance);
69
70
            if ($class->hasProperty('name')) {
71
                $property = $class->getProperty('name');
72
                $property->setAccessible(true);
73
                $property->setValue($instance, $this->name);
74
            }
75
76
            self::$instances[$this->class][$this->name] = $instance;
77
        }
78
79
        return self::$instances[$this->class][$this->name];
80
    }
81
}
82