1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Leaditin\Code\Generator; |
4
|
|
|
|
5
|
|
|
use Leaditin\Code\DocBlock; |
6
|
|
|
use Leaditin\Code\Member\Method; |
7
|
|
|
use Leaditin\Code\Tag; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @package Leaditin\Code |
11
|
|
|
* @author Igor Vuckovic <[email protected]> |
12
|
|
|
* @license MIT |
13
|
|
|
*/ |
14
|
|
|
class MethodGenerator extends Generator |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var ArgumentGenerator |
18
|
|
|
*/ |
19
|
|
|
protected $argumentGenerator; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var DocBlockGenerator |
23
|
|
|
*/ |
24
|
|
|
protected $docBlockGenerator; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var TypeGenerator |
28
|
|
|
*/ |
29
|
|
|
protected $typeGenerator; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var VisibilityGenerator |
33
|
|
|
*/ |
34
|
|
|
protected $visibilityGenerator; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param ArgumentGenerator $argumentGenerator |
38
|
|
|
* @param DocBlockGenerator $docBlockGenerator |
39
|
|
|
* @param TypeGenerator $typeGenerator |
40
|
|
|
* @param VisibilityGenerator $visibilityGenerator |
41
|
|
|
*/ |
42
|
3 |
View Code Duplication |
public function __construct( |
|
|
|
|
43
|
|
|
ArgumentGenerator $argumentGenerator, |
44
|
|
|
DocBlockGenerator $docBlockGenerator, |
45
|
|
|
TypeGenerator $typeGenerator, |
46
|
|
|
VisibilityGenerator $visibilityGenerator |
47
|
|
|
) { |
48
|
3 |
|
$this->argumentGenerator = $argumentGenerator; |
49
|
3 |
|
$this->docBlockGenerator = $docBlockGenerator; |
50
|
3 |
|
$this->typeGenerator = $typeGenerator; |
51
|
3 |
|
$this->visibilityGenerator = $visibilityGenerator; |
52
|
|
|
|
53
|
3 |
|
$this->setDepth(1); |
54
|
3 |
|
$this->docBlockGenerator->setDepth(1); |
55
|
3 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param Method $method |
59
|
|
|
* |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
3 |
|
public function generate(Method $method): string |
63
|
|
|
{ |
64
|
3 |
|
$output = $this->generateLine($this->docBlockGenerator->generate($this->generateDocBlock($method)), 0); |
65
|
|
|
|
66
|
3 |
|
$line = ''; |
67
|
3 |
|
if ($method->isAbstract()) { |
68
|
1 |
|
$line .= 'abstract '; |
69
|
2 |
|
} elseif ($method->isFinal()) { |
70
|
1 |
|
$line .= 'final '; |
71
|
|
|
} |
72
|
|
|
|
73
|
3 |
|
$line .= $this->visibilityGenerator->generate($method->visibility()); |
74
|
|
|
|
75
|
3 |
|
if ($method->isStatic()) { |
76
|
1 |
|
$line .= ' static'; |
77
|
|
|
} |
78
|
|
|
|
79
|
3 |
|
$line .= ' function ' . $method->name() . '('; |
80
|
|
|
|
81
|
3 |
|
$arguments = []; |
82
|
3 |
|
foreach ($method->arguments() as $argument) { |
83
|
2 |
|
$arguments[] = $this->argumentGenerator->generate($argument); |
84
|
|
|
} |
85
|
3 |
|
$line .= implode(', ', $arguments); |
86
|
|
|
|
87
|
3 |
|
$line .= ')'; |
88
|
|
|
|
89
|
3 |
|
if ($method->returnType() !== null) { |
90
|
3 |
|
$line .= ': ' . $this->typeGenerator->generate($method->returnType()); |
91
|
|
|
} |
92
|
|
|
|
93
|
3 |
|
if ($method->isAbstract()) { |
94
|
1 |
|
$output .= $this->generateLine($line . ';'); |
95
|
|
|
|
96
|
1 |
|
return rtrim($output, $this->endOfLine); |
97
|
|
|
} |
98
|
|
|
|
99
|
2 |
|
$output .= $this->generateLine($line); |
100
|
2 |
|
$output .= $this->generateLine('{'); |
101
|
|
|
|
102
|
2 |
|
if ($method->body() !== null) { |
103
|
2 |
|
$output .= $this->generateLine( |
104
|
2 |
|
preg_replace( |
105
|
2 |
|
'#^((?![a-zA-Z0-9_-]+;).+?)$#m', |
106
|
2 |
|
'$1', |
107
|
2 |
|
trim($method->body()) |
108
|
|
|
), |
109
|
2 |
|
2 |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
|
113
|
2 |
|
$output .= $this->generateLine('}'); |
114
|
|
|
|
115
|
2 |
|
return rtrim($output, $this->endOfLine); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param Method $method |
120
|
|
|
* |
121
|
|
|
* @return DocBlock |
122
|
|
|
*/ |
123
|
3 |
|
protected function generateDocBlock(Method $method): DocBlock |
124
|
|
|
{ |
125
|
3 |
|
$docblock = ''; |
126
|
3 |
|
$shortDescription = null; |
127
|
3 |
|
$longDescription = null; |
128
|
3 |
|
$tags = []; |
129
|
|
|
|
130
|
3 |
|
if ($method->docBlock() !== null) { |
131
|
2 |
|
$docblock = $this->docBlockGenerator->generate($method->docBlock()); |
132
|
2 |
|
$shortDescription = $method->docBlock()->shortDescription(); |
133
|
2 |
|
$longDescription = $method->docBlock()->longDescription(); |
134
|
2 |
|
$tags = $method->docBlock()->tags(); |
135
|
|
|
} |
136
|
|
|
|
137
|
3 |
|
foreach ($method->arguments() as $argument) { |
138
|
2 |
|
$comment = '@property ' . $this->argumentGenerator->generate($argument); |
139
|
|
|
|
140
|
2 |
|
if (strpos($docblock, $comment) === false) { |
141
|
2 |
|
$tags[] = new Tag('property', $this->argumentGenerator->generate($argument)); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
3 |
|
if ($method->returnType() !== null && strpos($docblock, '@return') === false) { |
146
|
3 |
|
$tags[] = new Tag('return', ($method->returnType()->isNullable() ? 'null|' : '') . $method->returnType()->type()); |
147
|
|
|
} |
148
|
|
|
|
149
|
3 |
|
return new DocBlock($shortDescription, $longDescription, $tags); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.