Loader::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Annotations loader
4
 * User: moyo
5
 * Date: 2018/6/4
6
 * Time: 10:45 AM
7
 */
8
9
namespace Carno\Validator\Anno;
10
11
use Carno\Container\Injection\Annotation;
12
use Carno\Validator\Coordinator;
13
use Carno\Validator\Inspector;
14
use Carno\Validator\Valid\Executor;
15
use ReflectionClass;
16
use ReflectionMethod;
17
18
class Loader
19
{
20
    /**
21
     * @var Coordinator
22
     */
23
    private $coordinator = null;
24
25
    /**
26
     * @var Inspector
27
     */
28
    private $inspector = null;
29
30
    /**
31
     * @var Executor
32
     */
33
    private $builder = null;
34
35
    /**
36
     * Loader constructor.
37
     * @param Coordinator $coordinator
38
     * @param Inspector $inspector
39
     */
40
    public function __construct(Coordinator $coordinator, Inspector $inspector)
41
    {
42
        $this->coordinator = $coordinator;
43
        $this->inspector = $inspector;
44
        $this->builder = new Executor;
45
    }
46
47
    /**
48
     * @param string $class
49
     */
50
    public function parsing(string $class) : void
51
    {
52
        foreach ((new ReflectionClass($class))->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
53
            $builder = new Executor;
54
55
            (new Annotation($method->getDocComment()))->rowing(function (string $key, $expr) use ($builder) {
0 ignored issues
show
Bug introduced by
It seems like $method->getDocComment() can also be of type boolean; however, parameter $doc of Carno\Container\Injectio...notation::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

55
            (new Annotation(/** @scrutinizer ignore-type */ $method->getDocComment()))->rowing(function (string $key, $expr) use ($builder) {
Loading history...
56
                $this->acknowledge($builder, $key, $expr);
57
            });
58
59
            $this->coordinator->group()->stop();
60
61
            $this->inspector->join($class, $method->getName(), $builder);
62
        }
63
    }
64
65
    /**
66
     * @param array $rules
67
     */
68
    public function walking(array $rules) : void
69
    {
70
        foreach ($rules as $rule) {
71
            $this->acknowledge($this->builder, array_shift($rule), implode(' ', $rule) ?: true);
0 ignored issues
show
Bug introduced by
It seems like implode(' ', $rule) ?: true can also be of type true; however, parameter $av of Carno\Validator\Anno\Loader::acknowledge() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

71
            $this->acknowledge($this->builder, array_shift($rule), /** @scrutinizer ignore-type */ implode(' ', $rule) ?: true);
Loading history...
72
        }
73
    }
74
75
    /**
76
     * @param Executor $builder
77
     * @param string $ak
78
     * @param string $av
79
     */
80
    private function acknowledge(Executor $builder, string $ak, $av) : void
81
    {
82
        switch ($ak) {
83
            case 'valid-group':
84
                $av === true
0 ignored issues
show
introduced by
The condition $av === true is always false.
Loading history...
85
                    ? $this->coordinator->group()->stop()
86
                    : $this->coordinator->group()->start($av, $builder);
87
                break;
88
            case 'valid-inherit':
89
                $this->coordinator->inherit()->sync($av, $this->coordinator->group(), $builder);
90
                break;
91
            case 'valid-named':
92
                $this->coordinator->group()->attach($this->coordinator->named()->mark($av, $builder));
93
                break;
94
            case 'valid-clone':
95
                $this->coordinator->clone()->from($av, $builder, $this->coordinator->named());
96
                break;
97
            case 'valid':
98
                $this->coordinator->group()->attach($builder->analyzing($av));
99
        }
100
    }
101
}
102