Passed
Pull Request — master (#352)
by Valentin
04:17
created
src/Files/src/Files.php 2 patches
Spacing   +39 added lines, -39 removed lines patch added patch discarded remove patch
@@ -45,7 +45,7 @@  discard block
 block discarded – undo
45 45
      */
46 46
     public function __destruct()
47 47
     {
48
-        foreach ($this->destructFiles as $filename) {
48
+        foreach ($this->destructFiles as $filename){
49 49
             $this->delete($filename);
50 50
         }
51 51
     }
@@ -60,30 +60,30 @@  discard block
 block discarded – undo
60 60
         int $mode = null,
61 61
         bool $recursivePermissions = true
62 62
     ): bool {
63
-        if (empty($mode)) {
63
+        if (empty($mode)){
64 64
             $mode = self::DEFAULT_FILE_MODE;
65 65
         }
66 66
 
67 67
         //Directories always executable
68 68
         $mode = $mode | 0111;
69
-        if (is_dir($directory)) {
69
+        if (is_dir($directory)){
70 70
             //Exists :(
71 71
             return $this->setPermissions($directory, $mode);
72 72
         }
73 73
 
74
-        if (!$recursivePermissions) {
74
+        if (!$recursivePermissions){
75 75
             return mkdir($directory, $mode, true);
76 76
         }
77 77
 
78 78
         $directoryChain = [basename($directory)];
79 79
 
80 80
         $baseDirectory = $directory;
81
-        while (!is_dir($baseDirectory = dirname($baseDirectory))) {
81
+        while (!is_dir($baseDirectory = dirname($baseDirectory))){
82 82
             $directoryChain[] = basename($baseDirectory);
83 83
         }
84 84
 
85
-        foreach (array_reverse($directoryChain) as $directory) {
86
-            if (!mkdir($baseDirectory = "{$baseDirectory}/{$directory}")) {
85
+        foreach (array_reverse($directoryChain) as $directory){
86
+            if (!mkdir($baseDirectory = "{$baseDirectory}/{$directory}")){
87 87
                 return false;
88 88
             }
89 89
 
@@ -98,7 +98,7 @@  discard block
 block discarded – undo
98 98
      */
99 99
     public function read(string $filename): string
100 100
     {
101
-        if (!$this->exists($filename)) {
101
+        if (!$this->exists($filename)){
102 102
             throw new FileNotFoundException($filename);
103 103
         }
104 104
 
@@ -119,12 +119,12 @@  discard block
 block discarded – undo
119 119
     ): bool {
120 120
         $mode = $mode ?? self::DEFAULT_FILE_MODE;
121 121
 
122
-        try {
123
-            if ($ensureDirectory) {
122
+        try{
123
+            if ($ensureDirectory){
124 124
                 $this->ensureDirectory(dirname($filename), $mode);
125 125
             }
126 126
 
127
-            if ($this->exists($filename)) {
127
+            if ($this->exists($filename)){
128 128
                 //Forcing mode for existed file
129 129
                 $this->setPermissions($filename, $mode);
130 130
             }
@@ -135,11 +135,11 @@  discard block
 block discarded – undo
135 135
                 $append ? FILE_APPEND | LOCK_EX : LOCK_EX
136 136
             );
137 137
 
138
-            if ($result !== false) {
138
+            if ($result !== false){
139 139
                 //Forcing mode after file creation
140 140
                 $this->setPermissions($filename, $mode);
141 141
             }
142
-        } catch (\Exception $e) {
142
+        }catch (\Exception $e){
143 143
             throw new WriteErrorException($e->getMessage(), $e->getCode(), $e);
144 144
         }
145 145
 
@@ -163,7 +163,7 @@  discard block
 block discarded – undo
163 163
      */
164 164
     public function delete(string $filename)
165 165
     {
166
-        if ($this->exists($filename)) {
166
+        if ($this->exists($filename)){
167 167
             $result = unlink($filename);
168 168
 
169 169
             //Wiping out changes in local file cache
@@ -187,7 +187,7 @@  discard block
 block discarded – undo
187 187
      */
188 188
     public function deleteDirectory(string $directory, bool $contentOnly = false): void
189 189
     {
190
-        if (!$this->isDirectory($directory)) {
190
+        if (!$this->isDirectory($directory)){
191 191
             throw new FilesException("Undefined or invalid directory {$directory}");
192 192
         }
193 193
 
@@ -196,15 +196,15 @@  discard block
 block discarded – undo
196 196
             \RecursiveIteratorIterator::CHILD_FIRST
197 197
         );
198 198
 
199
-        foreach ($files as $file) {
200
-            if ($file->isDir()) {
199
+        foreach ($files as $file){
200
+            if ($file->isDir()){
201 201
                 rmdir($file->getRealPath());
202
-            } else {
202
+            }else{
203 203
                 $this->delete($file->getRealPath());
204 204
             }
205 205
         }
206 206
 
207
-        if (!$contentOnly) {
207
+        if (!$contentOnly){
208 208
             rmdir($directory);
209 209
         }
210 210
     }
@@ -214,7 +214,7 @@  discard block
 block discarded – undo
214 214
      */
215 215
     public function move(string $filename, string $destination): bool
216 216
     {
217
-        if (!$this->exists($filename)) {
217
+        if (!$this->exists($filename)){
218 218
             throw new FileNotFoundException($filename);
219 219
         }
220 220
 
@@ -226,7 +226,7 @@  discard block
 block discarded – undo
226 226
      */
227 227
     public function copy(string $filename, string $destination): bool
228 228
     {
229
-        if (!$this->exists($filename)) {
229
+        if (!$this->exists($filename)){
230 230
             throw new FileNotFoundException($filename);
231 231
         }
232 232
 
@@ -238,7 +238,7 @@  discard block
 block discarded – undo
238 238
      */
239 239
     public function touch(string $filename, int $mode = null): bool
240 240
     {
241
-        if (!touch($filename)) {
241
+        if (!touch($filename)){
242 242
             return false;
243 243
         }
244 244
 
@@ -258,7 +258,7 @@  discard block
 block discarded – undo
258 258
      */
259 259
     public function size(string $filename): int
260 260
     {
261
-        if (!$this->exists($filename)) {
261
+        if (!$this->exists($filename)){
262 262
             throw new FileNotFoundException($filename);
263 263
         }
264 264
 
@@ -278,7 +278,7 @@  discard block
 block discarded – undo
278 278
      */
279 279
     public function md5(string $filename): string
280 280
     {
281
-        if (!$this->exists($filename)) {
281
+        if (!$this->exists($filename)){
282 282
             throw new FileNotFoundException($filename);
283 283
         }
284 284
 
@@ -290,7 +290,7 @@  discard block
 block discarded – undo
290 290
      */
291 291
     public function time(string $filename): int
292 292
     {
293
-        if (!$this->exists($filename)) {
293
+        if (!$this->exists($filename)){
294 294
             throw new FileNotFoundException($filename);
295 295
         }
296 296
 
@@ -318,7 +318,7 @@  discard block
 block discarded – undo
318 318
      */
319 319
     public function getPermissions(string $filename): int
320 320
     {
321
-        if (!$this->exists($filename)) {
321
+        if (!$this->exists($filename)){
322 322
             throw new FileNotFoundException($filename);
323 323
         }
324 324
 
@@ -330,7 +330,7 @@  discard block
 block discarded – undo
330 330
      */
331 331
     public function setPermissions(string $filename, int $mode)
332 332
     {
333
-        if (is_dir($filename)) {
333
+        if (is_dir($filename)){
334 334
             //Directories must always be executable (i.e. 664 for dir => 775)
335 335
             $mode |= 0111;
336 336
         }
@@ -344,9 +344,9 @@  discard block
 block discarded – undo
344 344
     public function getFiles(string $location, string $pattern = null): array
345 345
     {
346 346
         $result = [];
347
-        foreach ($this->filesIterator($location, $pattern) as $filename) {
348
-            if ($this->isDirectory($filename->getPathname())) {
349
-                $result = array_merge($result, $this->getFiles($filename . DIRECTORY_SEPARATOR));
347
+        foreach ($this->filesIterator($location, $pattern) as $filename){
348
+            if ($this->isDirectory($filename->getPathname())){
349
+                $result = array_merge($result, $this->getFiles($filename.DIRECTORY_SEPARATOR));
350 350
 
351 351
                 continue;
352 352
             }
@@ -362,13 +362,13 @@  discard block
 block discarded – undo
362 362
      */
363 363
     public function tempFilename(string $extension = '', string $location = null): string
364 364
     {
365
-        if (empty($location)) {
365
+        if (empty($location)){
366 366
             $location = sys_get_temp_dir();
367 367
         }
368 368
 
369 369
         $filename = tempnam($location, 'spiral');
370 370
 
371
-        if (!empty($extension)) {
371
+        if (!empty($extension)){
372 372
             [$old, $filename] = [$filename, "{$filename}.{$extension}"];
373 373
             rename($old, $filename);
374 374
             $this->destructFiles[] = $filename;
@@ -385,7 +385,7 @@  discard block
 block discarded – undo
385 385
         $path = str_replace(['//', '\\'], '/', $path);
386 386
 
387 387
         //Potentially open links and ../ type directories?
388
-        return rtrim($path, '/') . ($asDirectory ? '/' : '');
388
+        return rtrim($path, '/').($asDirectory ? '/' : '');
389 389
     }
390 390
 
391 391
     /**
@@ -402,21 +402,21 @@  discard block
 block discarded – undo
402 402
         $path = explode('/', $path);
403 403
         $relative = $path;
404 404
 
405
-        foreach ($from as $depth => $dir) {
405
+        foreach ($from as $depth => $dir){
406 406
             //Find first non-matching dir
407
-            if ($dir === $path[$depth]) {
407
+            if ($dir === $path[$depth]){
408 408
                 //Ignore this directory
409 409
                 array_shift($relative);
410
-            } else {
410
+            }else{
411 411
                 //Get number of remaining dirs to $from
412 412
                 $remaining = count($from) - $depth;
413
-                if ($remaining > 1) {
413
+                if ($remaining > 1){
414 414
                     //Add traversals up to first matching directory
415 415
                     $padLength = (count($relative) + $remaining - 1) * -1;
416 416
                     $relative = array_pad($relative, $padLength, '..');
417 417
                     break;
418 418
                 }
419
-                $relative[0] = './' . $relative[0];
419
+                $relative[0] = './'.$relative[0];
420 420
             }
421 421
         }
422 422
 
@@ -432,7 +432,7 @@  discard block
 block discarded – undo
432 432
     private function filesIterator(string $location, string $pattern = null): \GlobIterator
433 433
     {
434 434
         $pattern = $pattern ?? '*';
435
-        $regexp = rtrim($location, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . ltrim($pattern, DIRECTORY_SEPARATOR);
435
+        $regexp = rtrim($location, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.ltrim($pattern, DIRECTORY_SEPARATOR);
436 436
 
437 437
         return new \GlobIterator($regexp);
438 438
     }
Please login to merge, or discard this patch.
Braces   +73 added lines, -35 removed lines patch added patch discarded remove patch
@@ -45,7 +45,8 @@  discard block
 block discarded – undo
45 45
      */
46 46
     public function __destruct()
47 47
     {
48
-        foreach ($this->destructFiles as $filename) {
48
+        foreach ($this->destructFiles as $filename)
49
+        {
49 50
             $this->delete($filename);
50 51
         }
51 52
     }
@@ -60,30 +61,36 @@  discard block
 block discarded – undo
60 61
         int $mode = null,
61 62
         bool $recursivePermissions = true
62 63
     ): bool {
63
-        if (empty($mode)) {
64
+        if (empty($mode))
65
+        {
64 66
             $mode = self::DEFAULT_FILE_MODE;
65 67
         }
66 68
 
67 69
         //Directories always executable
68 70
         $mode = $mode | 0111;
69
-        if (is_dir($directory)) {
71
+        if (is_dir($directory))
72
+        {
70 73
             //Exists :(
71 74
             return $this->setPermissions($directory, $mode);
72 75
         }
73 76
 
74
-        if (!$recursivePermissions) {
77
+        if (!$recursivePermissions)
78
+        {
75 79
             return mkdir($directory, $mode, true);
76 80
         }
77 81
 
78 82
         $directoryChain = [basename($directory)];
79 83
 
80 84
         $baseDirectory = $directory;
81
-        while (!is_dir($baseDirectory = dirname($baseDirectory))) {
85
+        while (!is_dir($baseDirectory = dirname($baseDirectory)))
86
+        {
82 87
             $directoryChain[] = basename($baseDirectory);
83 88
         }
84 89
 
85
-        foreach (array_reverse($directoryChain) as $directory) {
86
-            if (!mkdir($baseDirectory = "{$baseDirectory}/{$directory}")) {
90
+        foreach (array_reverse($directoryChain) as $directory)
91
+        {
92
+            if (!mkdir($baseDirectory = "{$baseDirectory}/{$directory}"))
93
+            {
87 94
                 return false;
88 95
             }
89 96
 
@@ -98,7 +105,8 @@  discard block
 block discarded – undo
98 105
      */
99 106
     public function read(string $filename): string
100 107
     {
101
-        if (!$this->exists($filename)) {
108
+        if (!$this->exists($filename))
109
+        {
102 110
             throw new FileNotFoundException($filename);
103 111
         }
104 112
 
@@ -119,12 +127,15 @@  discard block
 block discarded – undo
119 127
     ): bool {
120 128
         $mode = $mode ?? self::DEFAULT_FILE_MODE;
121 129
 
122
-        try {
123
-            if ($ensureDirectory) {
130
+        try
131
+        {
132
+            if ($ensureDirectory)
133
+            {
124 134
                 $this->ensureDirectory(dirname($filename), $mode);
125 135
             }
126 136
 
127
-            if ($this->exists($filename)) {
137
+            if ($this->exists($filename))
138
+            {
128 139
                 //Forcing mode for existed file
129 140
                 $this->setPermissions($filename, $mode);
130 141
             }
@@ -135,11 +146,14 @@  discard block
 block discarded – undo
135 146
                 $append ? FILE_APPEND | LOCK_EX : LOCK_EX
136 147
             );
137 148
 
138
-            if ($result !== false) {
149
+            if ($result !== false)
150
+            {
139 151
                 //Forcing mode after file creation
140 152
                 $this->setPermissions($filename, $mode);
141 153
             }
142
-        } catch (\Exception $e) {
154
+        }
155
+        catch (\Exception $e)
156
+        {
143 157
             throw new WriteErrorException($e->getMessage(), $e->getCode(), $e);
144 158
         }
145 159
 
@@ -163,7 +177,8 @@  discard block
 block discarded – undo
163 177
      */
164 178
     public function delete(string $filename)
165 179
     {
166
-        if ($this->exists($filename)) {
180
+        if ($this->exists($filename))
181
+        {
167 182
             $result = unlink($filename);
168 183
 
169 184
             //Wiping out changes in local file cache
@@ -187,7 +202,8 @@  discard block
 block discarded – undo
187 202
      */
188 203
     public function deleteDirectory(string $directory, bool $contentOnly = false): void
189 204
     {
190
-        if (!$this->isDirectory($directory)) {
205
+        if (!$this->isDirectory($directory))
206
+        {
191 207
             throw new FilesException("Undefined or invalid directory {$directory}");
192 208
         }
193 209
 
@@ -196,15 +212,20 @@  discard block
 block discarded – undo
196 212
             \RecursiveIteratorIterator::CHILD_FIRST
197 213
         );
198 214
 
199
-        foreach ($files as $file) {
200
-            if ($file->isDir()) {
215
+        foreach ($files as $file)
216
+        {
217
+            if ($file->isDir())
218
+            {
201 219
                 rmdir($file->getRealPath());
202
-            } else {
220
+            }
221
+            else
222
+            {
203 223
                 $this->delete($file->getRealPath());
204 224
             }
205 225
         }
206 226
 
207
-        if (!$contentOnly) {
227
+        if (!$contentOnly)
228
+        {
208 229
             rmdir($directory);
209 230
         }
210 231
     }
@@ -214,7 +235,8 @@  discard block
 block discarded – undo
214 235
      */
215 236
     public function move(string $filename, string $destination): bool
216 237
     {
217
-        if (!$this->exists($filename)) {
238
+        if (!$this->exists($filename))
239
+        {
218 240
             throw new FileNotFoundException($filename);
219 241
         }
220 242
 
@@ -226,7 +248,8 @@  discard block
 block discarded – undo
226 248
      */
227 249
     public function copy(string $filename, string $destination): bool
228 250
     {
229
-        if (!$this->exists($filename)) {
251
+        if (!$this->exists($filename))
252
+        {
230 253
             throw new FileNotFoundException($filename);
231 254
         }
232 255
 
@@ -238,7 +261,8 @@  discard block
 block discarded – undo
238 261
      */
239 262
     public function touch(string $filename, int $mode = null): bool
240 263
     {
241
-        if (!touch($filename)) {
264
+        if (!touch($filename))
265
+        {
242 266
             return false;
243 267
         }
244 268
 
@@ -258,7 +282,8 @@  discard block
 block discarded – undo
258 282
      */
259 283
     public function size(string $filename): int
260 284
     {
261
-        if (!$this->exists($filename)) {
285
+        if (!$this->exists($filename))
286
+        {
262 287
             throw new FileNotFoundException($filename);
263 288
         }
264 289
 
@@ -278,7 +303,8 @@  discard block
 block discarded – undo
278 303
      */
279 304
     public function md5(string $filename): string
280 305
     {
281
-        if (!$this->exists($filename)) {
306
+        if (!$this->exists($filename))
307
+        {
282 308
             throw new FileNotFoundException($filename);
283 309
         }
284 310
 
@@ -290,7 +316,8 @@  discard block
 block discarded – undo
290 316
      */
291 317
     public function time(string $filename): int
292 318
     {
293
-        if (!$this->exists($filename)) {
319
+        if (!$this->exists($filename))
320
+        {
294 321
             throw new FileNotFoundException($filename);
295 322
         }
296 323
 
@@ -318,7 +345,8 @@  discard block
 block discarded – undo
318 345
      */
319 346
     public function getPermissions(string $filename): int
320 347
     {
321
-        if (!$this->exists($filename)) {
348
+        if (!$this->exists($filename))
349
+        {
322 350
             throw new FileNotFoundException($filename);
323 351
         }
324 352
 
@@ -330,7 +358,8 @@  discard block
 block discarded – undo
330 358
      */
331 359
     public function setPermissions(string $filename, int $mode)
332 360
     {
333
-        if (is_dir($filename)) {
361
+        if (is_dir($filename))
362
+        {
334 363
             //Directories must always be executable (i.e. 664 for dir => 775)
335 364
             $mode |= 0111;
336 365
         }
@@ -344,8 +373,10 @@  discard block
 block discarded – undo
344 373
     public function getFiles(string $location, string $pattern = null): array
345 374
     {
346 375
         $result = [];
347
-        foreach ($this->filesIterator($location, $pattern) as $filename) {
348
-            if ($this->isDirectory($filename->getPathname())) {
376
+        foreach ($this->filesIterator($location, $pattern) as $filename)
377
+        {
378
+            if ($this->isDirectory($filename->getPathname()))
379
+            {
349 380
                 $result = array_merge($result, $this->getFiles($filename . DIRECTORY_SEPARATOR));
350 381
 
351 382
                 continue;
@@ -362,13 +393,15 @@  discard block
 block discarded – undo
362 393
      */
363 394
     public function tempFilename(string $extension = '', string $location = null): string
364 395
     {
365
-        if (empty($location)) {
396
+        if (empty($location))
397
+        {
366 398
             $location = sys_get_temp_dir();
367 399
         }
368 400
 
369 401
         $filename = tempnam($location, 'spiral');
370 402
 
371
-        if (!empty($extension)) {
403
+        if (!empty($extension))
404
+        {
372 405
             [$old, $filename] = [$filename, "{$filename}.{$extension}"];
373 406
             rename($old, $filename);
374 407
             $this->destructFiles[] = $filename;
@@ -402,15 +435,20 @@  discard block
 block discarded – undo
402 435
         $path = explode('/', $path);
403 436
         $relative = $path;
404 437
 
405
-        foreach ($from as $depth => $dir) {
438
+        foreach ($from as $depth => $dir)
439
+        {
406 440
             //Find first non-matching dir
407
-            if ($dir === $path[$depth]) {
441
+            if ($dir === $path[$depth])
442
+            {
408 443
                 //Ignore this directory
409 444
                 array_shift($relative);
410
-            } else {
445
+            }
446
+            else
447
+            {
411 448
                 //Get number of remaining dirs to $from
412 449
                 $remaining = count($from) - $depth;
413
-                if ($remaining > 1) {
450
+                if ($remaining > 1)
451
+                {
414 452
                     //Add traversals up to first matching directory
415 453
                     $padLength = (count($relative) + $remaining - 1) * -1;
416 454
                     $relative = array_pad($relative, $padLength, '..');
Please login to merge, or discard this patch.