Completed
Push — master ( d75c1f...eed677 )
by Viacheslav
12s
created

PhpDoc::toString()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 17
nc 12
nop 0
dl 0
loc 25
ccs 12
cts 12
cp 1
crap 6
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
namespace Swaggest\PhpCodeBuilder;
4
5
6
class PhpDoc extends PhpTemplate
7
{
8
    const TAG_PARAM = 'param';
9
    const TAG_FILE = 'file';
10
    const TAG_VAR = 'var';
11
    const TAG_PROPERTY = 'property';
12
    const TAG_RETURN = 'return';
13
    const TAG_THROWS = 'throws';
14
    const TAG_METHOD = 'method';
15
    const TAG_SEE = 'see';
16
    const TAG_CODE_COVERAGE_IGNORE_START = 'codeCoverageIgnoreStart';
17
    const TAG_CODE_COVERAGE_IGNORE_END = 'codeCoverageIgnoreEnd';
18
19
    /** @var PhpDocTag[] */
20
    private $tags = array();
21
22 6
    public function add($name, $value = '', $id = null)
23
    {
24 6
        if ($id) {
25 2
            $this->tags[$id] = new PhpDocTag($name, $value);
26
        } else {
27 6
            $this->tags[] = new PhpDocTag($name, $value);
28
        }
29 6
        return $this;
30
    }
31
32
    public function removeById($id)
33
    {
34
        if (isset($this->tags[$id])) {
35
            unset($this->tags[$id]);
36
        }
37
        return $this;
38
    }
39
40 2
    public function prepend($name, $value = '')
41
    {
42 2
        array_unshift($this->tags, new PhpDocTag($name, $value));
43 2
        return $this;
44
    }
45
46
    public function isEmpty()
47
    {
48
        return empty($this->tags);
49
    }
50
51 7
    protected function toString()
52
    {
53 7
        $result = '';
54 7
        foreach ($this->tags as $tag) {
55 6
            if ($tag->name) {
56 6
                if ($tag->name === 'method') {
57 2
                    echo '.';
58
                }
59 6
                $value = $tag->value ? $this->padLines(' * ', ' ' . $tag->value, true, true) : '';
60
                $result .= <<<PHP
61 6
 * @{$tag->name}{$value}
62
63
PHP;
64
            } else {
65 6
                $result .= $this->padLines(' * ', $tag->value, false, true) . "\n";
66
            }
67
        }
68 7
        if ($result) {
69
            $result = <<<PHP
70
/**
71 6
$result */
72
73
PHP;
74
        }
75 7
        return $result;
76
    }
77
}