Completed
Push — master ( 9332be...e7ee4a )
by Vitaly
02:50
created

CommentsGenerator::defVar()   A

Complexity

Conditions 2
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 2
eloc 2
nc 1
nop 2
1
<?php declare(strict_types = 1);
2
/**
3
 * Created by Vitaly Iegorov <[email protected]>.
4
 * on 03.09.16 at 13:02
5
 */
6
namespace samsonphp\generator;
7
8
/**
9
 * Comments block generator.
10
 *
11
 * @author Vitaly Egorov <[email protected]>
12
 */
13
class CommentsGenerator extends AbstractGenerator
14
{
15
    /**
16
     * Set @var comment line.
17
     *
18
     * @param string      $type        Type
19
     * @param string|null $description Description
20
     *
21
     * @return CommentsGenerator
22
     */
23
    public function defVar(string $type, string $description = null) : CommentsGenerator
24
    {
25
        return $this->defLine('@var ' . $type . ($description !== null ? ' ' . $description : ''));
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function code($indentation = 0) : string
32
    {
33
        $indentationString = $this->indentation($indentation);
34
35
        return count($this->code) === 1
36
            ? $this->formatSingleLine($indentationString)
37
            : $this->formatMultiLine($indentationString);
38
    }
39
40
    /**
41
     * Format comments code into single line comment.
42
     *
43
     * @param string $indentation Indentation string
44
     *
45
     * @return string Single line comments code
46
     */
47
    protected function formatSingleLine(string $indentation)
48
    {
49
        return $indentation.'/** '.$this->code[0].' */';
50
    }
51
52
    /**
53
     * Format comments code into multi line comment.
54
     *
55
     * @param string $indentation Indentation string
56
     *
57
     * @return string Multi-line comments code
58
     */
59
    protected function formatMultiLine(string $indentation)
60
    {
61
        $formattedCode = ['/**'];
62
63
        // Prepend inner indentation to code
64
        foreach ($this->code as $codeLine) {
65
            $formattedCode[] = ' * ' . $codeLine;
66
        }
67
68
        $formattedCode[] = ' */';
69
70
        return implode("\n" . $indentation, $formattedCode);
71
    }
72
}
73