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