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 |
||
22 | class SiteParser |
||
23 | { |
||
24 | const EVENT_PARSER_INIT = 'parser.initialised'; |
||
25 | |||
26 | const EVENT_PAGE_PARSING = 'page.parsing'; |
||
27 | |||
28 | const EVENT_PAGE_PARSED = 'page.parsed'; |
||
29 | |||
30 | const TOKEN_REDIRECT = 'redirect'; |
||
31 | |||
32 | /** |
||
33 | * @var string |
||
34 | */ |
||
35 | private $filter; |
||
36 | |||
37 | /** |
||
38 | * @var string |
||
39 | */ |
||
40 | private $srcDir; |
||
41 | |||
42 | /** |
||
43 | * @var array |
||
44 | */ |
||
45 | private $metaConfig; |
||
46 | |||
47 | /** |
||
48 | * @var EventDispatcher |
||
49 | */ |
||
50 | private $eventDispatcher; |
||
51 | |||
52 | /** |
||
53 | * @var PageParser |
||
54 | */ |
||
55 | private $pageParser; |
||
56 | |||
57 | /** |
||
58 | * @var Htaccess |
||
59 | */ |
||
60 | private $htaccess; |
||
61 | |||
62 | /** |
||
63 | * @var string |
||
64 | */ |
||
65 | private $publicDir; |
||
66 | |||
67 | /** |
||
68 | * @var bool |
||
69 | */ |
||
70 | private $async; |
||
71 | |||
72 | /** |
||
73 | * @var string |
||
74 | */ |
||
75 | private $environment; |
||
76 | |||
77 | /** |
||
78 | * @var SiteMap |
||
79 | */ |
||
80 | private $siteMap; |
||
81 | |||
82 | /** |
||
83 | * SiteParser constructor. |
||
84 | * |
||
85 | * @param string $srcDir |
||
86 | * @param string $publicDir |
||
87 | * @param string $environment |
||
88 | * @param bool $async |
||
89 | * @param EventDispatcher $eventDispatcher |
||
90 | * @param PageParser $pageParser |
||
91 | * @param Htaccess $htaccess |
||
92 | * @param SiteMap $siteMap |
||
93 | * @param array $metaConfig |
||
94 | */ |
||
95 | View Code Duplication | public function __construct( |
|
116 | |||
117 | /** |
||
118 | * Load a site from YAML configuration files in the `directories.src`/site directory. |
||
119 | * All YAML files are loaded and parsed into Page objects and added to a Site collection. |
||
120 | * |
||
121 | * @param array $routes |
||
122 | * |
||
123 | * @return Site |
||
124 | * @throws InvalidSiteException |
||
125 | * @see \Brendt\Stitcher\Site\Page |
||
126 | * @see \Brendt\Stitcher\Site\Site |
||
127 | */ |
||
128 | public function loadSite(array $routes = []) : Site { |
||
151 | |||
152 | /** |
||
153 | * @param Site $site |
||
154 | * @param string $route |
||
155 | * @param array $config |
||
156 | */ |
||
157 | private function loadPage(Site $site, string $route, array $config) { |
||
167 | |||
168 | /** |
||
169 | * Parse a path into usable data. |
||
170 | * |
||
171 | * @param array $routes |
||
172 | * @param string $filterValue |
||
173 | * |
||
174 | * @return array |
||
175 | * @throws TemplateNotFoundException |
||
176 | */ |
||
177 | public function parse($routes = [], string $filterValue = null) { |
||
204 | |||
205 | /** |
||
206 | * Create a page render process |
||
207 | * |
||
208 | * @param Page $page |
||
209 | * @param string|null $filterValue |
||
210 | * |
||
211 | * @return PageRenderProcess |
||
212 | */ |
||
213 | private function createPageRenderProcess(Page $page, string $filterValue = null) : PageRenderProcess { |
||
227 | |||
228 | /** |
||
229 | * @param string $filter |
||
230 | * |
||
231 | * @return SiteParser |
||
232 | */ |
||
233 | public function setFilter(string $filter) : SiteParser { |
||
238 | |||
239 | /** |
||
240 | * @return Meta |
||
241 | */ |
||
242 | private function createMeta() : Meta { |
||
251 | } |
||
252 |
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.