ReflectionMethodWrapper   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 0
loc 50
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getParameterByName() 0 14 3
A getNonBuiltInTypeForParameter() 0 16 3
A getFriendlyName() 0 8 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Maba\Bundle\RestBundle\Service\Annotation;
5
6
use Maba\Bundle\RestBundle\Exception\ConfigurationException;
7
use ReflectionMethod;
8
use ReflectionParameter;
9
10
/**
11
 * @internal
12
 */
13
class ReflectionMethodWrapper
14
{
15
    private $reflectionMethod;
16
17 89
    public function __construct(ReflectionMethod $reflectionMethod)
18
    {
19 89
        $this->reflectionMethod = $reflectionMethod;
20 89
    }
21
22 87
    public function getParameterByName(string $name): ReflectionParameter
23
    {
24 87
        foreach ($this->reflectionMethod->getParameters() as $parameter) {
25 86
            if ($parameter->getName() === $name) {
0 ignored issues
show
Bug introduced by
Consider using $parameter->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
26 86
                return $parameter;
27
            }
28
        }
29
30 2
        throw new ConfigurationException(sprintf(
31 2
            'Parameter %s is configured but not found in method %s',
32 2
            '$' . $name,
33 2
            $this->getFriendlyName()
34
        ));
35
    }
36
37 85
    public function getNonBuiltInTypeForParameter(string $name): string
38
    {
39 85
        $parameter = $this->getParameterByName($name);
40
41 84
        $type = $parameter->getType();
42 84
        if ($type === null || $type->isBuiltin()) {
43 2
            throw new ConfigurationException(sprintf(
44 2
                'Expected non built-in type-hint for %s in %s',
45 2
                '$' . $parameter->getName(),
0 ignored issues
show
Bug introduced by
Consider using $parameter->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
46 2
                $this->getFriendlyName()
47
            ));
48
        }
49
50
        // `$type->getName()` should be used when 7.0 support is dropped
51 82
        return (string)$type;
52
    }
53
54 87
    public function getFriendlyName()
55
    {
56 87
        return sprintf(
57 87
            '%s::%s',
58 87
            $this->reflectionMethod->getDeclaringClass()->getName(),
0 ignored issues
show
introduced by
Consider using $this->reflectionMethod->class. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
59 87
            $this->reflectionMethod->getName()
0 ignored issues
show
Bug introduced by
Consider using $this->reflectionMethod->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
60
        );
61
    }
62
}
63