Complex classes like NativeGenerator 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 NativeGenerator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | abstract class NativeGenerator |
||
11 | { |
||
12 | |||
13 | /** |
||
14 | * Make data "safe" - all dangerous html/js/etc will be removed |
||
15 | * @param string $data |
||
16 | * @param bool $quotes |
||
17 | * @return string |
||
18 | */ |
||
19 | public static function safe($data, $quotes = false) |
||
40 | |||
41 | /** |
||
42 | * Remove all html tags from data |
||
43 | * @param string $data |
||
44 | * @param bool $quotes |
||
45 | * @return string |
||
46 | */ |
||
47 | public static function nohtml($data, $quotes = false) |
||
52 | |||
53 | /** |
||
54 | * Build property for html element from array. |
||
55 | * IMPORTANT: $property can be null-string (some times this happend's) - do not remove NULL!! |
||
56 | * @param array $property |
||
57 | * @return null|string |
||
58 | */ |
||
59 | public static function applyProperty(array $property = null) |
||
75 | |||
76 | /** |
||
77 | * Fast building single tag based on property's array |
||
78 | * @param string $tagName |
||
79 | * @param array|null $property |
||
80 | * @param bool $closeSlash |
||
81 | * @return string |
||
82 | */ |
||
83 | public static function buildSingleTag($tagName, array $property = null, $closeSlash = true) |
||
87 | |||
88 | /** |
||
89 | * Fast building container type tag based on property's and value |
||
90 | * @param string $tagName |
||
91 | * @param array|null $property |
||
92 | * @param null|string $value |
||
93 | * @param bool $valueHtml |
||
94 | * @return string |
||
95 | */ |
||
96 | public static function buildContainerTag($tagName, array $property = null, $value = null, $valueHtml = false) |
||
105 | |||
106 | /** |
||
107 | * Make parts of URI safe and usable |
||
108 | * @param string $string |
||
109 | * @param bool $encode |
||
110 | * @return string |
||
111 | */ |
||
112 | public static function safeUri($string, $encode = true) |
||
121 | |||
122 | /** |
||
123 | * Check if uri $source is equal to current uri point with array of $aliases and active $order set |
||
124 | * @param null $source |
||
125 | * @param array|null $aliases |
||
126 | * @param bool $order |
||
127 | * @return bool |
||
128 | */ |
||
129 | public static function isCurrentLink($source = null, array $aliases = null, $order = false) |
||
188 | |||
189 | /** |
||
190 | * Apply security for string to output as html |
||
191 | * @param string|null $object |
||
192 | * @param bool $html |
||
193 | * @param bool $secure |
||
194 | * @return null|string |
||
195 | */ |
||
196 | public static function applyEscape($object = null, $html = false, $secure = false) |
||
207 | |||
208 | /** |
||
209 | * Convert link-binding type to classic link with security filter |
||
210 | * @param string|array $uri |
||
211 | * @return string |
||
212 | */ |
||
213 | public static function convertLink($uri) |
||
227 | |||
228 | /** |
||
229 | * Encode safe uri links with slashes |
||
230 | * @param $uri |
||
231 | * @return string |
||
232 | */ |
||
233 | public static function encode($uri) |
||
239 | |||
240 | |||
241 | } |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.