Passed
Push — master ( ef54a1...9c3277 )
by Alexander
01:23
created

FileHelper::matchPathname()   C

Complexity

Conditions 12
Paths 72

Size

Total Lines 44
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 12.7571

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
eloc 22
c 1
b 0
f 0
nc 72
nop 5
dl 0
loc 44
ccs 19
cts 23
cp 0.8261
crap 12.7571
rs 6.9666

How to fix   Complexity   

Long Method

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:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Files;
6
7
use Yiisoft\Strings\StringHelper;
8
use Yiisoft\Strings\WildcardPattern;
9
10
/**
11
 * FileHelper provides useful methods to manage files and directories
12
 */
13
class FileHelper
14
{
15
    /**
16
     * @var int PATTERN_NO_DIR
17
     */
18
    private const PATTERN_NO_DIR = 1;
19
20
    /**
21
     * @var int PATTERN_ENDS_WITH
22
     */
23
    private const PATTERN_ENDS_WITH = 4;
24
25
    /**
26
     * @var int PATTERN_MUST_BE_DIR
27
     */
28
    private const PATTERN_MUST_BE_DIR = 8;
29
30
    /**
31
     * @var int PATTERN_NEGATIVE
32
     */
33
    private const PATTERN_NEGATIVE = 16;
34
35
    /**
36
     * @var int PATTERN_CASE_INSENSITIVE
37
     */
38
    private const PATTERN_CASE_INSENSITIVE = 32;
39
40
    /**
41
     * Creates a new directory.
42
     *
43
     * This method is similar to the PHP `mkdir()` function except that it uses `chmod()` to set the permission of the
44
     * created directory in order to avoid the impact of the `umask` setting.
45
     *
46
     * @param string $path path of the directory to be created.
47
     * @param int $mode the permission to be set for the created directory.
48
     *
49
     * @return bool whether the directory is created successfully.
50
     */
51 20
    public static function createDirectory(string $path, int $mode = 0775): bool
52
    {
53 20
        $path = static::normalizePath($path);
54
55
        try {
56 20
            if (!mkdir($path, $mode, true) && !is_dir($path)) {
57 20
                return false;
58
            }
59 2
        } catch (\Exception $e) {
60 2
            if (!is_dir($path)) {
61 1
                throw new \RuntimeException(
62 1
                    "Failed to create directory \"$path\": " . $e->getMessage(),
63 1
                    $e->getCode(),
64
                    $e
65
                );
66
            }
67
        }
68
69 20
        return static::chmod($path, $mode);
70
    }
71
72
    /**
73
     * Set permissions directory.
74
     *
75
     * @param string $path
76
     * @param integer $mode
77
     *
78
     * @throws \RuntimeException
79
     *
80
     * @return boolean|null
81
     */
82 20
    private static function chmod(string $path, int $mode): ?bool
83
    {
84
        try {
85 20
            return chmod($path, $mode);
86
        } catch (\Exception $e) {
87
            throw new \RuntimeException(
88
                "Failed to change permissions for directory \"$path\": " . $e->getMessage(),
89
                $e->getCode(),
90
                $e
91
            );
92
        }
93
    }
94
95
    /**
96
     * Normalizes a file/directory path.
97
     *
98
     * The normalization does the following work:
99
     *
100
     * - Convert all directory separators into `/` (e.g. "\a/b\c" becomes "/a/b/c")
101
     * - Remove trailing directory separators (e.g. "/a/b/c/" becomes "/a/b/c")
102
     * - Turn multiple consecutive slashes into a single one (e.g. "/a///b/c" becomes "/a/b/c")
103
     * - Remove ".." and "." based on their meanings (e.g. "/a/./b/../c" becomes "/a/c")
104
     *
105
     * @param string $path the file/directory path to be normalized
106
     *
107
     * @return string the normalized file/directory path
108
     */
109 20
    public static function normalizePath(string $path): string
110
    {
111 20
        $isWindowsShare = strpos($path, '\\\\') === 0;
112
113 20
        if ($isWindowsShare) {
114 1
            $path = substr($path, 2);
115
        }
116
117 20
        $path = rtrim(strtr($path, '/\\', '//'), '/');
118
119 20
        if (strpos('/' . $path, '/.') === false && strpos($path, '//') === false) {
120 20
            return $isWindowsShare ? "\\\\$path" : $path;
121
        }
122
123 1
        $parts = [];
124
125 1
        foreach (explode('/', $path) as $part) {
126 1
            if ($part === '..' && !empty($parts) && end($parts) !== '..') {
127 1
                array_pop($parts);
128 1
            } elseif ($part !== '.' && ($part !== '' || empty($parts))) {
129 1
                $parts[] = $part;
130
            }
131
        }
132
133 1
        $path = implode('/', $parts);
134
135 1
        if ($isWindowsShare) {
136 1
            $path = '\\\\' . $path;
137
        }
138
139 1
        return $path === '' ? '.' : $path;
140
    }
141
142
    /**
143
     * Removes a directory (and all its content) recursively.
144
     *
145
     * @param string $directory the directory to be deleted recursively.
146
     * @param array $options options for directory remove ({@see clearDirectory()}).
147
     *
148
     * @return void
149
     */
150 20
    public static function removeDirectory(string $directory, array $options = []): void
151
    {
152
        try {
153 20
            static::clearDirectory($directory, $options);
154 1
        } catch (\InvalidArgumentException $e) {
155 1
            return;
156
        }
157
158 20
        if (is_link($directory)) {
159 2
            self::unlink($directory);
160
        } else {
161 20
            rmdir($directory);
162
        }
163
    }
164
165
    /**
166
     * Clear all directory content.
167
     *
168
     * @param string $directory the directory to be cleared.
169
     * @param array $options options for directory clear . Valid options are:
170
     *
171
     * - traverseSymlinks: boolean, whether symlinks to the directories should be traversed too.
172
     *   Defaults to `false`, meaning the content of the symlinked directory would not be deleted.
173
     *   Only symlink would be removed in that default case.
174
     *
175
     * @throws \InvalidArgumentException if unable to open directory
176
     *
177
     * @return void
178
     */
179 20
    public static function clearDirectory(string $directory, array $options = []): void
180
    {
181 20
        $handle = static::openDirectory($directory);
182 20
        if (!empty($options['traverseSymlinks']) || !is_link($directory)) {
183 20
            while (($file = readdir($handle)) !== false) {
184 20
                if ($file === '.' || $file === '..') {
185 20
                    continue;
186
                }
187 19
                $path = $directory . '/' . $file;
188 19
                if (is_dir($path)) {
189 19
                    self::removeDirectory($path, $options);
190
                } else {
191 12
                    self::unlink($path);
192
                }
193
            }
194 20
            closedir($handle);
195
        }
196
    }
197
198
    /**
199
     * Removes a file or symlink in a cross-platform way.
200
     *
201
     * @param string $path
202
     *
203
     * @return bool
204
     */
205 12
    public static function unlink(string $path): bool
206
    {
207 12
        $isWindows = DIRECTORY_SEPARATOR === '\\';
208
209 12
        if (!$isWindows) {
210 12
            return unlink($path);
211
        }
212
213
        if (is_link($path) && is_dir($path)) {
214
            return rmdir($path);
215
        }
216
217
        return unlink($path);
218
    }
219
220
    /**
221
     * Copies a whole directory as another one.
222
     *
223
     * The files and sub-directories will also be copied over.
224
     *
225
     * @param string $source the source directory.
226
     * @param string $destination the destination directory.
227
     * @param array $options options for directory copy. Valid options are:
228
     *
229
     * - dirMode: integer, the permission to be set for newly copied directories. Defaults to 0775.
230
     * - fileMode:  integer, the permission to be set for newly copied files. Defaults to the current environment
231
     *   setting.
232
     * - filter: callback, a PHP callback that is called for each directory or file.
233
     *   The signature of the callback should be: `function ($path)`, where `$path` refers the full path to be filtered.
234
     *   The callback can return one of the following values:
235
     *
236
     *   * true: the directory or file will be copied (the "only" and "except" options will be ignored).
237
     *   * false: the directory or file will NOT be copied (the "only" and "except" options will be ignored).
238
     *   * null: the "only" and "except" options will determine whether the directory or file should be copied.
239
     *
240
     * - only: array, list of patterns that the file paths should match if they want to be copied. A path matches a
241
     *   pattern if it contains the pattern string at its end. For example, '.php' matches all file paths ending with
242
     *   '.php'.
243
     *   Note, the '/' characters in a pattern matches both '/' and '\' in the paths. If a file path matches a pattern
244
     *   in both "only" and "except", it will NOT be copied.
245
     * - except: array, list of patterns that the files or directories should match if they want to be excluded from
246
     *   being copied. A path matches a pattern if it contains the pattern string at its end. Patterns ending with '/'
247
     *   apply to directory paths only, and patterns not ending with '/' apply to file paths only. For example, '/a/b'
248
     *   matches all file paths ending with '/a/b'; and '.svn/' matches directory paths ending with '.svn'. Note, the
249
     *   '/' characters in a pattern matches both '/' and '\' in the paths.
250
     * - caseSensitive: boolean, whether patterns specified at "only" or "except" should be case sensitive. Defaults to
251
     *   true.
252
     * - recursive: boolean, whether the files under the subdirectories should also be copied. Defaults to true.
253
     * - beforeCopy: callback, a PHP callback that is called before copying each sub-directory or file. If the callback
254
     *   returns false, the copy operation for the sub-directory or file will be cancelled. The signature of the
255
     *   callback should be: `function ($from, $to)`, where `$from` is the sub-directory or file to be copied from,
256
     *   while `$to` is the copy target.
257
     * - afterCopy: callback, a PHP callback that is called after each sub-directory or file is successfully copied.
258
     *   The signature of the callback should be: `function ($from, $to)`, where `$from` is the sub-directory or file
259
     *   copied from, while `$to` is the copy target.
260
     * - copyEmptyDirectories: boolean, whether to copy empty directories. Set this to false to avoid creating
261
     *   directories that do not contain files. This affects directories that do not contain files initially as well as
262
     *   directories that do not contain files at the target destination because files have been filtered via `only` or
263
     *   `except`. Defaults to true.
264
     *
265
     * @throws \InvalidArgumentException if unable to open directory
266
     * @throws \Exception
267
     *
268
     * @return void
269
     */
270 12
    public static function copyDirectory(string $source, string $destination, array $options = []): void
271
    {
272 12
        $source = static::normalizePath($source);
273 12
        $destination = static::normalizePath($destination);
274
275 12
        static::assertNotSelfDirectory($source, $destination);
276
277 10
        $destinationExists = static::setDestination($destination, $options);
278
279 10
        $handle = static::openDirectory($source);
280
281 9
        $options = static::setBasePath($source, $options);
282
283 9
        while (($file = readdir($handle)) !== false) {
284 9
            if ($file === '.' || $file === '..') {
285 9
                continue;
286
            }
287
288 7
            $from = $source . '/' . $file;
289 7
            $to = $destination . '/' . $file;
290
291 7
            if (static::filterPath($from, $options)) {
292 7
                if (is_file($from)) {
293 7
                    if (!$destinationExists) {
294 2
                        static::createDirectory($destination, $options['dirMode'] ?? 0775);
295 2
                        $destinationExists = true;
296
                    }
297 7
                    copy($from, $to);
298 7
                    if (isset($options['fileMode'])) {
299 7
                        static::chmod($to, $options['fileMode']);
300
                    }
301 6
                } elseif (!isset($options['recursive']) || $options['recursive']) {
302 5
                    static::copyDirectory($from, $to, $options);
303
                }
304
            }
305
        }
306
307 9
        closedir($handle);
308
    }
309
310
    /**
311
     * Check copy it self directory.
312
     *
313
     * @param string $source
314
     * @param string $destination
315
     *
316
     * @throws \InvalidArgumentException
317
     */
318 12
    private static function assertNotSelfDirectory(string $source, string $destination): void
319
    {
320 12
        if ($source === $destination || strpos($destination, $source . '/') === 0) {
321 2
            throw new \InvalidArgumentException('Trying to copy a directory to itself or a subdirectory.');
322
        }
323
    }
324
325
    /**
326
     * Open directory handle.
327
     *
328
     * @param string $directory
329
     *
330
     * @return resource
331
     * @throws \InvalidArgumentException
332
     */
333 20
    private static function openDirectory(string $directory)
334
    {
335 20
        $handle = @opendir($directory);
336
337 20
        if ($handle === false) {
338 3
            throw new \InvalidArgumentException("Unable to open directory: $directory");
339
        }
340
341 20
        return $handle;
342
    }
343
344
    /**
345
     * Set base path directory.
346
     *
347
     * @param string $source
348
     * @param array $options
349
     *
350
     * @return array
351
     */
352 9
    private static function setBasePath(string $source, array $options): array
353
    {
354 9
        if (!isset($options['basePath'])) {
355
            // this should be done only once
356 9
            $options['basePath'] = realpath($source);
357 9
            $options = static::normalizeOptions($options);
358
        }
359
360 9
        return $options;
361
    }
362
363
    /**
364
     * Set destination directory.
365
     *
366
     * @param string $destination
367
     * @param array $options
368
     *
369
     * @return bool
370
     */
371 10
    private static function setDestination(string $destination, array $options): bool
372
    {
373 10
        $destinationExists = is_dir($destination);
374
375 10
        if (!$destinationExists && (!isset($options['copyEmptyDirectories']) || $options['copyEmptyDirectories'])) {
376 6
            static::createDirectory($destination, $options['dirMode'] ?? 0775);
377 6
            $destinationExists = true;
378
        }
379
380 10
        return $destinationExists;
381
    }
382
383
    /**
384
     * Normalize options.
385
     *
386
     * @param array $options raw options.
387
     *
388
     * @return array normalized options.
389
     */
390 9
    protected static function normalizeOptions(array $options): array
391
    {
392 9
        $options = static::setCaseSensitive($options);
393 9
        $options = static::setExcept($options);
394 9
        $options = static::setOnly($options);
395
396 9
        return $options;
397
    }
398
399
    /**
400
     * Set options case sensitive.
401
     *
402
     * @param array $options
403
     *
404
     * @return array
405
     */
406 9
    private static function setCaseSensitive(array $options): array
407
    {
408 9
        if (!array_key_exists('caseSensitive', $options)) {
409 9
            $options['caseSensitive'] = true;
410
        }
411
412 9
        return $options;
413
    }
414
415
    /**
416
     * Set options except.
417
     *
418
     * @param array $options
419
     *
420
     * @return array
421
     */
422 9
    private static function setExcept(array $options): array
423
    {
424 9
        if (isset($options['except'])) {
425 1
            foreach ($options['except'] as $key => $value) {
426 1
                if (\is_string($value)) {
427 1
                    $options['except'][$key] = self::parseExcludePattern($value, $options['caseSensitive']);
428
                }
429
            }
430
        }
431
432 9
        return $options;
433
    }
434
435
    /**
436
     * Set options only.
437
     *
438
     * @param array $options
439
     *
440
     * @return array
441
     */
442 9
    private static function setOnly(array $options): array
443
    {
444 9
        if (isset($options['only'])) {
445 2
            foreach ($options['only'] as $key => $value) {
446 2
                if (\is_string($value)) {
447 2
                    $options['only'][$key] = self::parseExcludePattern($value, $options['caseSensitive']);
448
                }
449
            }
450
        }
451
452 9
        return $options;
453
    }
454
455
    /**
456
     * Checks if the given file path satisfies the filtering options.
457
     *
458
     * @param string $path the path of the file or directory to be checked.
459
     * @param array $options the filtering options.
460
     *
461
     * @return bool whether the file or directory satisfies the filtering options.
462
     */
463 7
    public static function filterPath(string $path, array $options): bool
464
    {
465 7
        $path = str_replace('\\', '/', $path);
466
467 7
        if (!empty($options['except'])) {
468 1
            if ((self::lastExcludeMatchingFromList($options['basePath'], $path, $options['except'])) !== null) {
469 1
                return false;
470
            }
471
        }
472
473 7
        if (!empty($options['only']) && !is_dir($path)) {
474
            // don't check PATTERN_NEGATIVE since those entries are not prefixed with !
475 2
            return self::lastExcludeMatchingFromList($options['basePath'], $path, $options['only']) !== null;
476
        }
477
478 7
        return true;
479
    }
480
481
    /**
482
     * Searches for the first wildcard character in the pattern.
483
     *
484
     * @param string $pattern the pattern to search in.
485
     *
486
     * @return int|bool position of first wildcard character or false if not found.
487
     */
488 2
    private static function firstWildcardInPattern(string $pattern)
489
    {
490 2
        $wildcards = ['*', '?', '[', '\\'];
491 2
        $wildcardSearch = static function ($carry, $item) use ($pattern) {
492 2
            $position = strpos($pattern, $item);
493 2
            if ($position === false) {
494 2
                return $carry === false ? $position : $carry;
495
            }
496 2
            return $carry === false ? $position : min($carry, $position);
497 2
        };
498 2
        return array_reduce($wildcards, $wildcardSearch, false);
499
    }
500
501
502
    /**
503
     * Scan the given exclude list in reverse to see whether pathname should be ignored.
504
     *
505
     * The first match (i.e. the last on the list), if any, determines the fate.  Returns the element which matched,
506
     * or null for undecided.
507
     *
508
     * Based on last_exclude_matching_from_list() from dir.c of git 1.8.5.3 sources.
509
     *
510
     * @param string $basePath.
511
     * @param string $path.
512
     * @param array $excludes list of patterns to match $path against.
513
     *
514
     * @return null|array null or one of $excludes item as an array with keys: 'pattern', 'flags'.
515
     *
516
     * @throws \InvalidArgumentException if any of the exclude patterns is not a string or an array with keys: pattern,
517
     *                                   flags, firstWildcard.
518
     */
519 2
    private static function lastExcludeMatchingFromList(string $basePath, string $path, array $excludes): ?array
520
    {
521 2
        foreach (array_reverse($excludes) as $exclude) {
522 2
            if (\is_string($exclude)) {
523
                $exclude = self::parseExcludePattern($exclude, false);
524
            }
525
526 2
            if (!isset($exclude['pattern'], $exclude['flags'], $exclude['firstWildcard'])) {
527
                throw new \InvalidArgumentException(
528
                    'If exclude/include pattern is an array it must contain the pattern, flags and firstWildcard keys.'
529
                );
530
            }
531
532 2
            if (($exclude['flags'] & self::PATTERN_MUST_BE_DIR) && !is_dir($path)) {
533
                continue;
534
            }
535
536 2
            if ($exclude['flags'] & self::PATTERN_NO_DIR) {
537
                if (self::matchBasename(basename($path), $exclude['pattern'], $exclude['firstWildcard'], $exclude['flags'])) {
538
                    return $exclude;
539
                }
540
                continue;
541
            }
542
543 2
            if (self::matchPathname($path, $basePath, $exclude['pattern'], $exclude['firstWildcard'], $exclude['flags'])) {
544 2
                return $exclude;
545
            }
546
        }
547
548 2
        return null;
549
    }
550
551
    /**
552
     * Performs a simple comparison of file or directory names.
553
     *
554
     * Based on match_basename() from dir.c of git 1.8.5.3 sources.
555
     *
556
     * @param string $baseName file or directory name to compare with the pattern.
557
     * @param string $pattern the pattern that $baseName will be compared against.
558
     * @param int|bool $firstWildcard location of first wildcard character in the $pattern.
559
     * @param int $flags pattern flags
560
     *
561
     * @return bool whether the name matches against pattern
562
     */
563
    private static function matchBasename(string $baseName, string $pattern, $firstWildcard, int $flags): bool
564
    {
565
        if ($firstWildcard === false) {
566
            if ($pattern === $baseName) {
567
                return true;
568
            }
569
        } elseif ($flags & self::PATTERN_ENDS_WITH) {
570
            /* "*literal" matching against "fooliteral" */
571
            $n = StringHelper::byteLength($pattern);
572
            if (StringHelper::byteSubstring($pattern, 1, $n) === StringHelper::byteSubstring($baseName, -$n, $n)) {
573
                return true;
574
            }
575
        }
576
577
578
        $wildcardPattern = new WildcardPattern($pattern);
579
580
        if ($flags & self::PATTERN_CASE_INSENSITIVE) {
581
            $wildcardPattern = $wildcardPattern->ignoreCase();
582
        }
583
584
        return $wildcardPattern->match($baseName);
585
    }
586
587
    /**
588
     * Compares a path part against a pattern with optional wildcards.
589
     *
590
     * Based on match_pathname() from dir.c of git 1.8.5.3 sources.
591
     *
592
     * @param string $path full path to compare
593
     * @param string $basePath base of path that will not be compared
594
     * @param string $pattern the pattern that path part will be compared against
595
     * @param int|bool $firstWildcard location of first wildcard character in the $pattern
596
     * @param int $flags pattern flags
597
     *
598
     * @return bool whether the path part matches against pattern
599
     */
600 2
    private static function matchPathname(string $path, string $basePath, string $pattern, $firstWildcard, int $flags): bool
601
    {
602
        // match with FNM_PATHNAME; the pattern has base implicitly in front of it.
603 2
        if (strpos($pattern, '/') === 0) {
604
            $pattern = StringHelper::byteSubstring($pattern, 1, StringHelper::byteLength($pattern));
605
            if ($firstWildcard !== false && $firstWildcard !== 0) {
606
                $firstWildcard--;
607
            }
608
        }
609
610 2
        $namelen = StringHelper::byteLength($path) - (empty($basePath) ? 0 : StringHelper::byteLength($basePath) + 1);
611 2
        $name = StringHelper::byteSubstring($path, -$namelen, $namelen);
612
613 2
        if ($firstWildcard !== 0) {
614 2
            if ($firstWildcard === false) {
615 1
                $firstWildcard = StringHelper::byteLength($pattern);
616
            }
617
618
            // if the non-wildcard part is longer than the remaining pathname, surely it cannot match.
619 2
            if ($firstWildcard > $namelen) {
620 1
                return false;
621
            }
622
623 2
            if (strncmp($pattern, $name, (int) $firstWildcard)) {
624 2
                return false;
625
            }
626
627 2
            $pattern = StringHelper::byteSubstring($pattern, (int) $firstWildcard, StringHelper::byteLength($pattern));
628 2
            $name = StringHelper::byteSubstring($name, (int) $firstWildcard, $namelen);
629
630
            // If the whole pattern did not have a wildcard, then our prefix match is all we need; we do not need to call fnmatch at all.
631 2
            if (empty($pattern) && empty($name)) {
632 1
                return true;
633
            }
634
        }
635
636 2
        $wildcardPattern = (new WildcardPattern($pattern))
637 2
            ->withExactSlashes();
638
639 2
        if ($flags & self::PATTERN_CASE_INSENSITIVE) {
640
            $wildcardPattern = $wildcardPattern->ignoreCase();
641
        }
642
643 2
        return $wildcardPattern->match($name);
644
    }
645
646
    /**
647
     * Processes the pattern, stripping special characters like / and ! from the beginning and settings flags instead.
648
     *
649
     * @param string $pattern
650
     * @param bool $caseSensitive
651
     *
652
     * @return array with keys: (string) pattern, (int) flags, (int|bool) firstWildcard
653
     */
654 2
    private static function parseExcludePattern(string $pattern, bool $caseSensitive): array
655
    {
656
        $result = [
657 2
            'pattern' => $pattern,
658 2
            'flags' => 0,
659
            'firstWildcard' => false,
660
        ];
661
662 2
        $result = static::isCaseInsensitive($caseSensitive, $result);
663
664 2
        if (!isset($pattern[0])) {
665
            return $result;
666
        }
667
668 2
        if (strpos($pattern, '!') === 0) {
669
            $result['flags'] |= self::PATTERN_NEGATIVE;
670
            $pattern = StringHelper::byteSubstring($pattern, 1, StringHelper::byteLength($pattern));
671
        }
672
673 2
        if (StringHelper::byteLength($pattern) && StringHelper::byteSubstring($pattern, -1, 1) === '/') {
674
            $pattern = StringHelper::byteSubstring($pattern, 0, -1);
675
            $result['flags'] |= self::PATTERN_MUST_BE_DIR;
676
        }
677
678 2
        $result = static::isPatternNoDir($pattern, $result);
679
680 2
        $result['firstWildcard'] = self::firstWildcardInPattern($pattern);
681
682 2
        $result = static::isPatternEndsWith($pattern, $result);
683
684 2
        $result['pattern'] = $pattern;
685
686 2
        return $result;
687
    }
688
689
    /**
690
     * Check isCaseInsensitive.
691
     *
692
     * @param boolean $caseSensitive
693
     * @param array $result
694
     *
695
     * @return array
696
     */
697 2
    private static function isCaseInsensitive(bool $caseSensitive, array $result): array
698
    {
699 2
        if (!$caseSensitive) {
700
            $result['flags'] |= self::PATTERN_CASE_INSENSITIVE;
701
        }
702
703 2
        return $result;
704
    }
705
706
    /**
707
     * Check pattern no directory.
708
     *
709
     * @param string $pattern
710
     * @param array $result
711
     *
712
     * @return array
713
     */
714 2
    private static function isPatternNoDir(string $pattern, array $result): array
715
    {
716 2
        if (strpos($pattern, '/') === false) {
717
            $result['flags'] |= self::PATTERN_NO_DIR;
718
        }
719
720 2
        return $result;
721
    }
722
723
    /**
724
     * Check pattern ends with
725
     *
726
     * @param string $pattern
727
     * @param array $result
728
     *
729
     * @return array
730
     */
731 2
    private static function isPatternEndsWith(string $pattern, array $result): array
732
    {
733 2
        if (strpos($pattern, '*') === 0 && self::firstWildcardInPattern(StringHelper::byteSubstring($pattern, 1, StringHelper::byteLength($pattern))) === false) {
734
            $result['flags'] |= self::PATTERN_ENDS_WITH;
735
        }
736
737 2
        return $result;
738
    }
739
}
740