Complex classes like Proxy 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 Proxy, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class Proxy extends \ArrayObject implements DOMElement, SimpleXMLElement { |
||
19 | |||
20 | 1 | use \arc\traits\Proxy { |
|
21 | \arc\traits\Proxy::__construct as private ProxyConstruct; |
||
22 | \arc\traits\Proxy::__call as private ProxyCall; |
||
23 | } |
||
24 | |||
25 | private $parser = null; |
||
26 | |||
27 | 18 | public function __construct( $node, $parser) { |
|
31 | |||
32 | 2 | public function __toString() { |
|
35 | |||
36 | 10 | private function _isDomProperty( $name ) { |
|
37 | $domProperties = [ |
||
38 | 10 | 'tagName', 'nodeType', 'parentNode', |
|
39 | 'firstChild', 'lastChild', 'previousSibling', 'nextSibling', |
||
40 | 'ownerDocument', 'namespaceURI', 'prefix', |
||
41 | 'localName', 'baseURI', 'textContent' |
||
42 | ]; |
||
43 | 10 | return in_array( $name, $domProperties ); |
|
44 | } |
||
45 | |||
46 | 12 | private function _parseName( $name ) { |
|
47 | 12 | $ns = ''; |
|
48 | 12 | $name = trim($name); |
|
49 | 12 | $prefix = false; |
|
50 | 12 | if ( $name[0] == '{' ) { |
|
51 | 2 | list($ns, $name) = explode('}', $name); |
|
52 | 2 | $ns = substr($ns, 1); |
|
53 | 12 | } else if ( strpos($name, ':') !== false ) { |
|
54 | 4 | list ($ns, $name) = explode(':', $name); |
|
55 | 4 | if ( isset($this->parser->namespaces[$ns]) ) { |
|
56 | 4 | $prefix = $ns; |
|
57 | 4 | $ns = $this->parser->namespaces[$ns]; |
|
58 | } else { |
||
59 | $prefix = $this->lookupPrefix($ns); |
||
|
|||
60 | } |
||
61 | } |
||
62 | 12 | return [ $ns, $name, $prefix ]; |
|
63 | } |
||
64 | |||
65 | 10 | private function _getTargetProperty($name) { |
|
66 | 10 | $value = null; |
|
67 | list( $uri, $name, $prefix ) |
||
68 | 10 | = $this->_parseName($name); |
|
69 | 10 | if ( $uri ) { |
|
70 | 2 | $value = $this->target->children($uri)->{$name}; |
|
71 | 10 | } else if ( !$this->_isDomProperty($name) ) { |
|
72 | 10 | $value = $this->target->{$name}; |
|
73 | } else { |
||
74 | 4 | $dom = dom_import_simplexml($this->target); |
|
75 | 4 | if ( isset($dom) ) { |
|
76 | 4 | $value = $dom->{$name}; |
|
77 | } |
||
78 | } |
||
79 | 10 | return $value; |
|
80 | } |
||
81 | |||
82 | 18 | private function _proxyResult( $value ) { |
|
83 | 18 | if ( $value instanceof \DOMElement ) { |
|
84 | 4 | $value = simplexml_import_dom($value); |
|
85 | 18 | } else if ( $value instanceof \DOMNodeList ) { |
|
86 | $array = []; |
||
87 | for ( $i=0, $l=$value->length; $i<$l; $i ++ ) { |
||
88 | $array[$i] = $value[$i]; |
||
89 | } |
||
90 | $value = $array; |
||
91 | } |
||
92 | 18 | if ( $value instanceof \SimpleXMLElement ) { |
|
93 | 18 | $value = new static( $value, $this->parser ); |
|
94 | 16 | } else if ( is_array($value) ) { |
|
95 | 8 | foreach ( $value as $key => $subvalue ) { |
|
96 | 8 | $value[$key] = $this->_proxyResult( $subvalue ); |
|
97 | } |
||
98 | } |
||
99 | 18 | return $value; |
|
100 | } |
||
101 | |||
102 | 16 | public function __get( $name) { |
|
108 | |||
109 | 4 | public function __set( $name, $value ) { |
|
110 | 4 | if ($name == 'nodeValue') { |
|
111 | $this->target = $value; |
||
112 | } else { |
||
113 | 4 | list($uri, $name, $prefix) = $this->_parseName($name); |
|
114 | 4 | if ( $uri && !$this->isDefaultNamespace($uri) ) { |
|
115 | 2 | $el = $this->ownerDocument->createElementNS($uri, $prefix.':'.$name, $value); |
|
116 | 2 | $this->appendChild($el); |
|
117 | } else { |
||
118 | 2 | $this->target->{$name} = $value; |
|
119 | } |
||
120 | } |
||
121 | 4 | } |
|
122 | |||
123 | 8 | private function _domCall( $name, $args ) { |
|
124 | 8 | $dom = dom_import_simplexml($this->target); |
|
125 | 8 | foreach ( $args as $index => $arg ) { |
|
126 | 8 | if ( $arg instanceof \arc\xml\Proxy ) { |
|
127 | 2 | $args[$index] = dom_import_simplexml( $arg->nodeValue ); |
|
128 | 6 | } else if ( $arg instanceof \SimpleXMLElement ) { |
|
129 | $args[$index] = dom_import_simplexml( $arg ); |
||
130 | } |
||
131 | } |
||
132 | $importMethods = [ |
||
133 | 8 | 'appendChild', 'insertBefore', 'replaceChild' |
|
134 | ]; |
||
135 | 8 | if ( in_array( $name, $importMethods ) ) { |
|
136 | 4 | if ( isset($args[0]) && $args[0] instanceof \DOMNode ) { |
|
137 | 4 | if ( $args[0]->ownerDocument !== $this->ownerDocument ) { |
|
138 | 2 | $args[0] = $this->ownerDocument->importNode( $args[0], true); |
|
139 | } |
||
140 | } |
||
141 | } |
||
142 | 8 | return call_user_func_array( [ $dom, $name], $args ); |
|
143 | } |
||
144 | |||
145 | 8 | public function __call( $name, $args ) { |
|
152 | |||
153 | /** |
||
154 | * Search through the XML DOM with a single CSS selector |
||
155 | * @param string $query the CSS selector, most CSS 2 selectors work |
||
156 | * @return Proxy |
||
157 | */ |
||
158 | 8 | public function find( $query) { |
|
162 | |||
163 | /** |
||
164 | * Searches through the subtree for an element with the given id and returns it |
||
165 | * @param string $id |
||
166 | * @return Proxy |
||
167 | */ |
||
168 | 2 | public function getElementById( $id ) { |
|
171 | |||
172 | /** |
||
173 | * Register a namespace alias and URI to use in xpath and find |
||
174 | * @param string $prefix the alias for this namespace |
||
175 | * @param string $ns the URI for this namespace |
||
176 | */ |
||
177 | 4 | public function registerNamespace( $prefix, $ns ) { |
|
178 | 4 | if ( $this->target && $this->target instanceof \SimpleXMLElement ) { |
|
179 | 4 | $this->target->registerXPathNamespace($prefix, $ns); |
|
180 | } |
||
181 | 4 | $this->parser->namespaces[$prefix] = $ns; |
|
182 | 4 | } |
|
183 | |||
184 | 6 | public function offsetGet( $offset ) |
|
193 | |||
194 | 4 | public function offsetSet( $offset, $value ) |
|
195 | { |
||
196 | 4 | list( $uri, $name, $prefix ) = $this->_parseName($offset); |
|
197 | 4 | if ( $uri && !$this->isDefaultNamespace($uri) ) { |
|
198 | 2 | $this->setAttributeNS($uri, $prefix.':'.$name, $value); |
|
199 | } else { |
||
200 | 2 | $this->target[$name] = $value; |
|
203 | |||
204 | 2 | public function offsetUnset( $offset ) |
|
213 | } |
||
214 |
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: