SourceFolderHelper::getSourceFolders()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 9
cts 9
cp 1
rs 9.7
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
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 6
    public function __construct(
17
        ConfigurationOptionFinder $configurationOptionFinder,
18
        array $defaultSourceFolders
19
    ) {
20 6
        $this->configurationOptionFinder = $configurationOptionFinder;
21 6
        $this->defaultSourceFolders = $defaultSourceFolders;
22 6
    }
23
24 6
    public function getSourceFolders(string $projectRootDir): array
25
    {
26 6
        $folders = array_merge(
27 6
            $this->defaultSourceFolders,
28 6
            $this->findAdditionalTestFolders($projectRootDir),
29 6
            $this->configurationOptionFinder->findConfiguredSourceFolders($projectRootDir)
30
        );
31
32 6
        $folders = $this->mapAndFilterExisting($projectRootDir, $folders);
33
34
        // as it might not have been created yet, exclude `vendor` always
35 6
        if (!isset($folders['vendor'])) {
36 6
            $folders[] = new SourceFolder('vendor', SourceFolder::TYPE_EXCLUDED);
37
        }
38
39 6
        return array_values($folders);
40
    }
41
42
    /**
43
     * @param string $projectRootDir
44
     * @return array|SourceFolder[]
45
     */
46 6
    private function findAdditionalTestFolders(string $projectRootDir): array
47
    {
48 6
        $srcDir = $projectRootDir . '/src';
49 6
        if (!is_dir($srcDir)) {
50 6
            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 6
    private function mapAndFilterExisting(string $projectRootDir, array $folders): array
73
    {
74 6
        $map = [];
75 6
        foreach ($folders as $folder) {
76 6
            if (file_exists($projectRootDir . '/' . $folder->getPath())) {
77 6
                $map[$folder->getPath()] = $folder;
78
            }
79
        }
80
81 6
        return $map;
82
    }
83
}
84