Test Failed
Push — uuid_annotation ( d6506f )
by David
06:37
created

AnnotationParser::parseAnnotation()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 2
nop 1
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace TheCodingMachine\TDBM\Utils\Annotation;
3
4
/**
5
 * Parses annotations in database columns.
6
 */
7
class AnnotationParser
8
{
9
    /**
10
     * Parses the doc comment and initilizes all the values of interest.
11
     *
12
     */
13
    public function parse(string $comment): Annotations
14
    {
15
        $lines = explode("\n", $comment);
16
        $lines = array_map(function (string $line) {
17
            return trim($line, " \r\t");
18
        }, $lines);
19
20
        $annotations = [];
21
22
        // Is the line an annotation? Let's test this with a regexp.
23
        foreach ($lines as $line) {
24
            if (preg_match("/^[@][a-zA-Z]/", $line) === 1) {
25
                $annotations[] = $this->parseAnnotation($line);
26
            }
27
        }
28
29
        return new Annotations($annotations);
30
    }
31
32
    /**
33
     * Parses an annotation line and stores the result in the MoufPhpDocComment.
34
     *
35
     * @param string $line
36
     */
37
    private function parseAnnotation($line)
38
    {
39
        // Let's get the annotation text
40
        preg_match("/^[@]([a-zA-Z][a-zA-Z0-9]*)(.*)/", $line, $values);
41
42
        $annotationClass = isset($values[1])?$values[1]:null;
43
        $annotationParams = trim(isset($values[2])?$values[2]:null);
44
45
        return new Annotation($annotationClass, $annotationParams);
46
    }
47
}
48