SourceFolderHelper   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 74.19%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 4
dl 0
loc 73
ccs 23
cts 31
cp 0.7419
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getSourceFolders() 0 17 2
A findAdditionalTestFolders() 0 20 3
A mapAndFilterExisting() 0 11 3
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