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

SourceFolderHelper   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 68
ccs 0
cts 28
cp 0
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 15 2
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
    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