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 |
||
93 | View Code Duplication | static function validate( $data, $type = null ) { |
|
94 | if ( is_null( $data ) ) { |
||
95 | return $data; |
||
96 | } |
||
97 | switch( $type ) { |
||
98 | case 'bool': |
||
99 | return boolval( $data ); |
||
100 | case 'url': |
||
101 | return esc_url( $data ); |
||
102 | case 'on': |
||
103 | return ( 'on' == $data ? true : false ); |
||
104 | break; |
||
105 | case 'closed': |
||
106 | return ( 'closed' != $data ? true : false ); |
||
107 | case 'string': |
||
108 | return strval( $data ); |
||
109 | case 'int': |
||
110 | return ( is_numeric( $data ) ? intval( $data ) : 0 ); |
||
111 | case 'float': |
||
112 | return ( is_numeric( $data ) ? floatval( $data ) : 0 ); |
||
113 | case 'array': |
||
114 | return ( is_array( $data ) ? $data : array() ); |
||
115 | case 'rtrim-slash': |
||
116 | return strval( rtrim( $data, '/' ) ); |
||
117 | } |
||
118 | if ( is_string( $type ) && 'regex:' == substr( $type, 0, 6 ) ) { |
||
119 | return ( preg_match( substr( $type, 6 ), $data ) ? $data : null ); |
||
120 | } elseif ( is_array( $type ) ) { |
||
121 | // Is the array associative? |
||
122 | if ( count( array_filter( array_keys( $type ), 'is_string' ) ) ) { |
||
123 | foreach ( $type as $item => $check ) { |
||
124 | $data[ $item ] = self::validate( $data[ $item ], $check ); |
||
125 | } |
||
126 | return $data; |
||
127 | } else { |
||
128 | // check if the value exists in the array if not return the first value. |
||
129 | // Ex $type = array( 'open', 'closed' ); defaults to 'open' |
||
130 | return ( in_array( $data, $type ) ? $data: $type[0] ); |
||
131 | } |
||
132 | } |
||
133 | // Don't check for validity here |
||
134 | if ( 'no-validation' == $type ) { |
||
135 | return $data; |
||
136 | } |
||
137 | return null; |
||
138 | } |
||
139 | |||
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.