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 |
||
27 | class SelectFactory implements FactoryInterface, MutableCreationOptionsInterface |
||
28 | { |
||
29 | |||
30 | /** |
||
31 | * Creation options. |
||
32 | * |
||
33 | * @var array |
||
34 | */ |
||
35 | protected $options = []; |
||
36 | |||
37 | |||
38 | /** |
||
39 | * Creates a tree select form element |
||
40 | * |
||
41 | * @param ContainerInterface $container |
||
42 | * @param string $requestedName |
||
43 | * @param array|null $options |
||
44 | * |
||
45 | * @return Select |
||
46 | * @throws \RuntimeException |
||
47 | * @throws \DomainException |
||
48 | */ |
||
49 | public function __invoke(ContainerInterface $container, $requestedName, array $options = null) |
||
104 | |||
105 | /** |
||
106 | * Set creation options |
||
107 | * |
||
108 | * @param array $options |
||
109 | * |
||
110 | * @return void |
||
111 | */ |
||
112 | public function setCreationOptions(array $options) |
||
116 | |||
117 | |||
118 | /** |
||
119 | * Create a tree select form element. |
||
120 | * |
||
121 | * @internal |
||
122 | * proxies to {@link __invoke}. |
||
123 | * |
||
124 | * @param ServiceLocatorInterface $serviceLocator |
||
125 | * @return Select |
||
126 | */ |
||
127 | public function createService(ServiceLocatorInterface $serviceLocator) |
||
135 | |||
136 | /** |
||
137 | * Create value options from a node. |
||
138 | * |
||
139 | * @param NodeInterface $node |
||
140 | * @param bool $allowSelectNodes |
||
141 | * @param bool $isRoot |
||
142 | * |
||
143 | * @return array |
||
144 | */ |
||
145 | protected function createValueOptions(NodeInterface $node, $allowSelectNodes = false, $isRoot=true) |
||
173 | |||
174 | /** |
||
175 | * Get item value for use in the option tag. |
||
176 | * |
||
177 | * @param NodeInterface $item |
||
178 | * |
||
179 | * @return string item value prepended with all parent values except root node. |
||
180 | * @todo This code is currently duplicated in \Core\Form\Tree\Select. |
||
181 | */ |
||
182 | View Code Duplication | private function getItemValue(NodeInterface $item) |
|
197 | } |
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.