getNonBuiltInTypeForParameter()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 9
cts 9
cp 1
rs 9.7333
c 0
b 0
f 0
cc 3
nc 2
nop 1
crap 3
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