Passed
Pull Request — master (#105)
by Evgeniy
08:08 queued 04:03
created

MethodService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cekta\DI\Reflection;
6
7
use ReflectionMethod;
8
9
class MethodService
10
{
11
    /**
12
     * @var ParameterService
13
     */
14
    private $parameter;
15
16 15
    public function __construct()
17
    {
18 15
        $this->parameter = new ParameterService();
19 15
    }
20
    /**
21
     * @param ReflectionMethod|null $method
22
     * @return array<string>
23
     */
24 9
    public function findDependencies(?ReflectionMethod $method): array
25
    {
26 9
        $parameters = [];
27 9
        if ($method !== null) {
28 6
            $parameters = $this->getDependencies($method);
29
        }
30 9
        return $parameters;
31
    }
32
33
    /**
34
     * @param string $comment
35
     * @return array<string>
36
     */
37 6
    private function getAnnotationParameters(string $comment): array
38
    {
39 6
        $result = [];
40 6
        $matches = [];
41 6
        preg_match_all("/@inject \\\\?([\w\d\\\\]*) \\$([\w\d]*)/", $comment, $matches, PREG_SET_ORDER);
42 6
        foreach ($matches as $match) {
43 3
            $result[$match[2]] = $match[1];
44
        }
45 6
        return $result;
46
    }
47
48
    /**
49
     * @param ReflectionMethod $method
50
     * @return array<string>
51
     */
52 6
    private function getDependencies(ReflectionMethod $method): array
53
    {
54 6
        $parameters = [];
55 6
        $annotations = $this->getAnnotationParameters((string) $method->getDocComment());
56 6
        foreach ($method->getParameters() as $parameter) {
57 6
            $parameters[] = $this->parameter->getName($parameter, $annotations);
58
        }
59 6
        return $parameters;
60
    }
61
}
62