| Conditions | 24 |
| Paths | 23 |
| Total Lines | 46 |
| Code Lines | 35 |
| Lines | 46 |
| Ratio | 100 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 2 |
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 |
||
| 79 | View Code Duplication | static function validate( $data, $type = null ) { |
|
| 80 | if ( is_null( $data ) ) { |
||
| 81 | return $data; |
||
| 82 | } |
||
| 83 | switch( $type ) { |
||
| 84 | case 'bool': |
||
| 85 | return boolval( $data ); |
||
| 86 | case 'url': |
||
| 87 | return esc_url( $data ); |
||
| 88 | case 'on': |
||
| 89 | return ( 'on' == $data ? true : false ); |
||
| 90 | break; |
||
| 91 | case 'closed': |
||
| 92 | return ( 'closed' != $data ? true : false ); |
||
| 93 | case 'string': |
||
| 94 | return strval( $data ); |
||
| 95 | case 'int': |
||
| 96 | return ( is_numeric( $data ) ? intval( $data ) : 0 ); |
||
| 97 | case 'float': |
||
| 98 | return ( is_numeric( $data ) ? floatval( $data ) : 0 ); |
||
| 99 | case 'array': |
||
| 100 | return ( is_array( $data ) ? $data : array() ); |
||
| 101 | case 'rtrim-slash': |
||
| 102 | return strval( rtrim( $data, '/' ) ); |
||
| 103 | } |
||
| 104 | if ( is_string( $type ) && 'regex:' == substr( $type, 0, 6 ) ) { |
||
| 105 | return ( preg_match( substr( $type, 6 ), $data ) ? $data : null ); |
||
| 106 | } elseif ( is_array( $type ) ) { |
||
| 107 | // Is the array associative? |
||
| 108 | if ( count( array_filter( array_keys( $type ), 'is_string' ) ) ) { |
||
| 109 | foreach ( $type as $item => $check ) { |
||
| 110 | $data[ $item ] = self::validate( $data[ $item ], $check ); |
||
| 111 | } |
||
| 112 | return $data; |
||
| 113 | } else { |
||
| 114 | // check if the value exists in the array if not return the first value. |
||
| 115 | // Ex $type = array( 'open', 'closed' ); defaults to 'open' |
||
| 116 | return ( in_array( $data, $type ) ? $data: $type[0] ); |
||
| 117 | } |
||
| 118 | } |
||
| 119 | // Don't check for validity here |
||
| 120 | if ( 'no-validation' == $type ) { |
||
| 121 | return $data; |
||
| 122 | } |
||
| 123 | return null; |
||
| 124 | } |
||
| 125 | |||
| 173 |
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.