Conditions | 11 |
Paths | 21 |
Total Lines | 48 |
Code Lines | 29 |
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 |
||
20 | public function sanitize() { |
||
21 | $nodes = $this->dom->getElementsByTagName( self::$tag ); |
||
22 | $num_nodes = $nodes->length; |
||
23 | if ( 0 === $num_nodes ) { |
||
24 | return; |
||
25 | } |
||
26 | |||
27 | for ( $i = $num_nodes - 1; $i >= 0; $i-- ) { |
||
28 | $node = $nodes->item( $i ); |
||
29 | $old_attributes = AMP_DOM_Utils::get_node_attributes_as_assoc_array( $node ); |
||
30 | |||
31 | if ( empty( $old_attributes['src'] ) ) { |
||
32 | $node->parentNode->removeChild( $node ); |
||
33 | continue; |
||
34 | } |
||
35 | |||
36 | $new_attributes = $this->filter_attributes( $old_attributes ); |
||
37 | |||
38 | // Try to extract dimensions for the image, if not set. |
||
39 | if ( ! isset( $new_attributes['width'] ) || ! isset( $new_attributes['height'] ) ) { |
||
40 | $dimensions = AMP_Image_Dimension_Extractor::extract( $new_attributes['src'] ); |
||
41 | if ( is_array( $dimensions ) ) { |
||
42 | $new_attributes['width'] = $dimensions[0]; |
||
43 | $new_attributes['height'] = $dimensions[1]; |
||
44 | } |
||
45 | } |
||
46 | |||
47 | // Final fallback when we have no dimensions. |
||
48 | if ( ! isset( $new_attributes['width'] ) || ! isset( $new_attributes['height'] ) ) { |
||
49 | $new_attributes['width'] = isset( $this->args['content_max_width'] ) ? $this->args['content_max_width'] : self::FALLBACK_WIDTH; |
||
50 | $new_attributes['height'] = self::FALLBACK_HEIGHT; |
||
51 | |||
52 | $this->add_or_append_attribute( $new_attributes, 'class', 'amp-wp-unknown-size' ); |
||
53 | } |
||
54 | |||
55 | $new_attributes = $this->enforce_sizes_attribute( $new_attributes ); |
||
56 | |||
57 | if ( $this->is_gif_url( $new_attributes['src'] ) ) { |
||
58 | $this->did_convert_elements = true; |
||
59 | $new_tag = 'amp-anim'; |
||
60 | } else { |
||
61 | $new_tag = 'amp-img'; |
||
62 | } |
||
63 | |||
64 | $new_node = AMP_DOM_Utils::create_node( $this->dom, $new_tag, $new_attributes ); |
||
65 | $node->parentNode->replaceChild( $new_node, $node ); |
||
66 | } |
||
67 | } |
||
68 | |||
110 |
As per the PSR-2 coding standard, default statements should not be wrapped in curly braces.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.