Passed
Push — master ( c5cbd6...dd161a )
by Pierre
02:09
created

Constant   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 19
c 4
b 0
f 0
dl 0
loc 51
ccs 16
cts 16
cp 1
rs 10
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A supports() 0 21 5
A transform() 0 8 2
A extract() 0 10 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Knp\DictionaryBundle\ValueTransformer;
6
7
use Exception;
8
use Knp\DictionaryBundle\ValueTransformer;
9
use ReflectionClass;
10
11
final class Constant implements ValueTransformer
12
{
13
    private const PATTERN = '/^(?P<class>.*)::(?P<constant>.*)$/';
14
15
    public function supports($value): bool
16
    {
17 5
        if (!\is_string($value)) {
18
            return false;
19 5
        }
20 1
21
        $matches = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $matches is dead and can be removed.
Loading history...
22
23 4
        if (null === $matches = $this->extract($value)) {
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $matches is correct as $this->extract($value) targeting Knp\DictionaryBundle\Val...mer\Constant::extract() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
introduced by
The condition null === $matches = $this->extract($value) is always true.
Loading history...
24
            return false;
25 4
        }
26 1
27
        if (!class_exists($matches['class']) && !interface_exists($matches['class'])) {
28
            return false;
29 3
        }
30 1
31
        $constants = (new ReflectionClass($matches['class']))
32
            ->getConstants()
33 2
        ;
34 2
35
        return \array_key_exists($matches['constant'], $constants);
36
    }
37 2
38
    public function transform($value)
39
    {
40 1
        if (null === $matches = $this->extract($value)) {
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $matches is correct as $this->extract($value) targeting Knp\DictionaryBundle\Val...mer\Constant::extract() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
introduced by
The condition null === $matches = $this->extract($value) is always true.
Loading history...
41
            throw new Exception("Unable to resolve constant {$value}.");
42 1
        }
43
44 1
        return (new ReflectionClass($matches['class']))
45
            ->getConstant($matches['constant'])
46 1
        ;
47 1
    }
48
49
    /**
50
     * @return ?array{class: class-string, constant: string}
0 ignored issues
show
Documentation Bug introduced by
The doc comment ?array{class: class-string, constant: string} at position 4 could not be parsed: Unknown type name 'class-string' at position 4 in ?array{class: class-string, constant: string}.
Loading history...
51
     */
52
    private function extract(string $value): ?array
53
    {
54
        if (preg_match(self::PATTERN, $value, $matches)) {
55
            /**
56
             * @var array{class: class-string, constant: string} $matches
57
             */
58
            return $matches;
59
        }
60
61
        return null;
62
    }
63
}
64