Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
21 | class Renamer extends Command |
||
22 | { |
||
23 | /** |
||
24 | * {@inheritdoc} |
||
25 | */ |
||
26 | protected $signature = 'app:rename {name? : The new name}'; |
||
27 | |||
28 | /** |
||
29 | * {@inheritdoc} |
||
30 | */ |
||
31 | protected $description = 'Perform an application rename'; |
||
32 | |||
33 | /** |
||
34 | * {@inheritdoc} |
||
35 | */ |
||
36 | 1 | public function handle(): void |
|
42 | |||
43 | /** |
||
44 | * Updates the binary name and the application |
||
45 | * name on the composer.json. |
||
46 | * |
||
47 | * @return $this |
||
48 | */ |
||
49 | 1 | protected function rename(): Renamer |
|
61 | |||
62 | /** |
||
63 | * Asks for the application name. |
||
64 | * |
||
65 | * If there is no interaction, we take the folder basename. |
||
66 | * |
||
67 | * @return string |
||
68 | */ |
||
69 | 1 | protected function asksForApplicationName(): string |
|
81 | |||
82 | /** |
||
83 | * Update composer json with related information. |
||
84 | * |
||
85 | * @param string $name |
||
86 | * |
||
87 | * @return $this |
||
88 | */ |
||
89 | 1 | protected function updateComposer(string $name): Renamer |
|
139 | |||
140 | /** |
||
141 | * Renames the application binary. |
||
142 | * |
||
143 | * @param string $name |
||
144 | * |
||
145 | * @return $this |
||
146 | */ |
||
147 | 1 | protected function renameBinary(string $name): Renamer |
|
158 | |||
159 | /** |
||
160 | * Returns the current binary name. |
||
161 | * |
||
162 | * @return string |
||
163 | */ |
||
164 | 1 | protected function getCurrentBinaryName(): string |
|
170 | |||
171 | /** |
||
172 | * Get composer file. |
||
173 | * |
||
174 | * @return string |
||
175 | */ |
||
176 | 1 | protected function getComposer(): string |
|
186 | |||
187 | /** |
||
188 | * Get config file. |
||
189 | * |
||
190 | * @return string |
||
191 | */ |
||
192 | 1 | protected function getConfig(): string |
|
202 | } |
||
203 |
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: