| Conditions | 10 |
| Paths | 1 |
| Total Lines | 241 |
| Code Lines | 147 |
| 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 |
||
| 66 | public static function dataSkipCollectOnMatchIgnoreReferences(): iterable |
||
| 67 | { |
||
| 68 | $mkdirBefore = static function (string $path) { |
||
| 69 | if (is_dir($path)) { |
||
| 70 | @rmdir($path); |
||
| 71 | } |
||
| 72 | }; |
||
| 73 | $mkdirOperation = static function (string $path) { |
||
| 74 | mkdir($path, 0777, true); |
||
| 75 | }; |
||
| 76 | $mkdirAfter = $mkdirBefore; |
||
| 77 | |||
| 78 | yield 'mkdir matched' => [ |
||
| 79 | $path = __DIR__ . DIRECTORY_SEPARATOR . 'stub' . DIRECTORY_SEPARATOR . 'internal', |
||
| 80 | $mkdirBefore, |
||
| 81 | [], |
||
| 82 | [], |
||
| 83 | $mkdirOperation, |
||
| 84 | $mkdirAfter, |
||
| 85 | [ |
||
| 86 | 'mkdir' => [ |
||
| 87 | ['path' => $path, 'args' => ['mode' => 0777, 'options' => 9]], // 9 for some reason |
||
| 88 | ], |
||
| 89 | ], |
||
| 90 | ]; |
||
| 91 | yield 'mkdir ignored by path' => [ |
||
| 92 | $path, |
||
| 93 | $mkdirBefore, |
||
| 94 | [basename(__FILE__, '.php')], |
||
| 95 | [], |
||
| 96 | $mkdirOperation, |
||
| 97 | $mkdirAfter, |
||
| 98 | [], |
||
| 99 | ]; |
||
| 100 | yield 'mkdir ignored by class' => [ |
||
| 101 | $path, |
||
| 102 | $mkdirBefore, |
||
| 103 | [], |
||
| 104 | [self::class], |
||
| 105 | $mkdirOperation, |
||
| 106 | $mkdirAfter, |
||
| 107 | [], |
||
| 108 | ]; |
||
| 109 | |||
| 110 | $renameBefore = static function (string $path) { |
||
| 111 | if (!is_dir(dirname($path))) { |
||
| 112 | mkdir(dirname($path), 0777, true); |
||
| 113 | } |
||
| 114 | if (!is_file($path)) { |
||
| 115 | touch($path); |
||
| 116 | } |
||
| 117 | }; |
||
| 118 | $renameOperation = static function (string $path) { |
||
| 119 | rename($path, $path . '.renamed'); |
||
| 120 | }; |
||
| 121 | $renameAfter = static function (string $path) { |
||
| 122 | FileHelper::removeDirectory(dirname($path)); |
||
| 123 | }; |
||
| 124 | |||
| 125 | yield 'rename matched' => [ |
||
| 126 | $path = __DIR__ . DIRECTORY_SEPARATOR . 'stub' . DIRECTORY_SEPARATOR . 'file-to-rename.txt', |
||
| 127 | $renameBefore, |
||
| 128 | [], |
||
| 129 | [], |
||
| 130 | $renameOperation, |
||
| 131 | $renameAfter, |
||
| 132 | [ |
||
| 133 | 'rename' => [ |
||
| 134 | ['path' => $path, 'args' => ['path_to' => $path . '.renamed']], |
||
| 135 | ], |
||
| 136 | ], |
||
| 137 | ]; |
||
| 138 | yield 'rename ignored by path' => [ |
||
| 139 | $path, |
||
| 140 | $renameBefore, |
||
| 141 | [ basename(__FILE__, '.php') ], |
||
| 142 | [], |
||
| 143 | $renameOperation, |
||
| 144 | $renameAfter, |
||
| 145 | [], |
||
| 146 | ]; |
||
| 147 | yield 'rename ignored by class' => [ |
||
| 148 | $path, |
||
| 149 | $renameBefore, |
||
| 150 | [], |
||
| 151 | [self::class], |
||
| 152 | $renameOperation, |
||
| 153 | $renameAfter, |
||
| 154 | [], |
||
| 155 | ]; |
||
| 156 | |||
| 157 | $rmdirBefore = static function (string $path): void { |
||
| 158 | if (!is_dir($path)) { |
||
| 159 | mkdir($path, 0777, true); |
||
| 160 | } |
||
| 161 | }; |
||
| 162 | $rmdirOperation = static function (string $path): void { |
||
| 163 | rmdir($path); |
||
| 164 | }; |
||
| 165 | $rmdirAfter = static function (string $path): void { |
||
| 166 | if (is_dir($path)) { |
||
| 167 | rmdir($path); |
||
| 168 | } |
||
| 169 | }; |
||
| 170 | |||
| 171 | yield 'rmdir matched' => [ |
||
| 172 | $path = __DIR__ . DIRECTORY_SEPARATOR . 'stub' . DIRECTORY_SEPARATOR . 'dir-to-remove', |
||
| 173 | $rmdirBefore, |
||
| 174 | [], |
||
| 175 | [], |
||
| 176 | $rmdirOperation, |
||
| 177 | $rmdirAfter, |
||
| 178 | [ |
||
| 179 | 'rmdir' => [ |
||
| 180 | ['path' => $path, 'args' => ['options' => 8]], // 8 for some reason |
||
| 181 | ], |
||
| 182 | ], |
||
| 183 | ]; |
||
| 184 | yield 'rmdir ignored by path' => [ |
||
| 185 | $path, |
||
| 186 | $rmdirBefore, |
||
| 187 | [basename(__FILE__, '.php')], |
||
| 188 | [], |
||
| 189 | $rmdirOperation, |
||
| 190 | $rmdirAfter, |
||
| 191 | [], |
||
| 192 | ]; |
||
| 193 | yield 'rmdir ignored by class' => [ |
||
| 194 | $path, |
||
| 195 | $rmdirBefore, |
||
| 196 | [], |
||
| 197 | [self::class], |
||
| 198 | $rmdirOperation, |
||
| 199 | $rmdirAfter, |
||
| 200 | [], |
||
| 201 | ]; |
||
| 202 | |||
| 203 | $unlinkBefore = static function (string $path) { |
||
| 204 | if (!is_dir(dirname($path))) { |
||
| 205 | mkdir(dirname($path), 0777, true); |
||
| 206 | } |
||
| 207 | if (!is_file($path)) { |
||
| 208 | touch($path); |
||
| 209 | } |
||
| 210 | }; |
||
| 211 | $unlinkOperation = static function (string $path) { |
||
| 212 | unlink($path); |
||
| 213 | }; |
||
| 214 | $unlinkAfter = static function (string $path) { |
||
| 215 | FileHelper::removeDirectory(dirname($path)); |
||
| 216 | }; |
||
| 217 | |||
| 218 | yield 'unlink matched' => [ |
||
| 219 | $path = __DIR__ . DIRECTORY_SEPARATOR . 'stub' . DIRECTORY_SEPARATOR . 'file-to-unlink.txt', |
||
| 220 | $unlinkBefore, |
||
| 221 | [], |
||
| 222 | [], |
||
| 223 | $unlinkOperation, |
||
| 224 | $unlinkAfter, |
||
| 225 | [ |
||
| 226 | 'unlink' => [ |
||
| 227 | ['path' => $path, 'args' => []], |
||
| 228 | ], |
||
| 229 | ], |
||
| 230 | ]; |
||
| 231 | yield 'unlink ignored by path' => [ |
||
| 232 | $path, |
||
| 233 | $unlinkBefore, |
||
| 234 | [basename(__FILE__, '.php')], |
||
| 235 | [], |
||
| 236 | $unlinkOperation, |
||
| 237 | $unlinkAfter, |
||
| 238 | [], |
||
| 239 | ]; |
||
| 240 | yield 'unlink ignored by class' => [ |
||
| 241 | $path, |
||
| 242 | $unlinkBefore, |
||
| 243 | [], |
||
| 244 | [self::class], |
||
| 245 | $unlinkOperation, |
||
| 246 | $unlinkAfter, |
||
| 247 | [], |
||
| 248 | ]; |
||
| 249 | |||
| 250 | $fileStreamBefore = static function (string $path) { |
||
| 251 | if (!is_dir(dirname($path))) { |
||
| 252 | mkdir(dirname($path), 0777, true); |
||
| 253 | } |
||
| 254 | if (!is_file($path)) { |
||
| 255 | touch($path); |
||
| 256 | } |
||
| 257 | }; |
||
| 258 | $fileStreamOperation = static function (string $path) { |
||
| 259 | $stream = fopen($path, 'a+'); |
||
| 260 | fwrite($stream, 'test'); |
||
| 261 | fread($stream, 4); |
||
| 262 | fseek($stream, 0); |
||
| 263 | ftell($stream); |
||
| 264 | feof($stream); |
||
| 265 | ftruncate($stream, 0); |
||
| 266 | fstat($stream); |
||
| 267 | flock($stream, LOCK_EX); |
||
| 268 | fclose($stream); |
||
| 269 | }; |
||
| 270 | $fileStreamAfter = static function (string $path) { |
||
| 271 | FileHelper::removeDirectory(dirname($path)); |
||
| 272 | }; |
||
| 273 | |||
| 274 | yield 'file stream matched' => [ |
||
| 275 | $path = __DIR__ . DIRECTORY_SEPARATOR . 'stub' . DIRECTORY_SEPARATOR . 'file-to-stream.txt', |
||
| 276 | $fileStreamBefore, |
||
| 277 | [], |
||
| 278 | [], |
||
| 279 | $fileStreamOperation, |
||
| 280 | $fileStreamAfter, |
||
| 281 | [ |
||
| 282 | 'write' => [ |
||
| 283 | ['path' => $path, 'args' => []], |
||
| 284 | ], |
||
| 285 | 'read' => [ |
||
| 286 | ['path' => $path, 'args' => []], |
||
| 287 | ], |
||
| 288 | ], |
||
| 289 | ]; |
||
| 290 | yield 'file stream ignored by path' => [ |
||
| 291 | $path, |
||
| 292 | $fileStreamBefore, |
||
| 293 | [basename(__FILE__, '.php')], |
||
| 294 | [], |
||
| 295 | $fileStreamOperation, |
||
| 296 | $fileStreamAfter, |
||
| 297 | [], |
||
| 298 | ]; |
||
| 299 | yield 'file stream ignored by class' => [ |
||
| 300 | $path, |
||
| 301 | $fileStreamBefore, |
||
| 302 | [], |
||
| 303 | [self::class], |
||
| 304 | $fileStreamOperation, |
||
| 305 | $fileStreamAfter, |
||
| 306 | [], |
||
| 307 | ]; |
||
| 344 |