Completed
Pull Request — 2.x (#402)
by
unknown
11:10 queued 07:17
created

Enumerator::getExcludePaths()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
1
<?php
2
/*
3
 * Go! AOP framework
4
 *
5
 * @copyright Copyright 2015, Lisachenko Alexander <[email protected]>
6
 *
7
 * This source file is subject to the license that is bundled
8
 * with this source code in the file LICENSE.
9
 */
10
11
namespace Go\Instrument\FileSystem;
12
13
use CallbackFilterIterator;
14
use InvalidArgumentException;
15
use LogicException;
16
use RecursiveIteratorIterator;
17
use SplFileInfo;
18
use Symfony\Component\Finder\Finder;
19
use UnexpectedValueException;
20
21
/**
22
 * Enumerates files in the concrete directory, applying filtration logic
23
 */
24
class Enumerator
25
{
26
27
    /**
28
     * Path to the root directory, where enumeration should start
29
     *
30
     * @var string
31
     */
32
    private $rootDirectory;
33
34
    /**
35
     * List of additional include paths, should be below rootDirectory
36
     *
37
     * @var array
38
     */
39
    private $includePaths;
40
41
    /**
42
     * List of additional exclude paths, should be below rootDirectory
43
     *
44
     * @var array
45
     */
46
    private $excludePaths;
47
48
    /**
49
     * Initializes an enumerator
50
     *
51
     * @param string $rootDirectory Path to the root directory
52
     * @param array  $includePaths  List of additional include paths
53
     * @param array  $excludePaths  List of additional exclude paths
54
     */
55 7
    public function __construct($rootDirectory, array $includePaths = [], array $excludePaths = [])
56
    {
57 7
        $this->rootDirectory = $rootDirectory;
58 7
        $this->includePaths = $includePaths;
59 7
        $this->excludePaths = $excludePaths;
60 7
    }
61
62
    /**
63
     * Returns an enumerator for files
64
     *
65
     * @return CallbackFilterIterator|RecursiveIteratorIterator|SplFileInfo[]
66
     * @throws UnexpectedValueException
67
     * @throws InvalidArgumentException
68
     * @throws LogicException
69
     */
70 7
    public function enumerate()
71
    {
72 7
        $finder = new Finder();
73 7
        $finder->files()
74 7
            ->name('*.php')
75 7
            ->in($this->getInPaths());
76
77 7
        foreach ($this->getExcludePaths() as $path) {
78 4
            $finder->notPath($path);
79
        }
80
81 7
        return $finder->getIterator();
82
    }
83
84
    /**
85
     * @return array
86
     * @throws UnexpectedValueException
87
     */
88 7
    private function getInPaths()
89
    {
90 7
        $inPaths = [];
91
92 7
        foreach ($this->includePaths as $path) {
93 1
            if (strpos($path, $this->rootDirectory, 0) === false) {
94
                throw new UnexpectedValueException(sprintf('Path %s is not in %s', $path, $this->rootDirectory));
95
            }
96
97 1
            $path = str_replace('*', '', $path);
98 1
            $inPaths[] = $path;
99
        }
100
101 7
        if (empty($inPaths)) {
102 6
            $inPaths[] = $this->rootDirectory;
103
        }
104
105 7
        return $inPaths;
106
    }
107
108
    /**
109
     * @return array
110
     */
111 7
    private function getExcludePaths()
112
    {
113 7
        $excludePaths = [];
114
115 7
        foreach ($this->excludePaths as $path) {
116 4
            $path = str_replace('*', '.*', $path);
117 4
            $excludePaths[] = '#' . str_replace($this->rootDirectory . '/', '', $path) . '#';
118
        }
119
120 7
        return $excludePaths;
121
    }
122
123
    /**
124
     * Returns a filter callback for enumerating files
125
     *
126
     * @return \Closure
127
     */
128
    public function getFilter()
129
    {
130
        $rootDirectory = $this->rootDirectory;
131
        $includePaths = $this->includePaths;
132
        $excludePaths = $this->excludePaths;
133
134
        return function (SplFileInfo $file) use ($rootDirectory, $includePaths, $excludePaths) {
135
136
            if ($file->getExtension() !== 'php') {
137
                return false;
138
            }
139
140
            $fullPath = $this->getFileFullPath($file);
141
            // Do not touch files that not under rootDirectory
142
            if (strpos($fullPath, $rootDirectory) !== 0) {
143
                return false;
144
            }
145
146
            if (!empty($includePaths)) {
147
                $found = false;
148
                foreach ($includePaths as $includePattern) {
149
                    if (fnmatch("{$includePattern}*", $fullPath, FNM_NOESCAPE)) {
150
                        $found = true;
151
                        break;
152
                    }
153
                }
154
                if (!$found) {
155
                    return false;
156
                }
157
            }
158
159
            foreach ($excludePaths as $excludePattern) {
160
                if (fnmatch("{$excludePattern}*", $fullPath, FNM_NOESCAPE)) {
161
                    return false;
162
                }
163
            }
164
165
            return true;
166
        };
167
    }
168
169
    /**
170
     * Return the real path of the given file
171
     *
172
     * This is used for testing purpose with virtual file system.
173
     * In a vfs the 'realPath' methode will always return false.
174
     * So we have a chance to mock this single function to return different path.
175
     *
176
     * @param SplFileInfo $file
177
     *
178
     * @return string
179
     */
180
    protected function getFileFullPath(SplFileInfo $file)
181
    {
182
        return $file->getRealPath();
183
    }
184
185
}
186