Completed
Push — master ( 5c84c5...c7f8f4 )
by Marius
01:51
created

SourceFolderHelper::findAdditionalTestFolders()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 5.667

Importance

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