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:
Complex classes like LessRuleList often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use LessRuleList, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class LessRuleList |
||
10 | { |
||
11 | private $list = array(); |
||
12 | |||
13 | /** |
||
14 | * Add a new rule object to our list |
||
15 | * @param LessRule $rule |
||
16 | */ |
||
17 | 15 | public function addRule(LessRule $rule) |
|
21 | |||
22 | /** |
||
23 | * Build and returns a tree for the CSS input |
||
24 | * @return array |
||
25 | */ |
||
26 | 15 | protected function getTree() |
|
39 | |||
40 | /** |
||
41 | * Add support for direct descendants operator by aligning the spaces properly. |
||
42 | * the code below supports "html >p" since we split by spaces. A selector "html > p" would cause an |
||
43 | * additional tree level, we therefore normalize them with the two lines below. |
||
44 | * |
||
45 | * @param string $selector |
||
46 | * @return string |
||
47 | */ |
||
48 | 15 | protected function parseDirectDescendants($selector) |
|
54 | |||
55 | /** |
||
56 | * Support for pseudo classes by adding a space before ":" and also "&" to let less know that there |
||
57 | * shouldn't be a space when concatenating the nested selectors to a single css rule. We have to |
||
58 | * ignore every colon if it's wrapped by :not(...) as we don't nest this in LESS. |
||
59 | * |
||
60 | * @param string $selector |
||
61 | * @return string |
||
62 | */ |
||
63 | 15 | protected function parsePseudoClasses($selector) |
|
87 | |||
88 | /** |
||
89 | * Ensures that operators like "+" are properly combined with a "&" |
||
90 | * |
||
91 | * @param $selector |
||
92 | * @param array $characters |
||
93 | * @return string |
||
94 | */ |
||
95 | 15 | protected function parseSelectors($selector, array $characters) |
|
116 | |||
117 | /** |
||
118 | * Parse CSS input part into a LESS node |
||
119 | * @param $output |
||
120 | * @param $selectors |
||
121 | * @param $token |
||
122 | */ |
||
123 | 15 | protected function parseTreeNode(&$output, $selectors, $token) |
|
157 | |||
158 | /** |
||
159 | * Splits CSS selectors into an array, but only where it makes sense to create a new nested level in LESS/SCSS. |
||
160 | * We split "body div" into array(body, div), but we don't split "a[title='hello world']" and thus create |
||
161 | * array([title='hello world']) |
||
162 | * |
||
163 | * @param string $selector |
||
164 | * @return array |
||
165 | */ |
||
166 | 15 | protected function splitSelector($selector) |
|
199 | |||
200 | /** |
||
201 | * Format LESS nodes in a nicer way with indentation and proper brackets |
||
202 | * @param $token |
||
203 | * @param int $level |
||
204 | * @return string |
||
205 | */ |
||
206 | 15 | public function formatTokenAsLess(\aCssToken $token, $level = 0) |
|
230 | |||
231 | 15 | protected function formatAsLess($selector, $level = 0) |
|
253 | |||
254 | 15 | public function lessify() |
|
271 | } |
||
272 |
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.