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