Completed
Pull Request — master (#74)
by Marcel
02:43
created

Finder::findFromConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
ccs 0
cts 8
cp 0
rs 9.4285
cc 1
eloc 5
nc 1
nop 2
crap 2
1
<?php
2
3
namespace PHPSemVerChecker\Finder;
4
5
use Finder\Adapter\SymfonyFinder as BaseFinder;
6
use PHPSemVerChecker\Configuration\Configuration;
7
8
class Finder
9
{
10
	/**
11
	 * @param string $path
12
	 * @param array $includes
13
	 * @param array $excludes
14
	 * @return array
15
	 */
16
	public function find($path, array $includes, array $excludes = [])
17
	{
18
		$finder = new BaseFinder();
19
		$finder->ignoreDotFiles(true)
20
			->files()
21
			->name('*.php')
22
			->in($path);
23
24
		foreach ($includes as $include) {
25
			$finder->path($include);
26
		}
27
28
		foreach ($excludes as $exclude) {
29
			$finder->notPath($exclude);
30
		}
31
32
		$files = [];
33
		foreach ($finder as $file) {
34
			$files[] = $file->getRealpath();
35
		}
36
37
		return $files;
38
	}
39
40
	/**
41
	 * @param string $path
42
	 * @param string $includes
43
	 * @param string $excludes
44
	 * @return array
45
	 */
46
	public function findFromString($path, $includes, $excludes)
47
	{
48
		if ($includes === '*') {
49
			$includes = [];
50
		} else {
51
			$includes = preg_split('@(?:\s*,\s*|^\s*|\s*$)@', $includes, NULL, PREG_SPLIT_NO_EMPTY);
52
		}
53
54
		if ($excludes === '*') {
55
			$excludes = [];
56
		} else {
57
			$excludes = preg_split('@(?:\s*,\s*|^\s*|\s*$)@', $excludes, NULL, PREG_SPLIT_NO_EMPTY);
58
		}
59
60
		return $this->find($path, $includes, $excludes);
61
	}
62
63
	/**
64
	 * Find files based on source/include/exclude-after/before config.
65
	 *
66
	 * @param Configuration $config
67
	 * @param string $suffix 'before' or 'after'
68
	 * @return array
69
	 */
70
	public function findFromConfig(Configuration $config, $suffix)
71
	{
72
		return $this->findFromString(
73
			$config['source-' . $suffix],
74
			$config['include-' . $suffix],
75
			$config['exclude-' . $suffix]
76
		);
77
	}
78
}
79