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 |
||
20 | trait CompileWithContentArgumentAndRenderStatic |
||
21 | { |
||
22 | /** |
||
23 | * Name of variable that contains the value to use |
||
24 | * instead of render children closure, if specified. |
||
25 | * If no name is provided here, the first variable |
||
26 | * registered in `initializeArguments` of the ViewHelper |
||
27 | * will be used. |
||
28 | * |
||
29 | * Note: it is significantly better practice to define |
||
30 | * this property in your ViewHelper class and so fix it |
||
31 | * to one particular argument instead of resolving, |
||
32 | * especially when your ViewHelper is called multiple |
||
33 | * times within an uncompiled template! |
||
34 | * |
||
35 | * @var string |
||
36 | */ |
||
37 | protected $contentArgumentName; |
||
38 | |||
39 | /** |
||
40 | * @param string $argumentsName |
||
41 | * @param string $closureName |
||
42 | * @param string $initializationPhpCode |
||
43 | * @param ViewHelperNode $node |
||
44 | * @param TemplateCompiler $compiler |
||
45 | * @return string |
||
46 | */ |
||
47 | View Code Duplication | public function compile( |
|
64 | |||
65 | /** |
||
66 | * Helper which is mostly needed when calling renderStatic() from within |
||
67 | * render(). |
||
68 | * |
||
69 | * No public API yet. |
||
70 | * |
||
71 | * @return \Closure |
||
72 | */ |
||
73 | protected function buildRenderChildrenClosure() |
||
89 | |||
90 | /** |
||
91 | * @return string |
||
92 | */ |
||
93 | protected function resolveContentArgumentName() |
||
110 | } |
||
111 |
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.