| Conditions | 9 |
| Paths | 20 |
| Total 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 |
||
| 93 | public function get_alternate_links() { |
||
| 94 | $blogs = MslsBlogCollection::instance(); |
||
| 95 | $mydata = MslsOptions::create(); |
||
| 96 | |||
| 97 | foreach ( $blogs->get_objects() as $blog ) { |
||
| 98 | $language = $blog->get_language(); |
||
| 99 | $url = $mydata->get_current_link(); |
||
| 100 | $current = ( $blog->userblog_id == MslsBlogCollection::instance()->get_current_blog_id() ); |
||
| 101 | $title = $blog->get_description(); |
||
| 102 | |||
| 103 | if ( ! $current ) { |
||
| 104 | switch_to_blog( $blog->userblog_id ); |
||
| 105 | |||
| 106 | if ( 'MslsOptions' != get_class( $mydata ) && ( is_null( $mydata ) || ! $mydata->has_value( $language ) ) ) { |
||
| 107 | restore_current_blog(); |
||
| 108 | continue; |
||
| 109 | } |
||
| 110 | |||
| 111 | $url = $mydata->get_permalink( $language ); |
||
| 112 | $title = $blog->get_description(); |
||
| 113 | |||
| 114 | restore_current_blog(); |
||
| 115 | } |
||
| 116 | |||
| 117 | if ( has_filter( 'msls_head_hreflang' ) ) { |
||
| 118 | /** |
||
| 119 | * Overrides the hreflang value |
||
| 120 | * @since 0.9.9 |
||
| 121 | * @param string $language |
||
| 122 | */ |
||
| 123 | $hreflang = (string) apply_filters( 'msls_head_hreflang', $language ); |
||
| 124 | } |
||
| 125 | else { |
||
| 126 | $hreflang = $blog->get_alpha2(); |
||
| 127 | } |
||
| 128 | |||
| 129 | if ( ! isset( $default ) ) { |
||
| 130 | $default = sprintf( |
||
| 131 | '<link rel="alternate" hreflang="x-default" href="%s" title="%s" />', |
||
| 132 | $url, |
||
| 133 | esc_attr( $title ) |
||
| 134 | ); |
||
| 135 | } |
||
| 136 | |||
| 137 | $arr[] = sprintf( |
||
| 138 | '<link rel="alternate" hreflang="%s" href="%s" title="%s" />', |
||
| 139 | $hreflang, |
||
| 140 | $url, |
||
| 141 | esc_attr( $title ) |
||
| 142 | ); |
||
| 143 | } |
||
| 144 | |||
| 145 | if ( 1 === count( $arr ) ) { |
||
| 146 | return $default; |
||
| 147 | } |
||
| 148 | else { |
||
| 149 | return implode( PHP_EOL, $arr ); |
||
| 150 | } |
||
| 151 | } |
||
| 152 | |||
| 230 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.