|
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 samsonframework\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
|
|
|
* Set return comment line. |
|
46
|
|
|
* |
|
47
|
|
|
* @param string $type Return type |
|
48
|
|
|
* @param string|null $description Return description |
|
49
|
|
|
* |
|
50
|
|
|
* @return CommentsGenerator |
|
51
|
|
|
*/ |
|
52
|
|
|
public function defReturn(string $type, string $description = null) : CommentsGenerator |
|
53
|
|
|
{ |
|
54
|
|
|
return $this->defLine('@return ' . $type . ($description !== null ? ' ' . $description : '')); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Set method comment line. |
|
59
|
|
|
* |
|
60
|
|
|
* @param string $name Method name |
|
61
|
|
|
* @param string $returnType Method return type |
|
62
|
|
|
* @param array $arguments Method arguments |
|
63
|
|
|
* |
|
64
|
|
|
* @return CommentsGenerator |
|
65
|
|
|
* @internal param null|string $description Argument description |
|
66
|
|
|
* |
|
67
|
|
|
*/ |
|
68
|
|
|
public function defMethod(string $name, string $returnType, array $arguments = []) : CommentsGenerator |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->defLine('@method ' . $returnType . ' ' . $name . '(' . $this->buildArguments($arguments) . ')'); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* {@inheritdoc} |
|
75
|
|
|
*/ |
|
76
|
|
|
public function code(int $indentation = 0) : string |
|
77
|
|
|
{ |
|
78
|
|
|
$indentationString = $this->indentation($this->indentation); |
|
79
|
|
|
|
|
80
|
|
|
return count($this->code) === 1 |
|
81
|
|
|
? $this->formatSingleLine($indentationString) |
|
82
|
|
|
: $this->formatMultiLine($indentationString); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Format comments code into single line comment. |
|
87
|
|
|
* |
|
88
|
|
|
* @param string $indentation Indentation string |
|
89
|
|
|
* |
|
90
|
|
|
* @return string Single line comments code |
|
91
|
|
|
*/ |
|
92
|
|
|
protected function formatSingleLine(string $indentation) |
|
93
|
|
|
{ |
|
94
|
|
|
return $indentation.'/** '.$this->code[0].' */'; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Format comments code into multi line comment. |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $indentation Indentation string |
|
101
|
|
|
* |
|
102
|
|
|
* @return string Multi-line comments code |
|
103
|
|
|
*/ |
|
104
|
|
|
protected function formatMultiLine(string $indentation) |
|
105
|
|
|
{ |
|
106
|
|
|
$formattedCode = ['/**']; |
|
107
|
|
|
|
|
108
|
|
|
// Prepend inner indentation to code |
|
109
|
|
|
foreach ($this->code as $codeLine) { |
|
110
|
|
|
$formattedCode[] = ' * ' . $codeLine; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
$formattedCode[] = ' */'; |
|
114
|
|
|
|
|
115
|
|
|
return $indentation . implode("\n" . $indentation, $formattedCode); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|