Passed
Push — master ( dafcc2...9bd526 )
by Arnaud
05:52
created

FileExtensionFilter::getChildren()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Cecil.
7
 *
8
 * Copyright (c) Arnaud Ligny <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Cecil\Command\ShowContent;
15
16
use RecursiveFilterIterator;
17
18
/**
19
 * Filters files by extension type.
20
 */
21
class FileExtensionFilter extends RecursiveFilterIterator
22
{
23
    /** @var array */
24
    protected $allowedExt = ['md', 'yml'];
25
26
    /** @var array */
27
    protected $excludedDir = ['.git', '.cecil', '.cache', '_site', 'vendor', 'node_modules'];
28
29
    /**
30
     * @param \RecursiveIterator $iterator
31
     * @param string|array|null  $extensions
32
     */
33
    public function __construct(\RecursiveIterator $iterator, $extensions = null)
34
    {
35
        if (!\is_null($extensions)) {
36
            if (!\is_array($extensions)) {
37
                $extensions = [$extensions];
38
            }
39
            $this->allowedExt = $extensions;
40
        }
41
        parent::__construct($iterator);
42
    }
43
44
    /**
45
     * Get children with allowed extensions.
46
     */
47
    public function getChildren(): ?RecursiveFilterIterator
48
    {
49
        return new self($this->getInnerIterator()->getChildren(), $this->allowedExt); // @phpstan-ignore-line
0 ignored issues
show
Bug introduced by
The method getChildren() does not exist on Iterator. It seems like you code against a sub-type of Iterator such as RecursiveIterator or SimpleXMLElement or RecursiveCachingIterator or Symfony\Component\Finder...DirectoryFilterIterator or RecursiveFilterIterator or RecursiveCallbackFilterIterator or RecursiveRegexIterator or Phar or SplFileObject or RecursiveDirectoryIterator or RecursiveArrayIterator. ( Ignorable by Annotation )

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

49
        return new self($this->getInnerIterator()->/** @scrutinizer ignore-call */ getChildren(), $this->allowedExt); // @phpstan-ignore-line
Loading history...
50
    }
51
52
    /**
53
     * Valid file with allowed extensions.
54
     */
55
    public function accept(): bool
56
    {
57
        /** @var \SplFileInfo $file */
58
        $file = $this->current();
59
        if ($file->isFile()) {
60
            return \in_array($file->getExtension(), $this->allowedExt);
61
        }
62
        if ($file->isDir()) {
63
            return !\in_array($file->getBasename(), $this->excludedDir);
64
        }
65
66
        return true;
67
    }
68
}
69