Completed
Pull Request — master (#75)
by Bill
01:39
created

FileManager::getPhpFiles()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
1
<?php declare(strict_types = 1);
2
3
namespace Churn\Managers;
4
5
use Churn\Collections\FileCollection;
6
use Churn\Values\Config;
7
use RecursiveDirectoryIterator;
8
use RecursiveIteratorIterator;
9
use Churn\Values\File;
10
use SplFileInfo;
11
12
class FileManager
13
{
14
    /**
15
     * The config values.
16
     * @var Config
17
     */
18
    private $config;
19
20
    /**
21
     * Collection of File objects.
22
     * @var FileCollection;
23
     */
24
    private $files;
25
26
    /**
27
     * FileManager constructor.
28
     * @param Config $config Configuration Settings.
29
     */
30
    public function __construct(Config $config)
31
    {
32
        $this->config = $config;
33
    }
34
35
    /**
36
     * Recursively finds all files with the .php extension in the provided
37
     * $paths and returns list as array.
38
     * @param  array $paths Paths in which to look for .php files.
39
     * @return FileCollection
40
     */
41
    public function getPhpFiles(array $paths): FileCollection
42
    {
43
        $this->files = new FileCollection;
44
        foreach ($paths as $path) {
45
            $this->getPhpFilesFromPath($path);
46
        }
47
48
        return $this->files;
49
    }
50
51
    /**
52
     * Recursively finds all files with the .php extension in the provided
53
     * $path adds them to $this->files.
54
     * @param  string $path Path in which to look for .php files.
55
     * @return void
56
     */
57
    private function getPhpFilesFromPath(string $path)
58
    {
59
        $directoryIterator = new RecursiveDirectoryIterator($path);
60
        foreach (new RecursiveIteratorIterator($directoryIterator) as $file) {
61
            if ($file->getExtension() !== 'php') {
62
                continue;
63
            }
64
65
            if ($this->fileShouldBeIgnored($file)) {
66
                continue;
67
            }
68
69
            $this->files->push(new File(['displayPath' => $file->getPathName(), 'fullPath' => $file->getRealPath()]));
70
        }
71
    }
72
73
    /**
74
     * Determines if a file should be ignored.
75
     * @param \SplFileInfo $file File.
76
     * @return boolean
77
     */
78
    private function fileShouldBeIgnored(SplFileInfo $file): bool
79
    {
80
        foreach ($this->config->getFilesToIgnore() as $fileToIgnore) {
81
            $fileToIgnore = str_replace('/', '\/', $fileToIgnore);
82
            if (preg_match("/{$fileToIgnore}/", $file->getPathName())) {
83
                return true;
84
            }
85
        }
86
87
        return false;
88
    }
89
}
90