|
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
|
|
|
* Add function code line. |
|
30
|
|
|
* |
|
31
|
|
|
* @param string $code Code line |
|
32
|
|
|
* |
|
33
|
|
|
* @return $this |
|
34
|
|
|
*/ |
|
35
|
|
|
public function defLine(string $code) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->code[] = $code; |
|
38
|
|
|
|
|
39
|
|
|
return $this; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* {@inheritdoc} |
|
44
|
|
|
*/ |
|
45
|
|
|
public function code(int $indentation = 0) : string |
|
46
|
|
|
{ |
|
47
|
|
|
$indentationString = $this->indentation($indentation); |
|
48
|
|
|
|
|
49
|
|
|
return count($this->code) === 1 |
|
50
|
|
|
? $this->formatSingleLine($indentationString) |
|
51
|
|
|
: $this->formatMultiLine($indentationString); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Format comments code into single line comment. |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $indentation Indentation string |
|
58
|
|
|
* |
|
59
|
|
|
* @return string Single line comments code |
|
60
|
|
|
*/ |
|
61
|
|
|
protected function formatSingleLine(string $indentation) |
|
62
|
|
|
{ |
|
63
|
|
|
return $indentation.'/** '.$this->code[0].' */'; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Format comments code into multi line comment. |
|
68
|
|
|
* |
|
69
|
|
|
* @param string $indentation Indentation string |
|
70
|
|
|
* |
|
71
|
|
|
* @return string Multi-line comments code |
|
72
|
|
|
*/ |
|
73
|
|
|
protected function formatMultiLine(string $indentation) |
|
74
|
|
|
{ |
|
75
|
|
|
$formattedCode = ['/**']; |
|
76
|
|
|
|
|
77
|
|
|
// Prepend inner indentation to code |
|
78
|
|
|
foreach ($this->code as $codeLine) { |
|
79
|
|
|
$formattedCode[] = ' * ' . $codeLine; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$formattedCode[] = ' */'; |
|
83
|
|
|
|
|
84
|
|
|
return implode("\n" . $indentation, $formattedCode); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|