IOResourcePathResolution   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 1
dl 0
loc 65
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getDirectory() 0 4 2
A getFeatureMatch() 0 4 2
A __invoke() 0 26 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
7
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
8
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
9
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
10
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
11
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
12
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
13
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
14
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
15
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
16
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
17
 *
18
 * This software consists of voluntary contributions made by many individuals
19
 * and is licensed under the MIT license.
20
 */
21
namespace DocHeader\Helper;
22
23
use InvalidArgumentException;
24
use Symfony\Component\Finder\Finder;
25
use function array_map;
26
use function basename;
27
use function dirname;
28
use function is_dir;
29
use function rtrim;
30
31
final class IOResourcePathResolution
32
{
33
    /** @var string[] */
34
    private $directoryOrFile;
35
36
    /** @var string[] */
37
    private $excludedDirectory;
38
39
    /** @var string[] */
40
    private $excludedFiles;
41
42
    /**
43
     * @param string[] $directoryOrFile
44
     * @param string[] $excludedDirectory
45
     * @param string[] $excludedFiles
46
     */
47
    public function __construct(array $directoryOrFile, array $excludedDirectory, array $excludedFiles)
48 5
    {
49
        $this->directoryOrFile   = $directoryOrFile;
50 5
        $this->excludedDirectory = $excludedDirectory;
51 5
        $this->excludedFiles     = $excludedFiles;
52 5
    }
53 5
54
    private function getDirectory(string $directoryOrFile) : string
55 5
    {
56
        return is_dir($directoryOrFile) ? $directoryOrFile : dirname($directoryOrFile);
57 5
    }
58
59
    private function getFeatureMatch(string $directoryOrFile) : string
60 5
    {
61
        return is_dir($directoryOrFile) ? '*.php' : basename($directoryOrFile);
62 5
    }
63
64
    /**
65
     * @return Finder[]
66
     *
67
     * @throws InvalidArgumentException
68
     */
69
    public function __invoke() : array
70 5
    {
71
        return array_map(
72 5
            /**
73 5
             * @param string $directoryOrFile
74 5
             *
75 5
             * @return Finder
76 5
             */
77 5
            function ($directoryOrFile) {
78 5
                $finder = Finder::create()
79 5
                    ->files()
80
                    ->ignoreDotFiles(true)
81 5
                    ->in(rtrim($this->getDirectory($directoryOrFile), '/'))
82 1
                    ->name($this->getFeatureMatch($directoryOrFile))
83 5
                    ->exclude($this->excludedDirectory)
84
                    ->sortByName();
85 5
86 5
                foreach ($this->excludedFiles as $pattern) {
87 5
                    $finder->notPath($pattern);
88 5
                }
89
90
                return $finder;
91
            },
92
            $this->directoryOrFile
93
        );
94
    }
95
}
96