| Conditions | 16 |
| Paths | 4 |
| Total Lines | 82 |
| 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 |
||
| 92 | public function setup_http_request_cache() { |
||
| 93 | // This gets called before WP_UnitTestCase_Base::setUp(), so make sure the hooks are saved before we start adding some |
||
| 94 | // so they'll get removed correctly by WP_UnitTestCase_Base::tearDown(). |
||
| 95 | if ( ! self::$hooks_saved ) { |
||
| 96 | $this->_backup_hooks(); |
||
|
|
|||
| 97 | } |
||
| 98 | |||
| 99 | $request_args = array_flip( static::$request_args ); |
||
| 100 | if ( empty( static::$update_cache ) ) { |
||
| 101 | add_filter( |
||
| 102 | 'pre_http_request', |
||
| 103 | function ( $preempt, $parsed_args, $url ) use ( $request_args ) { |
||
| 104 | if ( $preempt ) { |
||
| 105 | // Something else already overrode it. |
||
| 106 | return $preempt; |
||
| 107 | } |
||
| 108 | if ( ! isset( static::$request_cache[ $url ] ) ) { |
||
| 109 | throw new UnexpectedValueException( "No cache for $url" ); |
||
| 110 | } |
||
| 111 | $args = array_intersect_key( $parsed_args, $request_args ); |
||
| 112 | ksort( $args ); |
||
| 113 | foreach ( static::$request_cache[ $url ] as $data ) { |
||
| 114 | if ( $data['args'] === $args ) { |
||
| 115 | $ret = $data['response']; |
||
| 116 | if ( is_string( $ret ) ) { |
||
| 117 | // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_unserialize |
||
| 118 | $ret = unserialize( $ret ); |
||
| 119 | } elseif ( is_array( $ret ) && isset( $ret['headers'] ) ) { |
||
| 120 | $headers = new Requests_Utility_CaseInsensitiveDictionary(); |
||
| 121 | foreach ( $ret['headers'] as $k => $v ) { |
||
| 122 | $headers[ $k ] = $v; |
||
| 123 | } |
||
| 124 | $ret['headers'] = $headers; |
||
| 125 | } |
||
| 126 | return $ret; |
||
| 127 | } |
||
| 128 | } |
||
| 129 | // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export |
||
| 130 | throw new UnexpectedValueException( "No cache for $url with the specified arguments\n" . var_export( $args, 1 ) ); |
||
| 131 | }, |
||
| 132 | 90, |
||
| 133 | 3 |
||
| 134 | ); |
||
| 135 | } else { |
||
| 136 | add_action( |
||
| 137 | 'http_api_debug', |
||
| 138 | function ( $response, $context, $class, $parsed_args, $url ) use ( $request_args ) { |
||
| 139 | $args = array_intersect_key( $parsed_args, $request_args ); |
||
| 140 | ksort( $args ); |
||
| 141 | |||
| 142 | if ( is_object( $response ) ) { |
||
| 143 | // Probably a WP_Error. |
||
| 144 | // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize |
||
| 145 | $response = serialize( $response ); |
||
| 146 | } elseif ( is_array( $response ) ) { |
||
| 147 | // We probably don't care about most of these fields. If it turns out you do, comment |
||
| 148 | // out the appropriate lines temporarily (or just add them back to the json manually). |
||
| 149 | $response['headers'] = iterator_to_array( $response['headers'] ); |
||
| 150 | unset( $response['http_response'] ); |
||
| 151 | unset( $response['cookies'] ); |
||
| 152 | unset( $response['headers'] ); |
||
| 153 | unset( $response['filename'] ); |
||
| 154 | } |
||
| 155 | |||
| 156 | if ( isset( static::$request_cache[ $url ] ) ) { |
||
| 157 | foreach ( static::$request_cache[ $url ] as $data ) { |
||
| 158 | if ( $data['args'] === $args ) { |
||
| 159 | // Duplicate. Hope the response is functionally the same. |
||
| 160 | return; |
||
| 161 | } |
||
| 162 | } |
||
| 163 | } |
||
| 164 | static::$request_cache[ $url ][] = array( |
||
| 165 | 'args' => $args, |
||
| 166 | 'response' => $response, |
||
| 167 | ); |
||
| 168 | }, |
||
| 169 | 10, |
||
| 170 | 5 |
||
| 171 | ); |
||
| 172 | } |
||
| 173 | } |
||
| 174 | |||
| 182 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.