Annotation   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 13
eloc 29
c 1
b 0
f 1
dl 0
loc 103
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 6 3
A __construct() 0 4 1
A rowing() 0 4 2
A all() 0 3 1
A parsing() 0 32 5
A has() 0 3 1
1
<?php
2
/**
3
 * Annotation parser
4
 * User: moyo
5
 * Date: 12/10/2017
6
 * Time: 10:33 AM
7
 */
8
9
namespace Carno\Container\Injection;
10
11
use Closure;
12
13
class Annotation
14
{
15
    /**
16
     * @var string
17
     */
18
    private $raw = null;
19
20
    /**
21
     * @var array
22
     */
23
    private $lines = [];
24
25
    /**
26
     * @var array
27
     */
28
    private $declares = [];
29
30
    /**
31
     * Annotation constructor.
32
     * @param string $doc
33
     */
34
    public function __construct(string $doc)
35
    {
36
        $this->raw = $doc;
37
        $this->parsing();
38
    }
39
40
    /**
41
     * @param string $key
42
     * @return bool
43
     */
44
    public function has(string $key) : bool
45
    {
46
        return isset($this->declares[$key]);
47
    }
48
49
    /**
50
     * @param string $key
51
     * @return mixed
52
     */
53
    public function get(string $key)
54
    {
55
        return
56
            $this->has($key)
57
                ? (count($expr = $this->declares[$key]) === 1 ? end($expr) : implode('', $expr))
58
                : null
59
            ;
60
    }
61
62
    /**
63
     * @param string $key
64
     * @return array
65
     */
66
    public function all(string $key) : array
67
    {
68
        return $this->declares[$key] ?? [];
69
    }
70
71
    /**
72
     * @param Closure $printer
73
     */
74
    public function rowing(Closure $printer) : void
75
    {
76
        foreach ($this->lines as $line) {
77
            $printer(...$line);
78
        }
79
    }
80
81
    /**
82
     * simple comment parsing
83
     */
84
    private function parsing() : void
85
    {
86
        $lines = explode("\n", $this->raw);
87
88
        foreach ($lines as $line) {
89
            // check "at" mark
90
            if (false === strpos($line, '@')) {
91
                continue;
92
            }
93
94
            // check inline annotation
95
            if ($anp = strpos($line, '//')) {
96
                $line = substr($line, 0, $anp);
97
            }
98
99
            // trim blanks
100
            $line = trim(ltrim($line, ' *'));
101
102
            // analyze expr
103
            if (false === $fws = strpos($line, ' ')) {
104
                // @some-key
105
                $key = substr($line, 1);
106
                $val = true;
107
            } else {
108
                // @some-key some-val
109
                $key = substr($line, 1, $fws - 1);
110
                $val = trim(substr($line, $fws));
111
            }
112
113
            $this->lines[] = [$key, $val];
114
115
            $this->declares[$key][] = $val;
116
        }
117
    }
118
}
119