Conditions | 15 |
Paths | 121 |
Total Lines | 78 |
Code Lines | 44 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
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 | |||
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.