Passed
Push — master ( 304c7c...8799b6 )
by Alexander
01:40
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 18
    public static function createDirectory(string $path, int $mode = 0775): bool
52
    {
53 18
        $path = static::normalizePath($path);
54
55
        try {
56 18
            if (!mkdir($path, $mode, true) && !is_dir($path)) {
57 18
                return false;
58
            }
59 1
        } catch (\Exception $e) {
60 1
            if (!is_dir($path)) {
61
                throw new \RuntimeException(
62
                    "Failed to create directory \"$path\": " . $e->getMessage(),
63
                    $e->getCode(),
64
                    $e
65
                );
66
            }
67
        }
68
69 18
        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 18
    private static function chmod(string $path, int $mode): ?bool
83
    {
84
        try {
85 18
            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 18
    public static function normalizePath(string $path): string
110
    {
111 18
        $isWindowsShare = strpos($path, '\\\\') === 0;
112
113 18
        if ($isWindowsShare) {
114 1
            $path = substr($path, 2);
115
        }
116
117 18
        $path = rtrim(strtr($path, '/\\', '//'), '/');
118
119 18
        if (strpos('/' . $path, '/.') === false && strpos($path, '//') === false) {
120 18
            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 18
    public static function removeDirectory(string $directory, array $options = []): void
151
    {
152
        try {
153 18
            static::clearDirectory($directory, $options);
154 1
        } catch (\InvalidArgumentException $e) {
155 1
            return;
156
        }
157
158 18
        if (is_link($directory)) {
159 2
            self::unlink($directory);
160
        } else {
161 18
            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 18
    public static function clearDirectory(string $directory, array $options = []): void
180
    {
181 18
        $handle = static::openDirectory($directory);
182 18
        if (!empty($options['traverseSymlinks']) || !is_link($directory)) {
183 18
            while (($file = readdir($handle)) !== false) {
184 18
                if ($file === '.' || $file === '..') {
185 18
                    continue;
186
                }
187 17
                $path = $directory . '/' . $file;
188 17
                if (is_dir($path)) {
189 17
                    self::removeDirectory($path, $options);
190
                } else {
191 11
                    self::unlink($path);
192
                }
193
            }
194 18
            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 11
    public static function unlink(string $path): bool
206
    {
207 11
        $isWindows = DIRECTORY_SEPARATOR === '\\';
208
209 11
        if (!$isWindows) {
210 11
            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 11
    public static function copyDirectory(string $source, string $destination, array $options = []): void
271
    {
272 11
        $source = static::normalizePath($source);
273 11
        $destination = static::normalizePath($destination);
274
275 11
        static::isSelfDirectory($source, $destination);
276
277 9
        $destinationExists = static::setDestination($destination, $options);
278
279 9
        $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
     * @return boolean
319
     */
320 11
    private static function isSelfDirectory(string $source, string $destination)
321
    {
322 11
        if ($source === $destination || strpos($destination, $source . '/') === 0) {
323 2
            throw new \InvalidArgumentException('Trying to copy a directory to itself or a subdirectory.');
324
        }
325
    }
326
327
    /**
328
     * Open directory handle.
329
     *
330
     * @param string $directory
331
     *
332
     * @return resource
333
     * @throws \InvalidArgumentException
334
     */
335 18
    private static function openDirectory(string $directory)
336
    {
337 18
        $handle = @opendir($directory);
338
339 18
        if ($handle === false) {
340 2
            throw new \InvalidArgumentException("Unable to open directory: $directory");
341
        }
342
343 18
        return $handle;
344
    }
345
346
    /**
347
     * Set base path directory.
348
     *
349
     * @param string $source
350
     * @param array $options
351
     *
352
     * @return array
353
     */
354 9
    private static function setBasePath(string $source, array $options): array
355
    {
356 9
        if (!isset($options['basePath'])) {
357
            // this should be done only once
358 9
            $options['basePath'] = realpath($source);
359 9
            $options = static::normalizeOptions($options);
360
        }
361
362 9
        return $options;
363
    }
364
365
    /**
366
     * Set destination directory.
367
     *
368
     * @param string $destination
369
     * @param array $options
370
     *
371
     * @return bool
372
     */
373 9
    private static function setDestination(string $destination, array $options): bool
374
    {
375 9
        $destinationExists = is_dir($destination);
376
377 9
        if (!$destinationExists && (!isset($options['copyEmptyDirectories']) || $options['copyEmptyDirectories'])) {
378 5
            static::createDirectory($destination, $options['dirMode'] ?? 0775);
379 5
            $destinationExists = true;
380
        }
381
382 9
        return $destinationExists;
383
    }
384
385
    /**
386
     * Normalize options.
387
     *
388
     * @param array $options raw options.
389
     *
390
     * @return array normalized options.
391
     */
392 9
    protected static function normalizeOptions(array $options): array
393
    {
394 9
        $options = static::setCaseSensitive($options);
395 9
        $options = static::setExcept($options);
396 9
        $options = static::setOnly($options);
397
398 9
        return $options;
399
    }
400
401
    /**
402
     * Set options case sensitive.
403
     *
404
     * @param array $options
405
     *
406
     * @return array
407
     */
408 9
    private static function setCaseSensitive(array $options): array
409
    {
410 9
        if (!array_key_exists('caseSensitive', $options)) {
411 9
            $options['caseSensitive'] = true;
412
        }
413
414 9
        return $options;
415
    }
416
417
    /**
418
     * Set options except.
419
     *
420
     * @param array $options
421
     *
422
     * @return array
423
     */
424 9
    private static function setExcept(array $options): array
425
    {
426 9
        if (isset($options['except'])) {
427 1
            foreach ($options['except'] as $key => $value) {
428 1
                if (\is_string($value)) {
429 1
                    $options['except'][$key] = self::parseExcludePattern($value, $options['caseSensitive']);
430
                }
431
            }
432
        }
433
434 9
        return $options;
435
    }
436
437
    /**
438
     * Set options only.
439
     *
440
     * @param array $options
441
     *
442
     * @return array
443
     */
444 9
    private static function setOnly(array $options): array
445
    {
446 9
        if (isset($options['only'])) {
447 2
            foreach ($options['only'] as $key => $value) {
448 2
                if (\is_string($value)) {
449 2
                    $options['only'][$key] = self::parseExcludePattern($value, $options['caseSensitive']);
450
                }
451
            }
452
        }
453
454 9
        return $options;
455
    }
456
457
    /**
458
     * Checks if the given file path satisfies the filtering options.
459
     *
460
     * @param string $path the path of the file or directory to be checked.
461
     * @param array $options the filtering options.
462
     *
463
     * @return bool whether the file or directory satisfies the filtering options.
464
     */
465 7
    public static function filterPath(string $path, array $options): bool
466
    {
467 7
        $path = str_replace('\\', '/', $path);
468
469 7
        if (!empty($options['except'])) {
470 1
            if ((self::lastExcludeMatchingFromList($options['basePath'], $path, $options['except'])) !== null) {
471 1
                return false;
472
            }
473
        }
474
475 7
        if (!empty($options['only']) && !is_dir($path)) {
476
            // don't check PATTERN_NEGATIVE since those entries are not prefixed with !
477 2
            return self::lastExcludeMatchingFromList($options['basePath'], $path, $options['only']) !== null;
478
        }
479
480 7
        return true;
481
    }
482
483
    /**
484
     * Searches for the first wildcard character in the pattern.
485
     *
486
     * @param string $pattern the pattern to search in.
487
     *
488
     * @return int|bool position of first wildcard character or false if not found.
489
     */
490 2
    private static function firstWildcardInPattern(string $pattern)
491
    {
492 2
        $wildcards = ['*', '?', '[', '\\'];
493 2
        $wildcardSearch = function ($carry, $item) use ($pattern) {
494 2
            $position = strpos($pattern, $item);
495 2
            if ($position === false) {
496 2
                return $carry === false ? $position : $carry;
497
            }
498 2
            return $carry === false ? $position : min($carry, $position);
499 2
        };
500 2
        return array_reduce($wildcards, $wildcardSearch, false);
501
    }
502
503
504
    /**
505
     * Scan the given exclude list in reverse to see whether pathname should be ignored.
506
     *
507
     * The first match (i.e. the last on the list), if any, determines the fate.  Returns the element which matched,
508
     * or null for undecided.
509
     *
510
     * Based on last_exclude_matching_from_list() from dir.c of git 1.8.5.3 sources.
511
     *
512
     * @param string $basePath.
513
     * @param string $path.
514
     * @param array $excludes list of patterns to match $path against.
515
     *
516
     * @return null|array null or one of $excludes item as an array with keys: 'pattern', 'flags'.
517
     *
518
     * @throws \InvalidArgumentException if any of the exclude patterns is not a string or an array with keys: pattern,
519
     *                                   flags, firstWildcard.
520
     */
521 2
    private static function lastExcludeMatchingFromList(string $basePath, string $path, array $excludes): ?array
522
    {
523 2
        foreach (array_reverse($excludes) as $exclude) {
524 2
            if (\is_string($exclude)) {
525
                $exclude = self::parseExcludePattern($exclude, false);
526
            }
527
528 2
            if (!isset($exclude['pattern'], $exclude['flags'], $exclude['firstWildcard'])) {
529
                throw new \InvalidArgumentException(
530
                    'If exclude/include pattern is an array it must contain the pattern, flags and firstWildcard keys.'
531
                );
532
            }
533
534 2
            if (($exclude['flags'] & self::PATTERN_MUST_BE_DIR) && !is_dir($path)) {
535
                continue;
536
            }
537
538 2
            if ($exclude['flags'] & self::PATTERN_NO_DIR) {
539
                if (self::matchBasename(basename($path), $exclude['pattern'], $exclude['firstWildcard'], $exclude['flags'])) {
540
                    return $exclude;
541
                }
542
                continue;
543
            }
544
545 2
            if (self::matchPathname($path, $basePath, $exclude['pattern'], $exclude['firstWildcard'], $exclude['flags'])) {
546 2
                return $exclude;
547
            }
548
        }
549
550 2
        return null;
551
    }
552
553
    /**
554
     * Performs a simple comparison of file or directory names.
555
     *
556
     * Based on match_basename() from dir.c of git 1.8.5.3 sources.
557
     *
558
     * @param string $baseName file or directory name to compare with the pattern.
559
     * @param string $pattern the pattern that $baseName will be compared against.
560
     * @param int|bool $firstWildcard location of first wildcard character in the $pattern.
561
     * @param int $flags pattern flags
562
     *
563
     * @return bool whether the name matches against pattern
564
     */
565
    private static function matchBasename(string $baseName, string $pattern, $firstWildcard, int $flags): bool
566
    {
567
        if ($firstWildcard === false) {
568
            if ($pattern === $baseName) {
569
                return true;
570
            }
571
        } elseif ($flags & self::PATTERN_ENDS_WITH) {
572
            /* "*literal" matching against "fooliteral" */
573
            $n = StringHelper::byteLength($pattern);
574
            if (StringHelper::byteSubstring($pattern, 1, $n) === StringHelper::byteSubstring($baseName, -$n, $n)) {
575
                return true;
576
            }
577
        }
578
579
580
        $wildcardPattern = new WildcardPattern($pattern);
581
582
        if ($flags & self::PATTERN_CASE_INSENSITIVE) {
583
            $wildcardPattern = $wildcardPattern->ignoreCase();
584
        }
585
586
        return $wildcardPattern->match($baseName);
587
    }
588
589
    /**
590
     * Compares a path part against a pattern with optional wildcards.
591
     *
592
     * Based on match_pathname() from dir.c of git 1.8.5.3 sources.
593
     *
594
     * @param string $path full path to compare
595
     * @param string $basePath base of path that will not be compared
596
     * @param string $pattern the pattern that path part will be compared against
597
     * @param int|bool $firstWildcard location of first wildcard character in the $pattern
598
     * @param int $flags pattern flags
599
     *
600
     * @return bool whether the path part matches against pattern
601
     */
602 2
    private static function matchPathname(string $path, string $basePath, string $pattern, $firstWildcard, int $flags): bool
603
    {
604
        // match with FNM_PATHNAME; the pattern has base implicitly in front of it.
605 2
        if (strpos($pattern, '/') === 0) {
606
            $pattern = StringHelper::byteSubstring($pattern, 1, StringHelper::byteLength($pattern));
607
            if ($firstWildcard !== false && $firstWildcard !== 0) {
608
                $firstWildcard--;
609
            }
610
        }
611
612 2
        $namelen = StringHelper::byteLength($path) - (empty($basePath) ? 0 : StringHelper::byteLength($basePath) + 1);
613 2
        $name = StringHelper::byteSubstring($path, -$namelen, $namelen);
614
615 2
        if ($firstWildcard !== 0) {
616 2
            if ($firstWildcard === false) {
617 1
                $firstWildcard = StringHelper::byteLength($pattern);
618
            }
619
620
            // if the non-wildcard part is longer than the remaining pathname, surely it cannot match.
621 2
            if ($firstWildcard > $namelen) {
622 1
                return false;
623
            }
624
625 2
            if (strncmp($pattern, $name, (int) $firstWildcard)) {
626 2
                return false;
627
            }
628
629 2
            $pattern = StringHelper::byteSubstring($pattern, (int) $firstWildcard, StringHelper::byteLength($pattern));
630 2
            $name = StringHelper::byteSubstring($name, (int) $firstWildcard, $namelen);
631
632
            // 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.
633 2
            if (empty($pattern) && empty($name)) {
634 1
                return true;
635
            }
636
        }
637
638 2
        $wildcardPattern = (new WildcardPattern($pattern))
639 2
            ->withExactSlashes();
640
641 2
        if ($flags & self::PATTERN_CASE_INSENSITIVE) {
642
            $wildcardPattern = $wildcardPattern->ignoreCase();
643
        }
644
645 2
        return $wildcardPattern->match($name);
646
    }
647
648
    /**
649
     * Processes the pattern, stripping special characters like / and ! from the beginning and settings flags instead.
650
     *
651
     * @param string $pattern
652
     * @param bool $caseSensitive
653
     *
654
     * @return array with keys: (string) pattern, (int) flags, (int|bool) firstWildcard
655
     */
656 2
    private static function parseExcludePattern(string $pattern, bool $caseSensitive): array
657
    {
658
        $result = [
659 2
            'pattern' => $pattern,
660 2
            'flags' => 0,
661
            'firstWildcard' => false,
662
        ];
663
664 2
        $result = static::isCaseInsensitive($caseSensitive, $result);
665
666 2
        if (!isset($pattern[0])) {
667
            return $result;
668
        }
669
670 2
        if (strpos($pattern, '!') === 0) {
671
            $result['flags'] |= self::PATTERN_NEGATIVE;
672
            $pattern = StringHelper::byteSubstring($pattern, 1, StringHelper::byteLength($pattern));
673
        }
674
675 2
        if (StringHelper::byteLength($pattern) && StringHelper::byteSubstring($pattern, -1, 1) === '/') {
676
            $pattern = StringHelper::byteSubstring($pattern, 0, -1);
677
            $result['flags'] |= self::PATTERN_MUST_BE_DIR;
678
        }
679
680 2
        $result = static::isPatternNoDir($pattern, $result);
681
682 2
        $result['firstWildcard'] = self::firstWildcardInPattern($pattern);
683
684 2
        $result = static::isPatternEndsWith($pattern, $result);
685
686 2
        $result['pattern'] = $pattern;
687
688 2
        return $result;
689
    }
690
691
    /**
692
     * Check isCaseInsensitive.
693
     *
694
     * @param boolean $caseSensitive
695
     * @param array $result
696
     *
697
     * @return array
698
     */
699 2
    private static function isCaseInsensitive(bool $caseSensitive, array $result): array
700
    {
701 2
        if (!$caseSensitive) {
702
            $result['flags'] |= self::PATTERN_CASE_INSENSITIVE;
703
        }
704
705 2
        return $result;
706
    }
707
708
    /**
709
     * Check pattern no directory.
710
     *
711
     * @param string $pattern
712
     * @param array $result
713
     *
714
     * @return array
715
     */
716 2
    private static function isPatternNoDir(string $pattern, array $result): array
717
    {
718 2
        if (strpos($pattern, '/') === false) {
719
            $result['flags'] |= self::PATTERN_NO_DIR;
720
        }
721
722 2
        return $result;
723
    }
724
725
    /**
726
     * Check pattern ends with
727
     *
728
     * @param string $pattern
729
     * @param array $result
730
     *
731
     * @return array
732
     */
733 2
    private static function isPatternEndsWith(string $pattern, array $result): array
734
    {
735 2
        if (strpos($pattern, '*') === 0 && self::firstWildcardInPattern(StringHelper::byteSubstring($pattern, 1, StringHelper::byteLength($pattern))) === false) {
736
            $result['flags'] |= self::PATTERN_ENDS_WITH;
737
        }
738
739 2
        return $result;
740
    }
741
}
742