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 |
||
48 | class StandardCacheWarmer implements FluidCacheWarmerInterface |
||
49 | { |
||
50 | |||
51 | /** |
||
52 | * Template file formats (file extensions) supported by this |
||
53 | * cache warmer implementation. |
||
54 | * |
||
55 | * @var array |
||
56 | */ |
||
57 | protected $formats = ['html', 'xml', 'txt', 'json', 'rtf', 'atom', 'rss']; |
||
58 | |||
59 | /** |
||
60 | * Warm up an entire collection of templates based on the |
||
61 | * provided RenderingContext (the TemplatePaths carried by |
||
62 | * the RenderingContext, to be precise). |
||
63 | * |
||
64 | * Returns a FluidCacheWarmupResult with result information |
||
65 | * about all detected template files and the compiling of |
||
66 | * those files. If a template fails to compile or throws an |
||
67 | * error, a mitigation suggestion is included for that file. |
||
68 | * |
||
69 | * @param RenderingContextInterface $renderingContext |
||
70 | * @return FluidCacheWarmupResult |
||
71 | */ |
||
72 | public function warm(RenderingContextInterface $renderingContext) |
||
83 | |||
84 | /** |
||
85 | * Warm up _templateRootPaths_ of the RenderingContext's |
||
86 | * TemplatePaths instance. |
||
87 | * |
||
88 | * Scans for template files recursively in all template root |
||
89 | * paths while respecting overlays, e.g. if a path replaces |
||
90 | * the template file of a lower priority path then only |
||
91 | * one result is returned - the overlayed template file. In |
||
92 | * other words the resolving happens exactly as if you were |
||
93 | * attempting to render each detected controller, so that the |
||
94 | * compiled template will be the same that is resolved when |
||
95 | * rendering that controller. |
||
96 | * |
||
97 | * Also scans the root level of all templateRootPaths for |
||
98 | * controller-less/fallback-action template files, e.g. files |
||
99 | * which would be rendered if a specified controller's action |
||
100 | * template does not exist (fallback-action) or if no controller |
||
101 | * name was specified in the context (controller-less). |
||
102 | * |
||
103 | * Like other methods, returns a FluidCacheWarmupResult instance |
||
104 | * which can be merged with other result instances. |
||
105 | * |
||
106 | * @param RenderingContextInterface $renderingContext |
||
107 | * @return FluidCacheWarmupResult |
||
108 | */ |
||
109 | protected function warmupTemplateRootPaths(RenderingContextInterface $renderingContext) |
||
142 | |||
143 | /** |
||
144 | * Warm up _partialRootPaths_ of the provided RenderingContext's |
||
145 | * TemplatePaths instance. Simple, recursive processing of all |
||
146 | * supported format template files in path(s), compiling only |
||
147 | * the topmost (override) template file if the same template |
||
148 | * exists in multiple partial root paths. |
||
149 | * |
||
150 | * Like other methods, returns a FluidCacheWarmupResult instance |
||
151 | * which can be merged with other result instances. |
||
152 | * |
||
153 | * @param RenderingContextInterface $renderingContext |
||
154 | * @return FluidCacheWarmupResult |
||
155 | */ |
||
156 | View Code Duplication | protected function warmupPartialRootPaths(RenderingContextInterface $renderingContext) |
|
173 | |||
174 | /** |
||
175 | * Warm up _layoutRootPaths_ of the provided RenderingContext's |
||
176 | * TemplatePaths instance. Simple, recursive processing of all |
||
177 | * supported format template files in path(s), compiling only |
||
178 | * the topmost (override) template file if the same template |
||
179 | * exists in multiple layout root paths. |
||
180 | * |
||
181 | * Like other methods, returns a FluidCacheWarmupResult instance |
||
182 | * which can be merged with other result instances. |
||
183 | * |
||
184 | * @param RenderingContextInterface $renderingContext |
||
185 | * @return FluidCacheWarmupResult |
||
186 | */ |
||
187 | View Code Duplication | protected function warmupLayoutRootPaths(RenderingContextInterface $renderingContext) |
|
204 | |||
205 | /** |
||
206 | * Detect all available controller names in provided TemplateRootPaths |
||
207 | * array, returning the "basename" components of controller-template |
||
208 | * directories encountered, as an array. |
||
209 | * |
||
210 | * @param string $templateRootPaths |
||
211 | * @return \Generator |
||
212 | */ |
||
213 | protected function detectControllerNamesInTemplateRootPaths(array $templateRootPaths) |
||
223 | |||
224 | /** |
||
225 | * Warm up a single template file. |
||
226 | * |
||
227 | * Performs reading, parsing and attempts compiling of a single |
||
228 | * template file. Catches errors that may occur and reports them |
||
229 | * in a FailedCompilingState (which can then be `add()`'ed to |
||
230 | * the FluidCacheWarmupResult to assimilate the information within. |
||
231 | * |
||
232 | * Adds basic mitigation suggestions for each specific type of error, |
||
233 | * giving hints to developers if a certain template fails to compile. |
||
234 | * |
||
235 | * @param string $templatePathAndFilename |
||
236 | * @param string $identifier |
||
237 | * @param RenderingContextInterface $renderingContext |
||
238 | * @return ParsedTemplateInterface |
||
239 | */ |
||
240 | protected function warmSingleFile($templatePathAndFilename, $identifier, RenderingContextInterface $renderingContext) |
||
305 | |||
306 | /** |
||
307 | * @param string $templatePathAndFilename |
||
308 | * @return \Closure |
||
309 | */ |
||
310 | protected function createClosure($templatePathAndFilename) |
||
316 | } |
||
317 |
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.