| Conditions | 29 |
| Paths | > 20000 |
| Total Lines | 100 |
| Lines | 28 |
| Ratio | 28 % |
| 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 // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
||
| 176 | public function testFixture( $filename ) { |
||
| 177 | // Load fixture file. The important parts are the bits delimited with `~~~~~~~~`, the rest is ignored. |
||
| 178 | $contents = file_get_contents( $filename ); |
||
| 179 | $this->assertIsString( $contents, 'Fixture contents cannot be fetched' ); |
||
| 180 | if ( ! preg_match_all( '/^( {0,3})~~~~~~~~\S* (\S+)\n(.*?)\n {0,3}~~~~~~~~$/sm', $contents, $m, PREG_SET_ORDER ) ) { |
||
| 181 | $this->fail( 'Fixture is invalid' ); |
||
| 182 | } |
||
| 183 | $data = array( 'args' => array() ); |
||
| 184 | foreach ( $m as list( , $indent, $key, $value ) ) { |
||
|
|
|||
| 185 | if ( strlen( $indent ) > 0 ) { |
||
| 186 | // We take advantage of markdown's ability to indent fenced code blocks for readability. |
||
| 187 | // Unindent them here before processing the contents. |
||
| 188 | $value = preg_replace( '/^ {0,' . strlen( $indent ) . '}/m', '', $value ); |
||
| 189 | } |
||
| 190 | switch ( $key ) { |
||
| 191 | case 'args': |
||
| 192 | $data[ $key ] = json_decode( $value, true ); |
||
| 193 | $this->assertIsArray( $data[ $key ] ); |
||
| 194 | break; |
||
| 195 | case 'object': |
||
| 196 | case 'parse-output': |
||
| 197 | $data[ $key ] = Changelog::jsonUnserialize( json_decode( $value, true ) ); |
||
| 198 | break; |
||
| 199 | case 'parse-exception': |
||
| 200 | case 'format-exception': |
||
| 201 | list( $class, $message ) = explode( "\n", $value, 2 ); |
||
| 202 | $this->assertTrue( is_a( $class, Exception::class, true ), "$class is not an Exception" ); |
||
| 203 | $data[ $key ] = new $class( $message ); |
||
| 204 | break; |
||
| 205 | case 'changelog': |
||
| 206 | case 'format-output': |
||
| 207 | $data[ $key ] = $value; |
||
| 208 | break; |
||
| 209 | default: |
||
| 210 | $this->fail( "Unknown fixture key $key" ); |
||
| 211 | } |
||
| 212 | } |
||
| 213 | $this->assertTrue( isset( $data['changelog'] ) || isset( $data['object'] ), 'Must provide at least one of "changelog" or "object"' ); |
||
| 214 | $this->assertFalse( isset( $data['parse-output'] ) && isset( $data['parse-exception'] ), 'Cannot provide both "parse-output" and "parse-exception".' ); |
||
| 215 | $this->assertFalse( isset( $data['format-output'] ) && isset( $data['format-exception'] ), 'Cannot provide both "format-output" and "format-exception".' ); |
||
| 216 | |||
| 217 | // Run the tests! |
||
| 218 | $parser = $this->newParser( $data['args'] ); |
||
| 219 | try { |
||
| 220 | View Code Duplication | if ( isset( $data['changelog'] ) ) { |
|
| 221 | if ( isset( $data['parse-exception'] ) ) { |
||
| 222 | try { |
||
| 223 | $parser->parse( $data['changelog'] ); |
||
| 224 | $this->fail( 'Expected exception not thrown from parse()' ); |
||
| 225 | } catch ( Exception $ex ) { |
||
| 226 | $this->assertInstanceOf( get_class( $data['parse-exception'] ), $ex, 'Expected exception from parse()' ); |
||
| 227 | $this->assertStringContainsString( $data['parse-exception']->getMessage(), $ex->getMessage(), 'Expected exception from parse()' ); |
||
| 228 | } |
||
| 229 | } else { |
||
| 230 | $expect = isset( $data['parse-output'] ) ? $data['parse-output'] : $data['object']; |
||
| 231 | $this->assertEquals( $expect, $parser->parse( $data['changelog'] ), 'Output from parse()' ); |
||
| 232 | } |
||
| 233 | } |
||
| 234 | View Code Duplication | if ( isset( $data['object'] ) ) { |
|
| 235 | if ( isset( $data['format-exception'] ) ) { |
||
| 236 | try { |
||
| 237 | $parser->format( $data['object'] ); |
||
| 238 | $this->fail( 'Expected exception not thrown from format()' ); |
||
| 239 | } catch ( Exception $ex ) { |
||
| 240 | $this->assertInstanceOf( get_class( $data['format-exception'] ), $ex, 'Expected exception from format()' ); |
||
| 241 | $this->assertStringContainsString( $data['format-exception']->getMessage(), $ex->getMessage(), 'Expected exception from format()' ); |
||
| 242 | } |
||
| 243 | } else { |
||
| 244 | $expect = isset( $data['format-output'] ) ? $data['format-output'] : $data['changelog']; |
||
| 245 | $this->assertEquals( $expect, $parser->format( $data['object'] ), 'Output from format()' ); |
||
| 246 | } |
||
| 247 | } |
||
| 248 | } catch ( Exception $ex ) { |
||
| 249 | if ( $this->updateFixtures ) { |
||
| 250 | // Re-run parse and format to get the new outputs for the fixture update. |
||
| 251 | // writeFixture() will take care of deduplication. |
||
| 252 | unset( $data['parse-output'], $data['parse-exception'], $data['format-output'], $data['format-exception'] ); |
||
| 253 | if ( isset( $data['changelog'] ) ) { |
||
| 254 | try { |
||
| 255 | $data['parse-output'] = $parser->parse( $data['changelog'] ); |
||
| 256 | } catch ( Exception $ex ) { |
||
| 257 | $data['parse-exception'] = $ex; |
||
| 258 | } |
||
| 259 | } |
||
| 260 | if ( isset( $data['object'] ) ) { |
||
| 261 | try { |
||
| 262 | $data['format-output'] = $parser->format( $data['object'] ); |
||
| 263 | } catch ( Exception $ex ) { |
||
| 264 | $data['format-exception'] = $ex; |
||
| 265 | } |
||
| 266 | } |
||
| 267 | $this->writeFixture( $filename, $data ); |
||
| 268 | } |
||
| 269 | throw $ex; |
||
| 270 | } |
||
| 271 | if ( $this->updateFixtures ) { |
||
| 272 | // The test passed, so the fixture data is good. But re-write it to clean up formatting. |
||
| 273 | $this->writeFixture( $filename, $data ); |
||
| 274 | } |
||
| 275 | } |
||
| 276 | |||
| 296 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.