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 Tag 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 Tag, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
35 | class Tag { |
||
36 | /** |
||
37 | * @var Tag[] Array of children tag elements |
||
38 | */ |
||
39 | public $tags = array(); |
||
40 | /** |
||
41 | * @var string Tag Type for Printable Tags |
||
42 | */ |
||
43 | public $type; |
||
44 | /** |
||
45 | * @var array Array of lines of Content |
||
46 | */ |
||
47 | public $content; |
||
48 | |||
49 | public $opt; |
||
50 | |||
51 | public $source; |
||
52 | /** |
||
53 | * Number of spaces for each Indent when doing pretty format of output |
||
54 | */ |
||
55 | const INDENT_SIZE = 2; |
||
56 | |||
57 | /** |
||
58 | * Initialize instance vars |
||
59 | */ |
||
60 | function __construct() { |
||
61 | $this->tags = array(); |
||
62 | $this->content = array(); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * @param array $path array of arrays[type/class/id] |
||
67 | * @return Tag[] The tags you are looking for |
||
68 | */ |
||
69 | function find($path) { |
||
70 | //var_dump($this->type, json_encode($path), $this->compare($path[0])); |
||
71 | $list = array(); |
||
72 | if ($this->compare($path[0])) { |
||
73 | if (count($path) == 1) { |
||
74 | $list[] = $this; |
||
75 | return $list; |
||
76 | } |
||
77 | array_shift($path); |
||
78 | } |
||
79 | foreach ($this->tags as $tag) |
||
80 | if ($found = $tag->find($path)) |
||
81 | $list = array_merge($list, $found); |
||
82 | return $list; |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * Replace a tag at $path with a new tag ($newTag) |
||
87 | * @param $path array Path Array |
||
88 | * @param $newTag Tag New tag to replace old tag with |
||
89 | * @return Tag |
||
90 | */ |
||
91 | function replace($path, Tag $newTag) { |
||
92 | if ($this->compare($path[0])) { |
||
93 | if (count($path) == 1) return $newTag; |
||
94 | array_shift($path); |
||
95 | } |
||
96 | foreach ($this->tags as $k => $tag) { |
||
97 | if ($r = $tag->replace($path, $newTag)) { |
||
98 | $r->addSnipContent($this->tags[$k]); |
||
99 | array_splice($this->tags, $k, 1, $r->tags); |
||
100 | } |
||
101 | } |
||
102 | return null; |
||
103 | } |
||
104 | |||
105 | function addSnipContent($contentTag, &$tagArray = array(), $key = 0) { |
||
106 | foreach ($this->tags as $k => $tag) |
||
107 | $tag->addSnipContent($contentTag, $this->tags, $k); |
||
108 | } |
||
109 | |||
110 | function compare($tic) { |
||
111 | if (isset($tic['type']) && $this->type != $tic['type']) |
||
112 | return false; |
||
113 | if (isset($tic['id']) && |
||
114 | !(isset($this->opt['id']) && $tic['id'] == $this->opt['id']) |
||
115 | ) |
||
116 | return false; |
||
117 | if (isset($tic['class']) && !(isset($this->opt['class']) |
||
118 | && !array_diff($tic['class'], $this->opt['class'])) |
||
119 | ) |
||
120 | return false; |
||
121 | return true; |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * Add a child tag to this tag |
||
126 | * @param Tag $tag Tag to add as child |
||
127 | * @param string $mode Mode to add child [append|prepend] |
||
128 | */ |
||
129 | function addChild(Tag $tag, $mode = "append") { |
||
130 | if ($mode == "prepend") |
||
131 | array_unshift($this->tags, $tag); |
||
132 | else |
||
133 | $this->tags[] = $tag; |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * Render html/php output to disk |
||
138 | * |
||
139 | * @param int $indent Number of spaces in current indent level |
||
140 | * @param boolean $doIndent Indent this tag |
||
141 | * @return string HTML/PHP Output |
||
142 | */ |
||
143 | function render($indent = 0, $doIndent = true) { |
||
144 | //if(strtoupper($this->type) == "A") var_dump($this); |
||
145 | $ind = $doIndent ? str_pad("", $indent, " ") : ""; |
||
146 | $oneliner = ((count($this->content) > 1 || $this->tags) ? false : true); |
||
147 | $out = $ind . $this->renderStTag() . ($oneliner ? "" : "\n"); |
||
148 | if ($this->content) $out .= $this->renderContent($ind, $oneliner); |
||
149 | foreach ($this->tags as $tag) |
||
150 | $out .= $tag->render($indent + self::INDENT_SIZE); |
||
151 | $out .= ($oneliner ? "" : $ind) . $this->renderEnTag() . "\n"; |
||
152 | return $out; |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * Apply indent, to content, and return as string |
||
157 | * |
||
158 | * @param string $pad Indent String |
||
159 | * @param bool $oneliner Render to fit single line |
||
160 | * @return string Indented Content |
||
161 | */ |
||
162 | function renderContent($pad = "", $oneliner = false) { |
||
163 | $out = ""; |
||
164 | foreach ($this->content as $c) |
||
165 | $out .= ($oneliner ? "" : $pad) . $c . ($oneliner ? "" : "\n"); |
||
166 | return $out; |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * Output the Start Tag for this element |
||
171 | */ |
||
172 | function renderStTag() { |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * Output the End Tag for this element |
||
177 | */ |
||
178 | function renderEnTag() { |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * Add content to this tag, one line at a time |
||
183 | * |
||
184 | * @param string $s One line of content |
||
185 | * @param int $strtype Type of string to parse, hamleString::TOKEN_* |
||
186 | */ |
||
187 | function addContent($s, $strtype = Text::TOKEN_HTML) { |
||
188 | if (trim($s)) { |
||
189 | $parse = new Text($s, $strtype); |
||
190 | $this->content[] = $parse->toHTML(); |
||
191 | } |
||
192 | } |
||
193 | |||
194 | } |
||
195 | |||
202 |