SplEnumTrait::__callStatic()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 6
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 11
ccs 0
cts 5
cp 0
crap 6
rs 10
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
namespace Ducks\Component\SplTypes;
13
14
/**
15
 * Trait used for enum emulation
16
 *
17
 * @phpstan-require-extends SplEnum
18
 *
19
 * @psalm-api
20
 */
21
trait SplEnumTrait
22
{
23
    /**
24
     * Return a new instance of enum
25
     *
26
     * @param string $name
27
     * @param mixed[] $arguments
28
     *
29
     * @return static
30
     *
31
     * @throws \Error if $name is not a case
32
     *
33
     * @phpstan-param string $name
34
     * @phpstan-param list<mixed> $arguments
35
     * @phpstan-return static
36
     * @phpstan-ignore-next-line
37
     *
38
     * @psalm-suppress UnsafeInstantiation
39
     */
40
    #[\ReturnTypeWillChange]
41
    public static function __callStatic(string $name, array $arguments)
42
    {
43
        try {
44
            $class = new \ReflectionClassConstant(static::class, $name);
45
        } catch (\ReflectionException $th) {
46
            throw new \Error('Undefined constant ' . static::class . '::' . $name);
47
        }
48
49
        // @phpstan-ignore-next-line
50
        return new static($class->getValue());
0 ignored issues
show
Unused Code introduced by
The call to Ducks\Component\SplTypes...numTrait::__construct() has too many arguments starting with $class->getValue(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

50
        return /** @scrutinizer ignore-call */ new static($class->getValue());

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
51
    }
52
}
53