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 |
||
19 | class Template extends Abstraction implements ResultFormatter |
||
20 | { |
||
21 | /** |
||
22 | * Template markup code |
||
23 | * |
||
24 | * @var string |
||
25 | */ |
||
26 | private $template; |
||
27 | |||
28 | /** |
||
29 | * Template constructor. |
||
30 | * |
||
31 | * @param string $file |
||
32 | */ |
||
33 | 3 | public function __construct(string $file) |
|
37 | |||
38 | /** |
||
39 | * Loads the body template if it exists. |
||
40 | * |
||
41 | * @param string $template |
||
42 | * @return string |
||
43 | * @throws \phpbu\App\Exception |
||
44 | */ |
||
45 | 3 | private function loadBodyFromTemplate(string $template) : string |
|
52 | |||
53 | /** |
||
54 | * Create request body from phpbu result data. |
||
55 | * |
||
56 | * @param \phpbu\App\Result $result |
||
57 | * @return string |
||
58 | */ |
||
59 | 2 | public function format(Result $result): string |
|
74 | |||
75 | /** |
||
76 | * Handles error sub template rendering if necessary. |
||
77 | * - Manipulates $this->template |
||
78 | * |
||
79 | * @param \phpbu\App\Result $result |
||
80 | */ |
||
81 | 2 | View Code Duplication | private function handleErrors(Result $result) |
90 | |||
91 | /** |
||
92 | * Handles backup sub template rendreing if necessary. |
||
93 | * - Manipulates $this->template |
||
94 | * |
||
95 | * @param \phpbu\App\Result $result |
||
96 | */ |
||
97 | 2 | View Code Duplication | private function handleBackups(Result $result) |
106 | |||
107 | /** |
||
108 | * Extract loop template. |
||
109 | * |
||
110 | * @param string $template |
||
111 | * @param string $loop |
||
112 | * @return string |
||
113 | */ |
||
114 | 2 | private function extractSubTemplate(string $template, string $loop) : string |
|
123 | |||
124 | /** |
||
125 | * Renders errors with extracted error sub template. |
||
126 | * |
||
127 | * @param string $errorTpl |
||
128 | * @param array $errors |
||
129 | * @return string |
||
130 | */ |
||
131 | 2 | private function renderErrors(string $errorTpl, array $errors) : string |
|
146 | |||
147 | /** |
||
148 | * Renders backups with the extracted sub template. |
||
149 | * |
||
150 | * @param string $backupTpl |
||
151 | * @param array $backups |
||
152 | * @return string |
||
153 | */ |
||
154 | 2 | private function renderBackups(string $backupTpl, array $backups) : string |
|
178 | |||
179 | /** |
||
180 | * Replace %name% placeholders in a string with [name => value]. |
||
181 | * |
||
182 | * @param string $template |
||
183 | * @param array $data |
||
184 | * @return string |
||
185 | */ |
||
186 | 2 | private function renderTemplate(string $template, array $data) : string |
|
193 | |||
194 | /** |
||
195 | * Replace a loop placeholder %%loopname%% with some pre rendered markup. |
||
196 | * |
||
197 | * @param string $loop |
||
198 | * @param string $markup |
||
199 | */ |
||
200 | 2 | private function renderLoop(string $loop, string $markup) |
|
204 | } |
||
205 |