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 |
||
24 | abstract class AbstractTemplate extends AbstractEntity implements |
||
25 | LoggerAwareInterface, |
||
26 | TemplateInterface |
||
27 | { |
||
28 | use LoggerAwareTrait; |
||
29 | |||
30 | /** |
||
31 | * The cache of parsed template names. |
||
32 | * |
||
33 | * @var array |
||
34 | */ |
||
35 | protected static $templateNameCache = []; |
||
36 | |||
37 | /** |
||
38 | * @param array|\ArrayAccess $data The dependencies (app and logger). |
||
39 | */ |
||
40 | View Code Duplication | public function __construct($data = null) |
|
48 | |||
49 | /** |
||
50 | * Retrieve the template's identifier. |
||
51 | * |
||
52 | * @return string |
||
53 | */ |
||
54 | public function templateName() |
||
77 | |||
78 | /** |
||
79 | * Initialize the template with a request. |
||
80 | * |
||
81 | * @param RequestInterface $request The request to intialize. |
||
82 | * @return boolean Success / Failure. |
||
83 | */ |
||
84 | public function init(RequestInterface $request) |
||
89 | |||
90 | /** |
||
91 | * Give an opportunity to children classes to inject dependencies from a Pimple Container. |
||
92 | * |
||
93 | * Does nothing by default, reimplement in children classes. |
||
94 | * |
||
95 | * The `$container` DI-container (from `Pimple`) should not be saved or passed around, only to be used to |
||
96 | * inject dependencies (typically via setters). |
||
97 | * |
||
98 | * @param Container $container A dependencies container instance. |
||
99 | * @return void |
||
100 | */ |
||
101 | protected function setDependencies(Container $container) |
||
105 | } |
||
106 |
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.