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 |
||
32 | class ViewBuilder |
||
33 | { |
||
34 | |||
35 | use ConfigTrait; |
||
36 | |||
37 | const ENGINE_FINDER_KEY = 'EngineFinder'; |
||
38 | const VIEW_FINDER_KEY = 'ViewFinder'; |
||
39 | |||
40 | /** |
||
41 | * ViewFinder instance. |
||
42 | * |
||
43 | * @since 0.1.0 |
||
44 | * |
||
45 | * @var ViewFinderInterface |
||
46 | */ |
||
47 | protected $viewFinder; |
||
48 | |||
49 | /** |
||
50 | * EngineFinder instance. |
||
51 | * |
||
52 | * @since 0.1.0 |
||
53 | * |
||
54 | * @var EngineFinderInterface |
||
55 | */ |
||
56 | protected $engineFinder; |
||
57 | |||
58 | /** |
||
59 | * Locations to scan for views. |
||
60 | * |
||
61 | * @since 0.1.0 |
||
62 | * |
||
63 | * @var LocationInterface[] |
||
64 | */ |
||
65 | protected $locations; |
||
66 | |||
67 | /** |
||
68 | * Instantiate a ViewBuilder object. |
||
69 | * |
||
70 | * @since 0.1.0 |
||
71 | * |
||
72 | * @param ConfigInterface $config Configuration settings. |
||
73 | * @param ViewFinderInterface|null $viewFinder ViewFinder instance. |
||
74 | * @param EngineFinderInterface|null $engineFinder EngineFinder instance. |
||
75 | * |
||
76 | * @throws FailedToProcessConfigException If the config could not be processed. |
||
77 | */ |
||
78 | 7 | public function __construct( |
|
87 | |||
88 | /** |
||
89 | * Create a new view for a given URI. |
||
90 | * |
||
91 | * @since 0.1.0 |
||
92 | * |
||
93 | * @param string $view View identifier to create a view for. |
||
94 | * @param string|null $type Type of view to create. |
||
95 | * |
||
96 | * @return ViewInterface Instance of the requested view. |
||
97 | */ |
||
98 | 17 | public function create($view, $type = null) |
|
107 | |||
108 | /** |
||
109 | * Get an Engine that can deal with the given URI. |
||
110 | * |
||
111 | * @since 0.1.0 |
||
112 | * |
||
113 | * @param string|false $uri URI to get an engine for. |
||
114 | * |
||
115 | * @return EngineInterface Instance of an engine that can deal with the given URI. |
||
116 | */ |
||
117 | 17 | public function getEngine($uri) |
|
121 | |||
122 | /** |
||
123 | * Get a view for a given URI, engine and type. |
||
124 | * |
||
125 | * @since 0.1.0 |
||
126 | * |
||
127 | * @param string $uri URI to get a view for. |
||
128 | * @param EngineInterface $engine Engine to use for the view. |
||
129 | * @param mixed $type Type of view to get. |
||
130 | * |
||
131 | * @return ViewInterface View that matches the given requirements. |
||
132 | */ |
||
133 | 17 | public function getView($uri, EngineInterface $engine, $type = null) |
|
141 | |||
142 | /** |
||
143 | * Get the ViewFinder instance. |
||
144 | * |
||
145 | * @since 0.1.0 |
||
146 | * |
||
147 | * @return ViewFinderInterface Instance of a ViewFinder. |
||
148 | */ |
||
149 | 17 | View Code Duplication | public function getViewFinder() |
158 | |||
159 | /** |
||
160 | * Get the EngineFinder instance. |
||
161 | * |
||
162 | * @since 0.1.0 |
||
163 | * |
||
164 | * @return EngineFinderInterface Instance of a EngineFinder. |
||
165 | */ |
||
166 | 17 | View Code Duplication | public function getEngineFinder() |
175 | |||
176 | /** |
||
177 | * Add a location to scan with the ViewFinder. |
||
178 | * |
||
179 | * @since 0.1.0 |
||
180 | * |
||
181 | * @param LocationInterface $location Location to scan with the ViewFinder. |
||
182 | */ |
||
183 | 17 | public function addLocation(LocationInterface $location) |
|
187 | |||
188 | /** |
||
189 | * Scan Locations for an URI that matches the specified criteria. |
||
190 | * |
||
191 | * @since 0.1.0 |
||
192 | * |
||
193 | * @param array $criteria Criteria to match. |
||
194 | * |
||
195 | * @return string|bool URI of the requested view, or false if not found. |
||
196 | */ |
||
197 | 17 | public function scanLocations(array $criteria) |
|
208 | |||
209 | /** |
||
210 | * Resolve the view type. |
||
211 | * |
||
212 | * @since 0.1.0 |
||
213 | * |
||
214 | * @param mixed $type Type of view that was requested. |
||
215 | * @param string $uri URI to get a view for. |
||
216 | * @param EngineInterface $engine Engine to use for the view. |
||
217 | * |
||
218 | * @return ViewInterface Resolved View object. |
||
219 | * @throws FailedToInstantiateViewException If the view type could not be resolved. |
||
220 | */ |
||
221 | View Code Duplication | protected function resolveType($type, $uri, EngineInterface $engine = null) |
|
242 | } |
||
243 |
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.