| Conditions | 24 |
| Paths | 23 |
| Total Lines | 46 |
| Code Lines | 35 |
| Lines | 46 |
| Ratio | 100 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 140 | View Code Duplication | static function validate_option( $data, $type = null ) { |
|
| 141 | if ( is_null( $data ) ) { |
||
| 142 | return $data; |
||
| 143 | } |
||
| 144 | switch( $type ) { |
||
| 145 | case 'bool': |
||
| 146 | return boolval( $data ); |
||
| 147 | case 'url': |
||
| 148 | return esc_url( $data ); |
||
| 149 | case 'on': |
||
| 150 | return ( 'on' == $data ? true : false ); |
||
| 151 | break; |
||
| 152 | case 'closed': |
||
| 153 | return ( 'closed' != $data ? true : false ); |
||
| 154 | case 'string': |
||
| 155 | return strval( $data ); |
||
| 156 | case 'int': |
||
| 157 | return ( is_numeric( $data ) ? intval( $data ) : 0 ); |
||
| 158 | case 'float': |
||
| 159 | return ( is_numeric( $data ) ? floatval( $data ) : 0 ); |
||
| 160 | case 'array': |
||
| 161 | return ( is_array( $data ) ? $data : array() ); |
||
| 162 | case 'rtrim-slash': |
||
| 163 | return strval( rtrim( $data, '/' ) ); |
||
| 164 | } |
||
| 165 | if ( is_string( $type ) && 'regex:' == substr( $type, 0, 6 ) ) { |
||
| 166 | return ( preg_match( substr( $type, 6 ), $data ) ? $data : null ); |
||
| 167 | } elseif ( is_array( $type ) ) { |
||
| 168 | // Is the array associative? |
||
| 169 | if ( count( array_filter( array_keys( $type ), 'is_string' ) ) ) { |
||
| 170 | foreach ( $type as $item => $check ) { |
||
| 171 | $data[ $item ] = self::validate( $data[ $item ], $check ); |
||
| 172 | } |
||
| 173 | return $data; |
||
| 174 | } else { |
||
| 175 | // check if the value exists in the array if not return the first value. |
||
| 176 | // Ex $type = array( 'open', 'closed' ); defaults to 'open' |
||
| 177 | return ( in_array( $data, $type ) ? $data: $type[0] ); |
||
| 178 | } |
||
| 179 | } |
||
| 180 | // Don't check for validity here |
||
| 181 | if ( 'no-validation' == $type ) { |
||
| 182 | return $data; |
||
| 183 | } |
||
| 184 | return null; |
||
| 185 | } |
||
| 186 | } |
||
| 187 |
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.