Completed
Pull Request — master (#72)
by
unknown
01:33
created

FileManager   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 62
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B getPhpFiles() 0 21 5
A fileShouldBeIgnored() 0 11 3
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
     * FileManager constructor.
22
     * @param Config $config Configuration Settings.
23
     */
24
    public function __construct(Config $config)
25
    {
26
        $this->config = $config;
27
    }
28
29
    /**
30
     * Recursively finds all files with the .php extension in the provided
31
     * $path and returns list as array.
32
     * @param  array $paths Paths in which to look for .php files.
33
     * @return FileCollection
34
     */
35
    public function getPhpFiles(array $paths): FileCollection
36
    {
37
        $files = new FileCollection;
38
        foreach ($paths as $path) {
39
            $directoryIterator = new RecursiveDirectoryIterator($path);
40
            foreach (new RecursiveIteratorIterator($directoryIterator) as $file) {
41
                if ($file->getExtension() !== 'php') {
42
                    continue;
43
                }
44
45
                if ($this->fileShouldBeIgnored($file)) {
46
                    continue;
47
                }
48
49
50
                $files->push(new File(['displayPath' => $file->getPathName(), 'fullPath' => $file->getRealPath()]));
51
            }
52
        }
53
54
        return $files;
55
    }
56
57
    /**
58
     * Determines if a file should be ignored.
59
     * @param \SplFileInfo $file File.
60
     * @return boolean
61
     */
62
    private function fileShouldBeIgnored(SplFileInfo $file): bool
63
    {
64
        foreach ($this->config->getFilesToIgnore() as $fileToIgnore) {
65
            $fileToIgnore = str_replace('/', '\/', $fileToIgnore);
66
            if (preg_match("/{$fileToIgnore}/", $file->getPathName())) {
67
                return true;
68
            }
69
        }
70
71
        return false;
72
    }
73
}
74