Passed
Pull Request — master (#5)
by Wilmer
01:28
created

FileHelper::lastExcludeMatchingFromList()   B

Complexity

Conditions 9
Paths 13

Size

Total Lines 30
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 15.7829

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 15
c 1
b 0
f 0
nc 13
nop 3
dl 0
loc 30
ccs 9
cts 16
cp 0.5625
crap 15.7829
rs 8.0555
1
<?php
2
declare(strict_types = 1);
3
4
namespace Yiisoft\Files;
5
6
use Yiisoft\Strings\StringHelper;
7
8
/**
9
 * FileHelper provides useful methods to manage files and directories
10
 */
11
class FileHelper
12
{
13
    /**
14
     * @var int PATTERN_NO_DIR
15
     */
16
    private const PATTERN_NO_DIR = 1;
17
18
    /**
19
     * @var int PATTERN_ENDS_WITH
20
     */
21
    private const PATTERN_ENDS_WITH = 4;
22
23
    /**
24
     * @var int PATTERN_MUST_BE_DIR
25
     */
26
    private const PATTERN_MUST_BE_DIR = 8;
27
28
    /**
29
     * @var int PATTERN_NEGATIVE
30
     */
31
    private const PATTERN_NEGATIVE = 16;
32
33
    /**
34
     * @var int PATTERN_CASE_INSENSITIVE
35
     */
36
    private const PATTERN_CASE_INSENSITIVE = 32;
37
38
    /**
39
     * Creates a new directory.
40
     *
41
     * This method is similar to the PHP `mkdir()` function except that it uses `chmod()` to set the permission of the
42
     * created directory in order to avoid the impact of the `umask` setting.
43
     *
44
     * @param string $path path of the directory to be created.
45
     * @param int $mode the permission to be set for the created directory.
46
     *
47
     * @return bool whether the directory is created successfully.
48
     */
49 17
    public static function createDirectory(string $path, int $mode = 0775): bool
50
    {
51 17
        $path = static::normalizePath($path);
52
53
        try {
54 17
            if (!mkdir($path, $mode, true) && !is_dir($path)) {
55 17
                return false;
56
            }
57 1
        } catch (\Exception $e) {
58 1
            if (!is_dir($path)) {
59
                throw new \RuntimeException(
60
                    "Failed to create directory \"$path\": " . $e->getMessage(),
61
                    $e->getCode(),
62
                    $e
63
                );
64
            }
65
        }
66
67 17
        return static::chmod($path, $mode);
68
    }
69
70
    /**
71
     * Set permissions directory.
72
     *
73
     * @param string $path
74
     * @param integer $mode
75
     *
76
     * @throws \RuntimeException
77
     *
78
     * @return boolean|null
79
     */
80 17
    private static function chmod(string $path, int $mode): ?bool
81
    {
82
        try {
83 17
            return chmod($path, $mode);
84
        } catch (\Exception $e) {
85
            throw new \RuntimeException(
86
                "Failed to change permissions for directory \"$path\": " . $e->getMessage(),
87
                $e->getCode(),
88
                $e
89
            );
90
        }
91
    }
92
93
    /**
94
     * Normalizes a file/directory path.
95
     *
96
     * The normalization does the following work:
97
     *
98
     * - Convert all directory separators into `/` (e.g. "\a/b\c" becomes "/a/b/c")
99
     * - Remove trailing directory separators (e.g. "/a/b/c/" becomes "/a/b/c")
100
     * - Turn multiple consecutive slashes into a single one (e.g. "/a///b/c" becomes "/a/b/c")
101
     * - Remove ".." and "." based on their meanings (e.g. "/a/./b/../c" becomes "/a/c")
102
     *
103
     * @param string $path the file/directory path to be normalized
104
     *
105
     * @return string the normalized file/directory path
106
     */
107 17
    public static function normalizePath(string $path): string
108
    {
109 17
        $isWindowsShare = strpos($path, '\\\\') === 0;
110
111 17
        if ($isWindowsShare) {
112 1
            $path = substr($path, 2);
113
        }
114
115 17
        $path = rtrim(strtr($path, '/\\', '//'), '/');
116
117 17
        if (strpos('/' . $path, '/.') === false && strpos($path, '//') === false) {
118 17
            if ($isWindowsShare) {
119 1
                $path = $path = '\\\\' . $path;
0 ignored issues
show
Unused Code introduced by
The assignment to $path is dead and can be removed.
Loading history...
120
            }
121
122 17
            return $path;
123
        }
124
125 1
        $parts = [];
126
127 1
        foreach (explode('/', $path) as $part) {
128 1
            if ($part === '..' && !empty($parts) && end($parts) !== '..') {
129 1
                array_pop($parts);
130 1
            } elseif ($part !== '.' && ($part !== '' || empty($parts))) {
131 1
                $parts[] = $part;
132
            }
133
        }
134
135 1
        $path = implode('/', $parts);
136
137 1
        if ($isWindowsShare) {
138 1
            $path = '\\\\' . $path;
139
        }
140
141 1
        return $path === '' ? '.' : $path;
142
    }
143
144
    /**
145
     * Removes a directory (and all its content) recursively.
146
     *
147
     * @param string $directory the directory to be deleted recursively.
148
     * @param array $options options for directory remove. Valid options are:
149
     *
150
     * - traverseSymlinks: boolean, whether symlinks to the directories should be traversed too.
151
     *   Defaults to `false`, meaning the content of the symlinked directory would not be deleted.
152
     *   Only symlink would be removed in that default case.
153
     *
154
     * @return void
155
     */
156 17
    public static function removeDirectory(string $directory, array $options = []): void
157
    {
158 17
        if (!empty($options['traverseSymlinks']) || !is_link($directory)) {
159 17
            if (!($handle = @opendir($directory))) {
160 1
                return;
161
            }
162
163 17
            while (($file = readdir($handle)) !== false) {
164 17
                if ($file === '.' || $file === '..') {
165 17
                    continue;
166
                }
167 16
                $path = $directory . '/' . $file;
168 16
                if (is_dir($path)) {
169 16
                    self::removeDirectory($path, $options);
170
                } else {
171 10
                    self::unlink($path);
172
                }
173
            }
174
175 17
            closedir($handle);
176
        }
177
178 17
        if (is_link($directory)) {
179 2
            self::unlink($directory);
180
        } else {
181 17
            rmdir($directory);
182
        }
183 17
    }
184
185
    /**
186
     * Removes a file or symlink in a cross-platform way.
187
     *
188
     * @param string $path
189
     *
190
     * @return bool
191
     */
192 10
    public static function unlink(string $path): bool
193
    {
194 10
        $isWindows = DIRECTORY_SEPARATOR === '\\';
195
196 10
        if (!$isWindows) {
197 10
            return unlink($path);
198
        }
199
200
        if (is_link($path) && is_dir($path)) {
201
            return rmdir($path);
202
        }
203
204
        return unlink($path);
205
    }
206
207
    /**
208
     * Copies a whole directory as another one.
209
     *
210
     * The files and sub-directories will also be copied over.
211
     *
212
     * @param string $source the source directory.
213
     * @param string $destination the destination directory.
214
     * @param array $options options for directory copy. Valid options are:
215
     *
216
     * - dirMode: integer, the permission to be set for newly copied directories. Defaults to 0775.
217
     * - fileMode:  integer, the permission to be set for newly copied files. Defaults to the current environment
218
     *   setting.
219
     * - filter: callback, a PHP callback that is called for each directory or file.
220
     *   The signature of the callback should be: `function ($path)`, where `$path` refers the full path to be filtered.
221
     *   The callback can return one of the following values:
222
     *
223
     *   * true: the directory or file will be copied (the "only" and "except" options will be ignored).
224
     *   * false: the directory or file will NOT be copied (the "only" and "except" options will be ignored).
225
     *   * null: the "only" and "except" options will determine whether the directory or file should be copied.
226
     *
227
     * - only: array, list of patterns that the file paths should match if they want to be copied. A path matches a
228
     *   pattern if it contains the pattern string at its end. For example, '.php' matches all file paths ending with
229
     *   '.php'.
230
     *   Note, the '/' characters in a pattern matches both '/' and '\' in the paths. If a file path matches a pattern
231
     *   in both "only" and "except", it will NOT be copied.
232
     * - except: array, list of patterns that the files or directories should match if they want to be excluded from
233
     *   being copied. A path matches a pattern if it contains the pattern string at its end. Patterns ending with '/'
234
     *   apply to directory paths only, and patterns not ending with '/' apply to file paths only. For example, '/a/b'
235
     *   matches all file paths ending with '/a/b'; and '.svn/' matches directory paths ending with '.svn'. Note, the
236
     *   '/' characters in a pattern matches both '/' and '\' in the paths.
237
     * - caseSensitive: boolean, whether patterns specified at "only" or "except" should be case sensitive. Defaults to
238
     *   true.
239
     * - recursive: boolean, whether the files under the subdirectories should also be copied. Defaults to true.
240
     * - beforeCopy: callback, a PHP callback that is called before copying each sub-directory or file. If the callback
241
     *   returns false, the copy operation for the sub-directory or file will be cancelled. The signature of the
242
     *   callback should be: `function ($from, $to)`, where `$from` is the sub-directory or file to be copied from,
243
     *   while `$to` is the copy target.
244
     * - afterCopy: callback, a PHP callback that is called after each sub-directory or file is successfully copied.
245
     *   The signature of the callback should be: `function ($from, $to)`, where `$from` is the sub-directory or file
246
     *   copied from, while `$to` is the copy target.
247
     * - copyEmptyDirectories: boolean, whether to copy empty directories. Set this to false to avoid creating
248
     *   directories that do not contain files. This affects directories that do not contain files initially as well as
249
     *   directories that do not contain files at the target destination because files have been filtered via `only` or
250
     *   `except`. Defaults to true.
251
     *
252
     * @throws \InvalidArgumentException if unable to open directory
253
     * @throws \Exception
254
     *
255
     * @return void
256
     */
257 11
    public static function copyDirectory(string $source, string $destination, array $options = []): void
258
    {
259 11
        $source = static::normalizePath($source);
260 11
        $destination = static::normalizePath($destination);
261
262 11
        static::isSelfDirectory($source, $destination);
263
264 9
        $destinationExists = static::setDestination($destination, $options);
265
266 9
        $handle = static::isSourceDirectory($source);
267
268 9
        $options = static::setBasePath($source, $options);
269
270 9
        while (($file = readdir($handle)) !== false) {
271 9
            if ($file === '.' || $file === '..') {
272 9
                continue;
273
            }
274
275 7
            $from = $source . '/' . $file;
276 7
            $to = $destination . '/' . $file;
277
278 7
            if (static::filterPath($from, $options)) {
279 7
                if (is_file($from)) {
280 7
                    if (!$destinationExists) {
281 2
                        static::createDirectory($destination, $options['dirMode'] ?? 0775);
282 2
                        $destinationExists = true;
283
                    }
284 7
                    copy($from, $to);
285 7
                    if (isset($options['fileMode'])) {
286 7
                        static::chmod($to, $options['fileMode']);
287
                    }
288 6
                } elseif (!isset($options['recursive']) || $options['recursive']) {
289 5
                    static::copyDirectory($from, $to, $options);
290
                }
291
            }
292
        }
293
294 9
        closedir($handle);
295 9
    }
296
297
    /**
298
     * Check copy it self directory.
299
     *
300
     * @param string $source
301
     * @param string $destination
302
     *
303
     * @throws \InvalidArgumentException
304
     *
305
     * @return boolean
306
     */
307 11
    private static function isSelfDirectory(string $source, string $destination)
308
    {
309 11
        if ($source === $destination || strpos($destination, $source . '/') === 0) {
310 2
            throw new \InvalidArgumentException('Trying to copy a directory to itself or a subdirectory.');
311
        }
312 9
    }
313
314
    /**
315
     * Undocumented function
316
     *
317
     * @param string $source
318
     *
319
     * @throws \InvalidArgumentException
320
     *
321
     * @return resource
322
     */
323 9
    private static function isSourceDirectory(string $source)
324
    {
325 9
        $handle = @opendir($source);
326
327 9
        if ($handle === false) {
328
            throw new \InvalidArgumentException("Unable to open directory: $source");
329
        }
330
331 9
        return $handle;
332
    }
333
334
    /**
335
     * Set base path directory.
336
     *
337
     * @param string $source
338
     * @param array $options
339
     *
340
     * @return array
341
     */
342 9
    private static function setBasePath(string $source, array $options): array
343
    {
344 9
        if (!isset($options['basePath'])) {
345
            // this should be done only once
346 9
            $options['basePath'] = realpath($source);
347 9
            $options = static::normalizeOptions($options);
348
        }
349
350 9
        return $options;
351
    }
352
353
    /**
354
     * Set destination directory.
355
     *
356
     * @param string $destination
357
     * @param array $options
358
     *
359
     * @return bool
360
     */
361 9
    private static function setDestination(string $destination, array $options): bool
362
    {
363 9
        $destinationExists = is_dir($destination);
364
365 9
        if (!$destinationExists && (!isset($options['copyEmptyDirectories']) || $options['copyEmptyDirectories'])) {
366 5
            static::createDirectory($destination, $options['dirMode'] ?? 0775);
367 5
            $destinationExists = true;
368
        }
369
370 9
        return $destinationExists;
371
    }
372
373
    /**
374
     * Normalize options.
375
     *
376
     * @param array $options raw options.
377
     *
378
     * @return array normalized options.
379
     */
380 9
    protected static function normalizeOptions(array $options): array
381
    {
382 9
        if (!array_key_exists('caseSensitive', $options)) {
383 9
            $options['caseSensitive'] = true;
384
        }
385
386 9
        if (isset($options['except'])) {
387 1
            foreach ($options['except'] as $key => $value) {
388 1
                if (\is_string($value)) {
389 1
                    $options['except'][$key] = self::parseExcludePattern($value, $options['caseSensitive']);
390
                }
391
            }
392
        }
393
394 9
        if (isset($options['only'])) {
395 2
            foreach ($options['only'] as $key => $value) {
396 2
                if (\is_string($value)) {
397 2
                    $options['only'][$key] = self::parseExcludePattern($value, $options['caseSensitive']);
398
                }
399
            }
400
        }
401
402 9
        return $options;
403
    }
404
405
    /**
406
     * Checks if the given file path satisfies the filtering options.
407
     *
408
     * @param string $path the path of the file or directory to be checked.
409
     * @param array $options the filtering options.
410
     *
411
     * @return bool whether the file or directory satisfies the filtering options.
412
     */
413 7
    public static function filterPath(string $path, array $options): bool
414
    {
415 7
        $path = str_replace('\\', '/', $path);
416
417 7
        if (!empty($options['except'])) {
418 1
            if ((self::lastExcludeMatchingFromList($options['basePath'], $path, $options['except'])) !== null) {
419 1
                return false;
420
            }
421
        }
422
423 7
        if (!empty($options['only']) && !is_dir($path)) {
424
            // don't check PATTERN_NEGATIVE since those entries are not prefixed with !
425 2
            return self::lastExcludeMatchingFromList($options['basePath'], $path, $options['only']) !== null;
426
        }
427
428 7
        return true;
429
    }
430
431
    /**
432
     * Searches for the first wildcard character in the pattern.
433
     *
434
     * @param string $pattern the pattern to search in.
435
     *
436
     * @return int|bool position of first wildcard character or false if not found.
437
     */
438 2
    private static function firstWildcardInPattern(string $pattern)
439
    {
440 2
        $wildcards = ['*', '?', '[', '\\'];
441
        $wildcardSearch = function ($carry, $item) use ($pattern) {
442 2
            $position = strpos($pattern, $item);
443 2
            if ($position === false) {
444 2
                return $carry === false ? $position : $carry;
445
            }
446 2
            return $carry === false ? $position : min($carry, $position);
447 2
        };
448 2
        return array_reduce($wildcards, $wildcardSearch, false);
449
    }
450
451
452
    /**
453
     * Scan the given exclude list in reverse to see whether pathname should be ignored.
454
     *
455
     * The first match (i.e. the last on the list), if any, determines the fate.  Returns the element which matched,
456
     * or null for undecided.
457
     *
458
     * Based on last_exclude_matching_from_list() from dir.c of git 1.8.5.3 sources.
459
     *
460
     * @param string $basePath.
461
     * @param string $path.
462
     * @param array $excludes list of patterns to match $path against.
463
     *
464
     * @return null|array null or one of $excludes item as an array with keys: 'pattern', 'flags'.
465
     *
466
     * @throws \InvalidArgumentException if any of the exclude patterns is not a string or an array with keys: pattern,
467
     *                                   flags, firstWildcard.
468
     */
469 2
    private static function lastExcludeMatchingFromList(string $basePath, string $path, array $excludes): ?array
470
    {
471 2
        foreach (array_reverse($excludes) as $exclude) {
472 2
            if (\is_string($exclude)) {
473
                $exclude = self::parseExcludePattern($exclude, false);
474
            }
475
476 2
            if (!isset($exclude['pattern'], $exclude['flags'], $exclude['firstWildcard'])) {
477
                throw new \InvalidArgumentException(
478
                    'If exclude/include pattern is an array it must contain the pattern, flags and firstWildcard keys.'
479
                );
480
            }
481
482 2
            if (($exclude['flags'] & self::PATTERN_MUST_BE_DIR) && !is_dir($path)) {
483
                continue;
484
            }
485
486 2
            if ($exclude['flags'] & self::PATTERN_NO_DIR) {
487
                if (self::matchBasename(basename($path), $exclude['pattern'], $exclude['firstWildcard'], $exclude['flags'])) {
488
                    return $exclude;
489
                }
490
                continue;
491
            }
492
493 2
            if (self::matchPathname($path, $basePath, $exclude['pattern'], $exclude['firstWildcard'], $exclude['flags'])) {
494 2
                return $exclude;
495
            }
496
        }
497
498 2
        return null;
499
    }
500
501
    /**
502
     * Performs a simple comparison of file or directory names.
503
     *
504
     * Based on match_basename() from dir.c of git 1.8.5.3 sources.
505
     *
506
     * @param string $baseName file or directory name to compare with the pattern.
507
     * @param string $pattern the pattern that $baseName will be compared against.
508
     * @param int|bool $firstWildcard location of first wildcard character in the $pattern.
509
     * @param int $flags pattern flags
510
     *
511
     * @return bool whether the name matches against pattern
512
     */
513
    private static function matchBasename(string $baseName, string $pattern, $firstWildcard, int $flags): bool
514
    {
515
        if ($firstWildcard === false) {
516
            if ($pattern === $baseName) {
517
                return true;
518
            }
519
        } elseif ($flags & self::PATTERN_ENDS_WITH) {
520
            /* "*literal" matching against "fooliteral" */
521
            $n = StringHelper::byteLength($pattern);
522
            if (StringHelper::byteSubstr($pattern, 1, $n) === StringHelper::byteSubstr($baseName, -$n, $n)) {
523
                return true;
524
            }
525
        }
526
527
        $matchOptions = [];
528
529
        if ($flags & self::PATTERN_CASE_INSENSITIVE) {
530
            $matchOptions['caseSensitive'] = false;
531
        }
532
533
        return StringHelper::matchWildcard($pattern, $baseName, $matchOptions);
534
    }
535
536
    /**
537
     * Compares a path part against a pattern with optional wildcards.
538
     *
539
     * Based on match_pathname() from dir.c of git 1.8.5.3 sources.
540
     *
541
     * @param string $path full path to compare
542
     * @param string $basePath base of path that will not be compared
543
     * @param string $pattern the pattern that path part will be compared against
544
     * @param int|bool $firstWildcard location of first wildcard character in the $pattern
545
     * @param int $flags pattern flags
546
     *
547
     * @return bool whether the path part matches against pattern
548
     */
549 2
    private static function matchPathname(string $path, string $basePath, string $pattern, $firstWildcard, int $flags): bool
550
    {
551
        // match with FNM_PATHNAME; the pattern has base implicitly in front of it.
552 2
        if (strpos($pattern, '/') === 0) {
553
            $pattern = StringHelper::byteSubstr($pattern, 1, StringHelper::byteLength($pattern));
554
            if ($firstWildcard !== false && $firstWildcard !== 0) {
555
                $firstWildcard--;
556
            }
557
        }
558
559 2
        $namelen = StringHelper::byteLength($path) - (empty($basePath) ? 0 : StringHelper::byteLength($basePath) + 1);
560 2
        $name = StringHelper::byteSubstr($path, -$namelen, $namelen);
561
562 2
        if ($firstWildcard !== 0) {
563 2
            if ($firstWildcard === false) {
564 1
                $firstWildcard = StringHelper::byteLength($pattern);
565
            }
566
567
            // if the non-wildcard part is longer than the remaining pathname, surely it cannot match.
568 2
            if ($firstWildcard > $namelen) {
569 1
                return false;
570
            }
571
572 2
            if (strncmp($pattern, $name, $firstWildcard)) {
0 ignored issues
show
Bug introduced by
It seems like $firstWildcard can also be of type true; however, parameter $len of strncmp() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

572
            if (strncmp($pattern, $name, /** @scrutinizer ignore-type */ $firstWildcard)) {
Loading history...
573 2
                return false;
574
            }
575
576 2
            $pattern = StringHelper::byteSubstr($pattern, $firstWildcard, StringHelper::byteLength($pattern));
0 ignored issues
show
Bug introduced by
It seems like $firstWildcard can also be of type true; however, parameter $start of Yiisoft\Strings\StringHelper::byteSubstr() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

576
            $pattern = StringHelper::byteSubstr($pattern, /** @scrutinizer ignore-type */ $firstWildcard, StringHelper::byteLength($pattern));
Loading history...
577 2
            $name = StringHelper::byteSubstr($name, $firstWildcard, $namelen);
578
579
            // 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.
580 2
            if (empty($pattern) && empty($name)) {
581 1
                return true;
582
            }
583
        }
584
585
        $matchOptions = [
586 2
            'filePath' => true
587
        ];
588
589 2
        if ($flags & self::PATTERN_CASE_INSENSITIVE) {
590
            $matchOptions['caseSensitive'] = false;
591
        }
592
593 2
        return StringHelper::matchWildcard($pattern, $name, $matchOptions);
594
    }
595
596
    /**
597
     * Processes the pattern, stripping special characters like / and ! from the beginning and settings flags instead.
598
     *
599
     * @param string $pattern
600
     * @param bool $caseSensitive
601
     *
602
     * @return array with keys: (string) pattern, (int) flags, (int|bool) firstWildcard
603
     */
604 2
    private static function parseExcludePattern(string $pattern, bool $caseSensitive): array
605
    {
606
        $result = [
607 2
            'pattern' => $pattern,
608 2
            'flags' => 0,
609
            'firstWildcard' => false,
610
        ];
611
612 2
        $result = static::isCaseInsensitive($caseSensitive, $result);
613
614 2
        if (!isset($pattern[0])) {
615
            return $result;
616
        }
617
618 2
        if (strpos($pattern, '!') === 0) {
619
            $result['flags'] |= self::PATTERN_NEGATIVE;
620
            $pattern = StringHelper::byteSubstr($pattern, 1, StringHelper::byteLength($pattern));
621
        }
622
623 2
        if (StringHelper::byteLength($pattern) && StringHelper::byteSubstr($pattern, -1, 1) === '/') {
624
            $pattern = StringHelper::byteSubstr($pattern, 0, -1);
625
            $result['flags'] |= self::PATTERN_MUST_BE_DIR;
626
        }
627
628 2
        $result = static::isPatternNoDir($pattern, $result);
629
630 2
        $result['firstWildcard'] = self::firstWildcardInPattern($pattern);
631
632 2
        $result = static::isPatternEndsWith($pattern, $result);
633
634 2
        $result['pattern'] = $pattern;
635
636 2
        return $result;
637
    }
638
639
    /**
640
     * Check isCaseInsensitive.
641
     *
642
     * @param boolean $caseSensitive
643
     * @param array $result
644
     *
645
     * @return array
646
     */
647 2
    private static function isCaseInsensitive(bool $caseSensitive, array $result): array
648
    {
649 2
        if (!$caseSensitive) {
650
            $result['flags'] |= self::PATTERN_CASE_INSENSITIVE;
651
        }
652
653 2
        return $result;
654
    }
655
656
    /**
657
     * Check pattern no directory.
658
     *
659
     * @param string $pattern
660
     * @param array $result
661
     *
662
     * @return array
663
     */
664 2
    private static function isPatternNoDir(string $pattern, array $result): array
665
    {
666 2
        if (strpos($pattern, '/') === false) {
667
            $result['flags'] |= self::PATTERN_NO_DIR;
668
        }
669
670 2
        return $result;
671
    }
672
673
    /**
674
     * Check pattern ends with
675
     *
676
     * @param string $pattern
677
     * @param array $result
678
     *
679
     * @return array
680
     */
681 2
    private static function isPatternEndsWith(string $pattern, array $result): array
682
    {
683 2
        if (strpos($pattern, '*') === 0 && self::firstWildcardInPattern(StringHelper::byteSubstr($pattern, 1, StringHelper::byteLength($pattern))) === false) {
684
            $result['flags'] |= self::PATTERN_ENDS_WITH;
685
        }
686
687 2
        return $result;
688
    }
689
}
690