Test Failed
Push — master ( 857c27...82742e )
by Marius
01:45
created

SourceFolderHelper::findAdditionalTestFolders()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 0
cts 9
cp 0
rs 9.7666
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Paysera\PhpStormHelper\Service;
6
7
use Paysera\PhpStormHelper\Entity\SourceFolder;
8
use Symfony\Component\Finder\Finder;
9
use Symfony\Component\Finder\SplFileInfo;
10
11
class SourceFolderHelper
12
{
13
    private $configurationOptionFinder;
14
    private $defaultSourceFolders;
15
16
    public function __construct(
17
        ConfigurationOptionFinder $configurationOptionFinder,
18
        array $defaultSourceFolders
19
    ) {
20
        $this->configurationOptionFinder = $configurationOptionFinder;
21
        $this->defaultSourceFolders = $defaultSourceFolders;
22
    }
23
24
    public function getSourceFolders(string $projectRootDir): array
25
    {
26
        $folders = array_merge(
27
            $this->defaultSourceFolders,
28
            $this->findAdditionalTestFolders($projectRootDir),
29
            $this->configurationOptionFinder->findConfiguredSourceFolders($projectRootDir)
30
        );
31
32
        $folders = $this->mapAndFilterExisting($projectRootDir, $folders);
33
34
        // as it might not have been created yet, exclude `vendor` always
35
        if (!isset($folders['vendor'])) {
36
            $folders[] = new SourceFolder('vendor', SourceFolder::TYPE_EXCLUDED);
37
        }
38
39
        return array_values($folders);
40
    }
41
42
    /**
43
     * @param string $projectRootDir
44
     * @return array|SourceFolder[]
45
     */
46
    private function findAdditionalTestFolders(string $projectRootDir): array
47
    {
48
        /** @var SplFileInfo[] $additionalTestDirectories */
49
        $additionalTestDirectories = (new Finder())
50
            ->in($projectRootDir . '/src')
51
            ->name(['Test', 'Tests'])
52
            ->directories()
53
        ;
54
55
        $folders = [];
56
        foreach ($additionalTestDirectories as $directory) {
57
            $folders[] = new SourceFolder($directory->getRelativePathname(), SourceFolder::TYPE_TEST_SOURCE);
58
        }
59
        return $folders;
60
    }
61
62
    /**
63
     * @param string $projectRootDir
64
     * @param array|SourceFolder[] $folders
65
     * @return array|SourceFolder[]
66
     */
67
    private function mapAndFilterExisting(string $projectRootDir, array $folders): array
68
    {
69
        $map = [];
70
        foreach ($folders as $folder) {
71
            if (file_exists($projectRootDir . '/' . $folder->getPath())) {
72
                $map[$folder->getPath()] = $folder;
73
            }
74
        }
75
76
        return $map;
77
    }
78
}
79