Completed
Pull Request — master (#63)
by Bill
01:27
created

FileManager::fileShouldBeIgnored()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
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
     * FileManager constructor.
16
     * @param Config $config Configuration Settings.
17
     */
18
    public function __construct(Config $config)
19
    {
20
        $this->config = $config;
0 ignored issues
show
Bug introduced by
The property config does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
21
    }
22
23
    /**
24
     * Recursively finds all files with the .php extension in the provided
25
     * $path and returns list as array.
26
     * @param  string $path Path to look for .php files.
27
     * @return FileCollection
28
     */
29
    public function getPhpFiles(string $path): FileCollection
30
    {
31
        $directoryIterator = new RecursiveDirectoryIterator($path);
32
        $files = new FileCollection;
33
        foreach (new RecursiveIteratorIterator($directoryIterator) as $file) {
34
            if ($file->getExtension() !== 'php') {
35
                continue;
36
            }
37
38
            if ($this->fileShouldBeIgnored($file)) {
39
                continue;
40
            }
41
42
43
            $files->push(new File(['displayPath' => $file->getPathName(), 'fullPath' => $file->getRealPath()]));
44
        }
45
46
        return $files;
47
    }
48
49
    /**
50
     * Determines if a file should be ignored.
51
     * @param \SplFileInfo $file File.
52
     * @return boolean
53
     */
54
    private function fileShouldBeIgnored(SplFileInfo $file): bool
55
    {
56
        foreach ($this->config->getFilesToIgnore() as $fileToIgnore) {
57
            $fileToIgnore = str_replace('/', '\/', $fileToIgnore);
58
            if (preg_match("/{$fileToIgnore}/", $file->getPathName())) {
59
                return true;
60
            }
61
        }
62
63
        return false;
64
    }
65
}
66