Conditions | 17 |
Paths | 96 |
Total Lines | 96 |
Code Lines | 59 |
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 |
||
144 | function crop_html( $string, $length = 256, $allowed_tags = array() ) { |
||
145 | $tags = $GLOBALS['allowedtags']; // Markup allowed in comments... |
||
146 | |||
147 | $tags['img'] = array( // ... plus images ... |
||
148 | 'alt' => true, |
||
149 | 'height' => true, |
||
150 | 'src' => true, |
||
151 | 'width' => true, |
||
152 | ); |
||
153 | |||
154 | // ... and some other basics |
||
155 | $tags['p'] = array(); |
||
156 | $tags['ul'] = array(); |
||
157 | $tags['ol'] = array(); |
||
158 | $tags['li'] = array(); |
||
159 | $tags['br'] = array(); |
||
160 | |||
161 | $tags = array_merge( $tags, $allowed_tags ); |
||
162 | |||
163 | // Clean up, then KSES to really lock it down |
||
164 | $string = trim( (string) $string ); |
||
165 | $string = preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', $string ); |
||
166 | $string = wp_kses( $string, $tags ); |
||
167 | |||
168 | $string = mb_convert_encoding( $string, 'HTML-ENTITIES', 'UTF-8' ); |
||
169 | $dom = new DOMDocument( '1.0', 'UTF-8' ); |
||
170 | |||
171 | // The @ is not enough to suppress errors when dealing with libxml, |
||
172 | // we have to tell it directly how we want to handle errors. |
||
173 | libxml_use_internal_errors( true ); |
||
174 | @$dom->loadHTML( "<html><body>$string</body></html>" ); |
||
175 | libxml_use_internal_errors( false ); |
||
176 | |||
177 | // Strip comment nodes, if any |
||
178 | $comment_nodes = self::get_comment_nodes( $dom->documentElement ); |
||
179 | foreach ( $comment_nodes as &$comment_node ) { |
||
180 | $comment_node->parentNode->removeChild( $comment_node ); |
||
181 | } |
||
182 | if ( $comment_nodes ) { |
||
183 | // Update the $string (some return paths work from just $string) |
||
184 | $string = $dom->saveHTML(); |
||
185 | $string = preg_replace( '/^<!DOCTYPE.+?>/', '', $string ); |
||
186 | $string = str_replace( array('<html>', '</html>', '<body>', '</body>' ), array( '', '', '', '' ), $string ); |
||
187 | $string = trim( $string ); |
||
188 | } |
||
189 | |||
190 | // Find the body |
||
191 | $body = false; |
||
192 | foreach ( $dom->childNodes as $child ) { |
||
193 | if ( XML_ELEMENT_NODE === $child->nodeType && 'html' === strtolower( $child->tagName ) ) { |
||
194 | $body = $child->firstChild; |
||
195 | break; |
||
196 | } |
||
197 | } |
||
198 | |||
199 | if ( !$body ) { |
||
200 | return self::crop_str( $string, $length ); |
||
201 | } |
||
202 | |||
203 | // If the text (without the markup) is shorter than $length, just return |
||
204 | if ( mb_strlen( $body->textContent, 'UTF-8' ) <= $length ) { |
||
205 | return $string; |
||
206 | } |
||
207 | |||
208 | $node = false; |
||
209 | do { |
||
210 | $node = self::remove_innermost_last_child( $body, $node_removed_from ); |
||
211 | $new_string_length = mb_strlen( $body->textContent, 'UTF-8' ); |
||
212 | } while ( $new_string_length > $length ); |
||
213 | |||
214 | $new_string = $dom->saveHTML( $body ); |
||
215 | $new_string = mb_substr( $new_string, 6, -7, 'UTF-8' ); // 6: <body>, 7: </body> |
||
216 | |||
217 | if ( !$node ) { |
||
218 | return $new_string ? $new_string : self::crop_str( $string, $length ); |
||
219 | } |
||
220 | |||
221 | $append_string_length = $length - $new_string_length; |
||
222 | |||
223 | if ( !$append_string_length ) { |
||
224 | return $new_string; |
||
225 | } |
||
226 | |||
227 | if ( $append_string_length > 1 && XML_TEXT_NODE === $node->nodeType ) { // 1: ellipsis |
||
228 | $append_string = self::crop_str( $node->textContent, $append_string_length ); // includes ellipsis |
||
229 | $append_node = $dom->createTextNode( $append_string ); |
||
230 | $node_removed_from->appendChild( $append_node ); |
||
231 | $new_string = $dom->saveHTML( $body ); |
||
232 | $new_string = mb_substr( $new_string, 6, -7, 'UTF-8' ); |
||
233 | } elseif ( $append_string_length > 9 && XML_ELEMENT_NODE === $node->nodeType && 'p' == strtolower( $node->nodeName ) ) { // 9: '<p>X{\xE2\x80\xA6}</p>' |
||
234 | $new_string .= '<p>' . self::crop_str( $node->textContent, $append_string_length - 8 ) . '</p>'; |
||
235 | } |
||
236 | |||
237 | // Clean up any empty Paragraphs that might have occurred after removing their children |
||
238 | return trim( preg_replace( '#<p>\s*</p>#i', '', $new_string ) ); |
||
239 | } |
||
240 | |||
313 |
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.