Passed
Pull Request — master (#405)
by Kirill
08:21 queued 04:03
created

getReflectionFunctionParameter()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 2
b 0
f 0
nc 3
nop 2
dl 0
loc 13
rs 10
1
<?php
2
3
/**
4
 * This file is part of Spiral Framework package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Tests\Attributes\Concerns;
13
14
trait InteractWithReflection
15
{
16
    protected function getReflectionClass(string $class): \ReflectionClass
17
    {
18
        return new \ReflectionClass($class);
19
    }
20
21
    protected function getReflectionMethod(string $class, string $name): \ReflectionMethod
22
    {
23
        return $this->getReflectionClass($class)
24
            ->getMethod($name)
25
        ;
26
    }
27
28
    protected function getReflectionFunction(string $name): \ReflectionFunction
29
    {
30
        return new \ReflectionFunction($name);
31
    }
32
33
    protected function getReflectionProperty(string $class, string $name): \ReflectionProperty
34
    {
35
        return $this->getReflectionClass($class)
36
            ->getProperty($name)
37
        ;
38
    }
39
40
    protected function getReflectionConstant(string $class, string $name): \ReflectionClassConstant
41
    {
42
        $constants = $this->getReflectionClass($class)
43
            ->getReflectionConstants()
44
        ;
45
46
        foreach ($constants as $reflection) {
47
            if ($reflection->getName() === $name) {
48
                return $reflection;
49
            }
50
        }
51
52
        throw new \ReflectionException('Constant ' . $name . ' not found');
53
    }
54
55
    protected function getReflectionFunctionParameter(string $function, string $name): \ReflectionParameter
56
    {
57
        $parameters = $this->getReflectionFunction($function)
58
            ->getParameters()
59
        ;
60
61
        foreach ($parameters as $reflection) {
62
            if ($reflection->getName() === $name) {
63
                return $reflection;
64
            }
65
        }
66
67
        throw new \ReflectionException('Parameter ' . $name . ' not found');
68
    }
69
70
    protected function getReflectionMethodParameter(string $class, string $method, string $name): \ReflectionParameter
71
    {
72
        $parameters = $this->getReflectionMethod($class, $method)
73
            ->getParameters()
74
        ;
75
76
        foreach ($parameters as $reflection) {
77
            if ($reflection->getName() === $name) {
78
                return $reflection;
79
            }
80
        }
81
82
        throw new \ReflectionException('Parameter ' . $name . ' not found');
83
    }
84
}
85