| Conditions | 29 |
| Paths | > 20000 |
| Total Lines | 94 |
| Lines | 28 |
| Ratio | 29.79 % |
| 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 |
||
| 143 | public function testFixture( $filename ) { |
||
| 144 | $contents = file_get_contents( $filename ); |
||
| 145 | $this->assertIsString( $contents, 'Fixture contents cannot be fetched' ); |
||
| 146 | if ( ! preg_match_all( '/^( {0,3})~~~~~~~~\S* (\S+)\n(.*?)\n {0,3}~~~~~~~~$/sm', $contents, $m, PREG_SET_ORDER ) ) { |
||
| 147 | $this->fail( 'Fixture is invalid' ); |
||
| 148 | } |
||
| 149 | $data = array( 'args' => array() ); |
||
| 150 | foreach ( $m as list( , $indent, $key, $value ) ) { |
||
|
|
|||
| 151 | if ( strlen( $indent ) > 0 ) { |
||
| 152 | $value = preg_replace( '/^ {0,' . strlen( $indent ) . '}/m', '', $value ); |
||
| 153 | } |
||
| 154 | switch ( $key ) { |
||
| 155 | case 'args': |
||
| 156 | $data[ $key ] = json_decode( $value, true ); |
||
| 157 | $this->assertIsArray( $data[ $key ] ); |
||
| 158 | break; |
||
| 159 | case 'object': |
||
| 160 | case 'parse-output': |
||
| 161 | $data[ $key ] = Changelog::jsonUnserialize( json_decode( $value, true ) ); |
||
| 162 | break; |
||
| 163 | case 'parse-exception': |
||
| 164 | case 'format-exception': |
||
| 165 | list( $class, $message ) = explode( "\n", $value, 2 ); |
||
| 166 | $this->assertTrue( is_a( $class, Exception::class, true ), "$class is not an Exception" ); |
||
| 167 | $data[ $key ] = new $class( $message ); |
||
| 168 | break; |
||
| 169 | case 'changelog': |
||
| 170 | case 'format-output': |
||
| 171 | $data[ $key ] = $value; |
||
| 172 | break; |
||
| 173 | default: |
||
| 174 | $this->fail( "Unknown fixture key $key" ); |
||
| 175 | } |
||
| 176 | } |
||
| 177 | $this->assertTrue( isset( $data['changelog'] ) || isset( $data['object'] ), 'Must provide at least one of "changelog" or "object"' ); |
||
| 178 | $this->assertFalse( isset( $data['parse-output'] ) && isset( $data['parse-exception'] ), 'Cannot provide both "parse-output" and "parse-exception".' ); |
||
| 179 | $this->assertFalse( isset( $data['format-output'] ) && isset( $data['format-exception'] ), 'Cannot provide both "format-output" and "format-exception".' ); |
||
| 180 | |||
| 181 | $parser = $this->newParser( $data['args'] ); |
||
| 182 | |||
| 183 | try { |
||
| 184 | View Code Duplication | if ( isset( $data['changelog'] ) ) { |
|
| 185 | if ( isset( $data['parse-exception'] ) ) { |
||
| 186 | try { |
||
| 187 | $parser->parse( $data['changelog'] ); |
||
| 188 | $this->fail( 'Expected exception not thrown from parse()' ); |
||
| 189 | } catch ( Exception $ex ) { |
||
| 190 | $this->assertInstanceOf( get_class( $data['parse-exception'] ), $ex, 'Expected exception from parse()' ); |
||
| 191 | $this->assertStringContainsString( $data['parse-exception']->getMessage(), $ex->getMessage(), 'Expected exception from parse()' ); |
||
| 192 | } |
||
| 193 | } else { |
||
| 194 | $expect = isset( $data['parse-output'] ) ? $data['parse-output'] : $data['object']; |
||
| 195 | $this->assertEquals( $expect, $parser->parse( $data['changelog'] ), 'Output from parse()' ); |
||
| 196 | } |
||
| 197 | } |
||
| 198 | View Code Duplication | if ( isset( $data['object'] ) ) { |
|
| 199 | if ( isset( $data['format-exception'] ) ) { |
||
| 200 | try { |
||
| 201 | $parser->format( $data['object'] ); |
||
| 202 | $this->fail( 'Expected exception not thrown from format()' ); |
||
| 203 | } catch ( Exception $ex ) { |
||
| 204 | $this->assertInstanceOf( get_class( $data['format-exception'] ), $ex, 'Expected exception from format()' ); |
||
| 205 | $this->assertStringContainsString( $data['format-exception']->getMessage(), $ex->getMessage(), 'Expected exception from format()' ); |
||
| 206 | } |
||
| 207 | } else { |
||
| 208 | $expect = isset( $data['format-output'] ) ? $data['format-output'] : $data['changelog']; |
||
| 209 | $this->assertEquals( $expect, $parser->format( $data['object'] ), 'Output from format()' ); |
||
| 210 | } |
||
| 211 | } |
||
| 212 | } catch ( Exception $ex ) { |
||
| 213 | if ( $this->updateFixtures ) { |
||
| 214 | unset( $data['parse-output'], $data['parse-exception'], $data['format-output'], $data['format-exception'] ); |
||
| 215 | if ( isset( $data['changelog'] ) ) { |
||
| 216 | try { |
||
| 217 | $data['parse-output'] = $parser->parse( $data['changelog'] ); |
||
| 218 | } catch ( Exception $ex ) { |
||
| 219 | $data['parse-exception'] = $ex; |
||
| 220 | } |
||
| 221 | } |
||
| 222 | if ( isset( $data['object'] ) ) { |
||
| 223 | try { |
||
| 224 | $data['format-output'] = $parser->format( $data['object'] ); |
||
| 225 | } catch ( Exception $ex ) { |
||
| 226 | $data['format-exception'] = $ex; |
||
| 227 | } |
||
| 228 | } |
||
| 229 | $this->writeFixture( $filename, $data ); |
||
| 230 | } |
||
| 231 | throw $ex; |
||
| 232 | } |
||
| 233 | if ( $this->updateFixtures ) { |
||
| 234 | $this->writeFixture( $filename, $data ); |
||
| 235 | } |
||
| 236 | } |
||
| 237 | |||
| 257 |
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.