Completed
Push — master ( 4ce755...511437 )
by Vitaly
02:11
created

CommentsGenerator::code()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
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
    use CodeTrait;
16
17
    /**
18
     * Set var comment line.
19
     *
20
     * @param string      $type        Type
21
     * @param string|null $description Description
22
     *
23
     * @return CommentsGenerator
24
     */
25
    public function defVar(string $type, string $description = null) : CommentsGenerator
26
    {
27
        return $this->defLine('@var ' . $type . ($description !== null ? ' ' . $description : ''));
28
    }
29
30
    /**
31
     * Set param comment line.
32
     *
33
     * @param string      $name        Argument name
34
     * @param string      $type        Argument type
35
     * @param string|null $description Argument description
36
     *
37
     * @return CommentsGenerator
38
     */
39
    public function defParam(string $name, string $type, string $description = null) : CommentsGenerator
40
    {
41
        return $this->defLine('@param ' . $type . ' ' . $name . ($description !== null ? ' ' . $description : ''));
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function code(int $indentation = 0) : string
48
    {
49
        $indentationString = $this->indentation($indentation);
50
51
        return count($this->code) === 1
52
            ? $this->formatSingleLine($indentationString)
53
            : $this->formatMultiLine($indentationString);
54
    }
55
56
    /**
57
     * Format comments code into single line comment.
58
     *
59
     * @param string $indentation Indentation string
60
     *
61
     * @return string Single line comments code
62
     */
63
    protected function formatSingleLine(string $indentation)
64
    {
65
        return $indentation.'/** '.$this->code[0].' */';
66
    }
67
68
    /**
69
     * Format comments code into multi line comment.
70
     *
71
     * @param string $indentation Indentation string
72
     *
73
     * @return string Multi-line comments code
74
     */
75
    protected function formatMultiLine(string $indentation)
76
    {
77
        $formattedCode = ['/**'];
78
79
        // Prepend inner indentation to code
80
        foreach ($this->code as $codeLine) {
81
            $formattedCode[] = ' * ' . $codeLine;
82
        }
83
84
        $formattedCode[] = ' */';
85
86
        return implode("\n" . $indentation, $formattedCode);
87
    }
88
}
89