Complex classes like AbstractFrame 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 AbstractFrame, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | abstract class AbstractFrame extends DOMDocument implements FrameInterface |
||
23 | { |
||
24 | protected $xpath; |
||
25 | protected $nodes; |
||
26 | protected $format; |
||
27 | protected $command; |
||
28 | protected $mapping; |
||
29 | protected $extension; |
||
30 | |||
31 | public function __construct($import = null) |
||
32 | { |
||
33 | parent::__construct('1.0', 'UTF-8'); |
||
34 | $this->xmlStandalone = false; |
||
35 | $this->formatOutput = true; |
||
36 | |||
37 | if ($import instanceof DOMDocument) { |
||
38 | $node = $this->importNode($import->documentElement, true); |
||
39 | $this->appendChild($node); |
||
40 | |||
41 | // register namespaces |
||
42 | $this->xpath = new DOMXPath($this); |
||
43 | foreach (ObjectSpec::$specs as $prefix => $spec) { |
||
44 | $this->xpath->registerNamespace($prefix, $spec['xmlns']); |
||
45 | } |
||
46 | |||
47 | $this->registerNodeClass('\DOMElement', '\AfriCC\EPP\DOM\DOMElement'); |
||
48 | } |
||
49 | |||
50 | $this->getStructure(); |
||
51 | } |
||
52 | |||
53 | public function set($path = null, $value = null) |
||
54 | { |
||
55 | $path = $this->realxpath($path); |
||
56 | |||
57 | if (!isset($this->nodes[$path])) { |
||
58 | $path = $this->createNodes($path); |
||
59 | } |
||
60 | |||
61 | if ($value !== null) { |
||
62 | $this->nodes[$path]->nodeValue = $value; |
||
63 | } |
||
64 | |||
65 | return $this->nodes[$path]; |
||
66 | } |
||
67 | |||
68 | public function get($query) |
||
69 | { |
||
70 | $nodes = $this->xpath->query($query); |
||
71 | if ($nodes === null || $nodes->length === 0) { |
||
72 | return false; |
||
73 | } |
||
74 | |||
75 | // try to figure out what type is being requested |
||
76 | $last_bit = substr(strrchr($query, '/'), 1); |
||
77 | |||
78 | // @see http://stackoverflow.com/a/24730245/567193 |
||
79 | if ($nodes->length === 1 && ( |
||
80 | ($last_bit[0] === '@' && $nodes->item(0)->nodeType === XML_ATTRIBUTE_NODE) || |
||
81 | (stripos($last_bit, 'text()') === 0 && $nodes->item(0)->nodeType === XML_TEXT_NODE) |
||
82 | )) { |
||
83 | return $nodes->item(0)->nodeValue; |
||
84 | } else { |
||
85 | return $nodes; |
||
86 | } |
||
87 | } |
||
88 | |||
89 | public function __toString() |
||
93 | |||
94 | protected function createNodes($path) |
||
95 | { |
||
96 | $path_parts = explode('/', $path); |
||
97 | $node_path = null; |
||
98 | |||
99 | for ($i = 0, $limit = count($path_parts); $i < $limit; ++$i) { |
||
100 | $attr_name = $attr_value = null; |
||
101 | |||
102 | // if no namespace given, use root-namespace |
||
103 | if (strpos($path_parts[$i], ':') === false) { |
||
104 | $node_ns = 'epp'; |
||
105 | $node_name = $path_parts[$i]; |
||
106 | $path_parts[$i] = $node_ns . ':' . $node_name; |
||
107 | } else { |
||
108 | list($node_ns, $node_name) = explode(':', $path_parts[$i], 2); |
||
109 | } |
||
110 | |||
111 | // check for node-array |
||
112 | if (substr($node_name, -2) === '[]') { |
||
113 | $node_name = substr($node_name, 0, -2); |
||
114 | // get next key |
||
115 | $next_key = -1; |
||
116 | foreach (array_keys($this->nodes) as $each) { |
||
117 | if (preg_match('/' . preg_quote($node_ns . ':' . $node_name, '/') . '\[(\d+)\]$/', $each, $matches)) { |
||
118 | if ($matches[1] > $next_key) { |
||
119 | $next_key = (int) $matches[1]; |
||
120 | } |
||
121 | } |
||
122 | } |
||
123 | ++$next_key; |
||
124 | $path_parts[$i] = sprintf('%s:%s[%d]', $node_ns, $node_name, $next_key); |
||
125 | } |
||
126 | // direct node-array access |
||
127 | if (preg_match('/^(.*)\[(\d+)\]$/', $node_name, $matches)) { |
||
128 | $node_name = $matches[1]; |
||
129 | } |
||
130 | // check if attribute needs to be set |
||
131 | elseif (preg_match('/^(.*)\[@([a-z0-9]+)=\'([a-z0-9]+)\'\]$/i', $node_name, $matches)) { |
||
132 | $node_name = $matches[1]; |
||
133 | $attr_name = $matches[2]; |
||
134 | $attr_value = $matches[3]; |
||
135 | } |
||
136 | |||
137 | $node_path = implode('/', array_slice($path_parts, 0, $i + 1)); |
||
138 | |||
139 | if (isset($this->nodes[$node_path])) { |
||
140 | continue; |
||
141 | } |
||
142 | |||
143 | // resolve node namespace |
||
144 | $node_xmlns = ObjectSpec::xmlns($node_ns); |
||
145 | if ($node_xmlns === false) { |
||
146 | throw new Exception(sprintf('unknown namespace: %s', $node_ns)); |
||
147 | } |
||
148 | |||
149 | // create node (but don't explicitely define root-node) |
||
150 | if ($node_ns === 'epp') { |
||
151 | $this->nodes[$node_path] = $this->createElementNS($node_xmlns, $node_name); |
||
152 | } else { |
||
153 | $this->nodes[$node_path] = $this->createElementNS($node_xmlns, $node_ns . ':' . $node_name); |
||
154 | } |
||
155 | |||
156 | // set attribute |
||
157 | if ($attr_name !== null && $attr_value !== null) { |
||
158 | $this->nodes[$node_path]->setAttribute($attr_name, $attr_value); |
||
159 | } |
||
160 | |||
161 | // now append node to parent |
||
162 | if ($i === 0) { |
||
163 | $parent = $this; |
||
164 | } else { |
||
165 | $parent = $this->nodes[implode('/', array_slice($path_parts, 0, $i))]; |
||
166 | } |
||
167 | $parent->appendChild($this->nodes[$node_path]); |
||
168 | } |
||
169 | |||
170 | return $node_path; |
||
171 | } |
||
172 | |||
173 | protected function realxpath($path) |
||
174 | { |
||
175 | if ($path === null) { |
||
176 | $path_parts = []; |
||
177 | } |
||
178 | // absolute path |
||
179 | elseif (isset($path[1]) && $path[0] === '/' && $path[1] === '/') { |
||
180 | return substr($path, 2); |
||
181 | } else { |
||
182 | $path_parts = explode('/', $path); |
||
183 | } |
||
184 | |||
185 | if (!empty($this->mapping) && !empty($this->command)) { |
||
186 | array_unshift($path_parts, $this->mapping . ':' . $this->command); |
||
187 | } |
||
188 | |||
189 | if (!empty($this->command)) { |
||
190 | array_unshift($path_parts, 'epp:' . $this->command); |
||
191 | } |
||
192 | |||
193 | if (!empty($this->format)) { |
||
194 | array_unshift($path_parts, 'epp:' . $this->format); |
||
195 | } |
||
196 | |||
197 | array_unshift($path_parts, 'epp:epp'); |
||
198 | |||
199 | return implode('/', $path_parts); |
||
200 | } |
||
201 | |||
202 | private function getStructure() |
||
239 | |||
240 | private function className($class) |
||
241 | { |
||
251 | |||
252 | public function getExtensionName() |
||
253 | { |
||
256 | } |
||
257 |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.