| Conditions | 10 |
| Paths | 49 |
| Total Lines | 72 |
| 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 |
||
| 38 | function render_block( $attributes, $content ) { |
||
| 39 | $save_in_post_content = get_attribute( $attributes, 'saveInPostContent' ); |
||
| 40 | |||
| 41 | if ( $save_in_post_content || ! class_exists( 'DOMDocument' ) ) { |
||
| 42 | return $content; |
||
| 43 | } |
||
| 44 | |||
| 45 | $element = get_attribute( $attributes, 'element' ); |
||
| 46 | $text = get_attribute( $attributes, 'text' ); |
||
| 47 | $unique_id = get_attribute( $attributes, 'uniqueId' ); |
||
| 48 | $classes = Jetpack_Gutenberg::block_classes( FEATURE_NAME, $attributes ); |
||
| 49 | |||
| 50 | $dom = new \DOMDocument(); |
||
| 51 | $button = $dom->createElement( $element, $content ); |
||
|
|
|||
| 52 | |||
| 53 | if ( 'input' === $element ) { |
||
| 54 | $button = $dom->createElement( 'input' ); |
||
| 55 | |||
| 56 | $attribute = $dom->createAttribute( 'value' ); |
||
| 57 | $attribute->value = $text; |
||
| 58 | $button->appendChild( $attribute ); |
||
| 59 | } else { |
||
| 60 | $button = $dom->createElement( $element, $text ); |
||
| 61 | } |
||
| 62 | |||
| 63 | $attribute = $dom->createAttribute( 'class' ); |
||
| 64 | $attribute->value = get_button_classes( $attributes ); |
||
| 65 | $button->appendChild( $attribute ); |
||
| 66 | |||
| 67 | $button_styles = get_button_styles( $attributes ); |
||
| 68 | if ( ! empty( $button_styles ) ) { |
||
| 69 | $attribute = $dom->createAttribute( 'style' ); |
||
| 70 | $attribute->value = $button_styles; |
||
| 71 | $button->appendChild( $attribute ); |
||
| 72 | } |
||
| 73 | |||
| 74 | $attribute = $dom->createAttribute( 'data-id-attr' ); |
||
| 75 | $attribute->value = empty( $unique_id ) ? 'placeholder' : $unique_id; |
||
| 76 | $button->appendChild( $attribute ); |
||
| 77 | if ( $unique_id ) { |
||
| 78 | $attribute = $dom->createAttribute( 'id' ); |
||
| 79 | $attribute->value = $unique_id; |
||
| 80 | $button->appendChild( $attribute ); |
||
| 81 | } |
||
| 82 | |||
| 83 | if ( 'a' === $element ) { |
||
| 84 | $attribute = $dom->createAttribute( 'href' ); |
||
| 85 | $attribute->value = get_attribute( $attributes, 'url' ); |
||
| 86 | $button->appendChild( $attribute ); |
||
| 87 | |||
| 88 | $attribute = $dom->createAttribute( 'target' ); |
||
| 89 | $attribute->value = '_blank'; |
||
| 90 | $button->appendChild( $attribute ); |
||
| 91 | |||
| 92 | $attribute = $dom->createAttribute( 'role' ); |
||
| 93 | $attribute->value = 'button'; |
||
| 94 | $button->appendChild( $attribute ); |
||
| 95 | |||
| 96 | $attribute = $dom->createAttribute( 'rel' ); |
||
| 97 | $attribute->value = 'noopener noreferrer'; |
||
| 98 | $button->appendChild( $attribute ); |
||
| 99 | } elseif ( 'button' === $element || 'input' === $element ) { |
||
| 100 | $attribute = $dom->createAttribute( 'type' ); |
||
| 101 | $attribute->value = 'submit'; |
||
| 102 | $button->appendChild( $attribute ); |
||
| 103 | } |
||
| 104 | |||
| 105 | $dom->appendChild( $button ); |
||
| 106 | |||
| 107 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
||
| 108 | return '<div class="' . esc_attr( $classes ) . '">' . $dom->saveHTML() . '</div>'; |
||
| 109 | } |
||
| 110 | |||
| 229 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.