Completed
Branch master (a2aa27)
by Randy
01:27
created

AnnotationParser::parsePropertyAnnotations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Dgame\Annotation;
4
5
/**
6
 * Class AnnotationParser
7
 * @package Dgame\Annotation
8
 */
9
final class AnnotationParser
10
{
11
    const REGEX = '\s+(.*?)\s*(\$.+?)?\s*\z';
12
13
    /**
14
     * @param string $comment
15
     *
16
     * @return VariableAnnotation[]
17
     */
18
    public function parseParameterAnnotations(string $comment): array
19
    {
20
        return $this->parseAnnotations('param', $comment);
21
    }
22
23
    /**
24
     * @param string $comment
25
     *
26
     * @return VariableAnnotation[]
27
     */
28
    public function parseVariableAnnotations(string $comment): array
29
    {
30
        return $this->parseAnnotations('var', $comment);
31
    }
32
33
    /**
34
     * @param string $comment
35
     *
36
     * @return VariableAnnotation[]
37
     */
38
    public function parsePropertyAnnotations(string $comment): array
39
    {
40
        return $this->parseAnnotations('property', $comment);
41
    }
42
43
    /**
44
     * @param string $annotation
45
     * @param string $comment
46
     *
47
     * @return VariableAnnotation[]
48
     */
49
    private function parseAnnotations(string $annotation, string $comment): array
50
    {
51
        $output = [];
52
        foreach (preg_split('/\R/m', $comment) as $line) {
53
            $result = $this->parseAnnotation($annotation, $line);
54
            if ($result !== null) {
55
                $output[] = $result;
56
            }
57
        }
58
59
        return $output;
60
    }
61
62
    /**
63
     * @param string $annotation
64
     * @param string $line
65
     *
66
     * @return VariableAnnotation|null
67
     */
68
    private function parseAnnotation(string $annotation, string $line): ?VariableAnnotation
69
    {
70
        $regex = sprintf('/@%s%s/i', $annotation, self::REGEX);
71
        if (preg_match($regex, $line, $matches)) {
72
            [$type, $var] = $this->parseAnnotationMatch($matches);
0 ignored issues
show
Bug introduced by
The variable $type does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $var does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
73
74
            return new VariableAnnotation($type, $var);
75
        }
76
77
        return null;
78
    }
79
80
    /**
81
     * @param array $matches
82
     *
83
     * @return array
84
     */
85
    private function parseAnnotationMatch(array $matches): array
86
    {
87
        array_shift($matches);
88
        if (!array_key_exists(0, $matches)) {
89
            $matches[0] = 'mixed';
90
        }
91
92
        if (!array_key_exists(1, $matches)) {
93
            $matches[1] = '';
94
        }
95
96
        return $matches;
97
    }
98
}