Conditions | 47 |
Paths | 146 |
Total Lines | 74 |
Code Lines | 47 |
Lines | 49 |
Ratio | 66.22 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
83 | function rest_validate_request_arg( $value, $request, $param ) { |
||
84 | |||
85 | $attributes = $request->get_attributes(); |
||
86 | if ( ! isset( $attributes['args'][ $param ] ) || ! is_array( $attributes['args'][ $param ] ) ) { |
||
87 | return true; |
||
88 | } |
||
89 | $args = $attributes['args'][ $param ]; |
||
90 | |||
91 | View Code Duplication | if ( ! empty( $args['enum'] ) ) { |
|
92 | if ( ! in_array( $value, $args['enum'] ) ) { |
||
93 | return new WP_Error( 'rest_invalid_param', sprintf( __( '%s is not one of %s' ), $param, implode( ', ', $args['enum'] ) ) ); |
||
94 | } |
||
95 | } |
||
96 | |||
97 | View Code Duplication | if ( 'integer' === $args['type'] && ! is_numeric( $value ) ) { |
|
98 | return new WP_Error( 'rest_invalid_param', sprintf( __( '%s is not of type %s' ), $param, 'integer' ) ); |
||
99 | } |
||
100 | |||
101 | View Code Duplication | if ( 'string' === $args['type'] && ! is_string( $value ) ) { |
|
102 | return new WP_Error( 'rest_invalid_param', sprintf( __( '%s is not of type %s' ), $param, 'string' ) ); |
||
103 | } |
||
104 | |||
105 | View Code Duplication | if ( isset( $args['format'] ) ) { |
|
106 | switch ( $args['format'] ) { |
||
107 | case 'date-time' : |
||
108 | if ( ! rest_parse_date( $value ) ) { |
||
109 | return new WP_Error( 'rest_invalid_date', __( 'The date you provided is invalid.' ) ); |
||
110 | } |
||
111 | break; |
||
112 | |||
113 | case 'email' : |
||
114 | if ( ! is_email( $value ) ) { |
||
115 | return new WP_Error( 'rest_invalid_email', __( 'The email address you provided is invalid.' ) ); |
||
116 | } |
||
117 | break; |
||
118 | } |
||
119 | } |
||
120 | |||
121 | if ( in_array( $args['type'], array( 'numeric', 'integer' ) ) && ( isset( $args['minimum'] ) || isset( $args['maximum'] ) ) ) { |
||
122 | if ( isset( $args['minimum'] ) && ! isset( $args['maximum'] ) ) { |
||
123 | View Code Duplication | if ( ! empty( $args['exclusiveMinimum'] ) && $value <= $args['minimum'] ) { |
|
124 | return new WP_Error( 'rest_invalid_param', sprintf( __( '%s must be greater than %d (exclusive)' ), $param, $args['minimum'] ) ); |
||
125 | } else if ( empty( $args['exclusiveMinimum'] ) && $value < $args['minimum'] ) { |
||
126 | return new WP_Error( 'rest_invalid_param', sprintf( __( '%s must be greater than %d (inclusive)' ), $param, $args['minimum'] ) ); |
||
127 | } |
||
128 | } else if ( isset( $args['maximum'] ) && ! isset( $args['minimum'] ) ) { |
||
129 | View Code Duplication | if ( ! empty( $args['exclusiveMaximum'] ) && $value >= $args['maximum'] ) { |
|
130 | return new WP_Error( 'rest_invalid_param', sprintf( __( '%s must be less than %d (exclusive)' ), $param, $args['maximum'] ) ); |
||
131 | } else if ( empty( $args['exclusiveMaximum'] ) && $value > $args['maximum'] ) { |
||
132 | return new WP_Error( 'rest_invalid_param', sprintf( __( '%s must be less than %d (inclusive)' ), $param, $args['maximum'] ) ); |
||
133 | } |
||
134 | } else if ( isset( $args['maximum'] ) && isset( $args['minimum'] ) ) { |
||
135 | if ( ! empty( $args['exclusiveMinimum'] ) && ! empty( $args['exclusiveMaximum'] ) ) { |
||
136 | if ( $value >= $args['maximum'] || $value <= $args['minimum'] ) { |
||
137 | return new WP_Error( 'rest_invalid_param', sprintf( __( '%s must be between %d (exclusive) and %d (exclusive)' ), $param, $args['minimum'], $args['maximum'] ) ); |
||
138 | } |
||
139 | View Code Duplication | } else if ( empty( $args['exclusiveMinimum'] ) && ! empty( $args['exclusiveMaximum'] ) ) { |
|
140 | if ( $value >= $args['maximum'] || $value < $args['minimum'] ) { |
||
141 | return new WP_Error( 'rest_invalid_param', sprintf( __( '%s must be between %d (inclusive) and %d (exclusive)' ), $param, $args['minimum'], $args['maximum'] ) ); |
||
142 | } |
||
143 | } else if ( ! empty( $args['exclusiveMinimum'] ) && empty( $args['exclusiveMaximum'] ) ) { |
||
144 | if ( $value > $args['maximum'] || $value <= $args['minimum'] ) { |
||
145 | return new WP_Error( 'rest_invalid_param', sprintf( __( '%s must be between %d (exclusive) and %d (inclusive)' ), $param, $args['minimum'], $args['maximum'] ) ); |
||
146 | } |
||
147 | } else if ( empty( $args['exclusiveMinimum'] ) && empty( $args['exclusiveMaximum'] ) ) { |
||
148 | if ( $value > $args['maximum'] || $value < $args['minimum'] ) { |
||
149 | return new WP_Error( 'rest_invalid_param', sprintf( __( '%s must be between %d (inclusive) and %d (inclusive)' ), $param, $args['minimum'], $args['maximum'] ) ); |
||
150 | } |
||
151 | } |
||
152 | } |
||
153 | } |
||
154 | |||
155 | return true; |
||
156 | } |
||
157 | } |
||
200 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.