| Conditions | 19 |
| Paths | 200 |
| Total Lines | 68 |
| Lines | 57 |
| Ratio | 83.82 % |
| 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 |
||
| 67 | protected function writeFixture( $filename, array $data ) { |
||
| 68 | $this->assertTrue( defined( 'JSON_THROW_ON_ERROR' ) ); |
||
| 69 | $this->assertTrue( isset( $data['changelog'] ) || isset( $data['object'] ), 'Must provide at least one of "changelog" or "object"' ); |
||
| 70 | $this->assertFalse( isset( $data['parse-output'] ) && isset( $data['parse-exception'] ), 'Cannot provide both "parse-output" and "parse-exception".' ); |
||
| 71 | $this->assertFalse( isset( $data['format-output'] ) && isset( $data['format-exception'] ), 'Cannot provide both "format-output" and "format-exception".' ); |
||
| 72 | $jsonFlags = JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR; // phpcs:ignore PHPCompatibility.Constants.NewConstants.json_throw_on_errorFound |
||
| 73 | |||
| 74 | $contents = "# Parser text fixture file\n"; |
||
| 75 | View Code Duplication | if ( ! empty( $data['args'] ) ) { |
|
| 76 | $this->assertIsArray( $data['args'] ); |
||
| 77 | $contents .= "\n## Constructor args\n"; |
||
| 78 | $contents .= " ~~~~~~~~json args\n"; |
||
| 79 | $contents .= ' ' . str_replace( "\n", "\n ", json_encode( $data['args'], $jsonFlags ) ) . "\n"; |
||
| 80 | $contents .= " ~~~~~~~~\n"; |
||
| 81 | } |
||
| 82 | View Code Duplication | if ( isset( $data['changelog'] ) ) { |
|
| 83 | $this->assertIsString( $data['changelog'] ); |
||
| 84 | $contents .= "\n## Changelog file\n"; |
||
| 85 | $contents .= " ~~~~~~~~markdown changelog\n"; |
||
| 86 | $contents .= ' ' . str_replace( "\n", "\n ", $data['changelog'] ) . "\n"; |
||
| 87 | $contents .= " ~~~~~~~~\n"; |
||
| 88 | } |
||
| 89 | View Code Duplication | if ( isset( $data['object'] ) ) { |
|
| 90 | $this->assertInstanceOf( Changelog::class, $data['object'] ); |
||
| 91 | $contents .= "\n## Changelog object\n"; |
||
| 92 | $contents .= " ~~~~~~~~json object\n"; |
||
| 93 | $contents .= ' ' . str_replace( "\n", "\n ", json_encode( $data['object'], $jsonFlags ) ) . "\n"; |
||
| 94 | $contents .= " ~~~~~~~~\n"; |
||
| 95 | } |
||
| 96 | View Code Duplication | if ( isset( $data['changelog'] ) ) { |
|
| 97 | if ( isset( $data['parse-exception'] ) ) { |
||
| 98 | $this->assertInstanceOf( Exception::class, $data['parse-exception'] ); |
||
| 99 | $contents .= "\n## Expected exception from `parse()`\n"; |
||
| 100 | $contents .= " ~~~~~~~~text parse-exception\n"; |
||
| 101 | $contents .= ' ' . get_class( $data['parse-exception'] ) . "\n"; |
||
| 102 | $contents .= ' ' . str_replace( "\n", "\n ", $data['parse-exception']->getMessage() ) . "\n"; |
||
| 103 | $contents .= " ~~~~~~~~\n"; |
||
| 104 | } elseif ( isset( $data['parse-output'] ) && ! ( isset( $data['object'] ) && $data['object'] == $data['parse-output'] ) ) { // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison |
||
| 105 | $this->assertInstanceOf( Changelog::class, $data['parse-output'] ); |
||
| 106 | $contents .= "\n## Expected output from `parse()`\n"; |
||
| 107 | $contents .= " ~~~~~~~~json parse-output\n"; |
||
| 108 | $contents .= ' ' . str_replace( "\n", "\n ", json_encode( $data['parse-output'], $jsonFlags ) ) . "\n"; |
||
| 109 | $contents .= " ~~~~~~~~\n"; |
||
| 110 | } elseif ( ! isset( $data['object'] ) ) { |
||
| 111 | $this->fail( 'At least one of "object", "parse-output", or "parse-exception" is required when "changelog" is given.' ); |
||
| 112 | } |
||
| 113 | } |
||
| 114 | View Code Duplication | if ( isset( $data['object'] ) ) { |
|
| 115 | if ( isset( $data['format-exception'] ) ) { |
||
| 116 | $this->assertInstanceOf( Exception::class, $data['format-exception'] ); |
||
| 117 | $contents .= "\n## Expected exception from `format()`\n"; |
||
| 118 | $contents .= " ~~~~~~~~text format-exception\n"; |
||
| 119 | $contents .= ' ' . get_class( $data['format-exception'] ) . "\n"; |
||
| 120 | $contents .= ' ' . str_replace( "\n", "\n ", $data['format-exception']->getMessage() ) . "\n"; |
||
| 121 | $contents .= " ~~~~~~~~\n"; |
||
| 122 | } elseif ( isset( $data['format-output'] ) && ! ( isset( $data['changelog'] ) && $data['changelog'] == $data['format-output'] ) ) { // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison |
||
| 123 | $this->assertIsString( $data['format-output'] ); |
||
| 124 | $contents .= "\n## Expected output from `format()`\n"; |
||
| 125 | $contents .= " ~~~~~~~~markdown format-output\n"; |
||
| 126 | $contents .= ' ' . str_replace( "\n", "\n ", $data['format-output'] ) . "\n"; |
||
| 127 | $contents .= " ~~~~~~~~\n"; |
||
| 128 | } elseif ( ! isset( $data['changelog'] ) ) { |
||
| 129 | $this->fail( 'At least one of "changelog", "format-output", or "format-exception" is required when "object" is given.' ); |
||
| 130 | } |
||
| 131 | } |
||
| 132 | |||
| 133 | file_put_contents( $filename, $contents ); |
||
| 134 | } |
||
| 135 | |||
| 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.