Passed
Push — master ( 99a92e...b669f8 )
by Marius
04:00
created

ConfigurationOptionFinder::findWebpackConfigPath()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 22.9396

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 4
cts 18
cp 0.2222
rs 8.8177
c 0
b 0
f 0
cc 6
nc 5
nop 1
crap 22.9396
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Paysera\PhpStormHelper\Service;
6
7
use DOMElement;
8
9
class ConfigurationOptionFinder
10
{
11
    private $domHelper;
12
13 5
    public function __construct(DomHelper $domHelper)
14
    {
15 5
        $this->domHelper = $domHelper;
16 5
    }
17
18 4
    public function findUsedDockerImage(string $projectRootDir)
19
    {
20 4
        $phpXmlFilePath = $projectRootDir . '/.idea/php.xml';
21 4
        if (!file_exists($phpXmlFilePath)) {
22 4
            return null;
23
        }
24
25
        $contents = file_get_contents($phpXmlFilePath);
26
27
        preg_match('/DOCKER_IMAGE_NAME="([^"]+)"/', $contents, $matches);
28
        return $matches[1] ?? null;
29
    }
30
31 4
    public function findWebpackConfigPath(string $projectRootDir)
32
    {
33 4
        $phpXmlFilePath = $projectRootDir . '/.idea/misc.xml';
34 4
        if (!file_exists($phpXmlFilePath)) {
35 4
            return null;
36
        }
37
38
        $document = $this->domHelper->loadDocument($phpXmlFilePath);
39
        $projectElement = $this->domHelper->findNode($document, 'project');
40
41
        $component = $this->domHelper->findOptionalNode($projectElement, 'component', [
42
            'name' => 'WebPackConfiguration',
43
        ]);
44
        if ($component === null) {
45
            return null;
46
        }
47
48
        $option = $this->domHelper->findOptionalNode($component, 'option', ['name' => 'path']);
49
        if ($option === null || !$option instanceof DOMElement) {
50
            return null;
51
        }
52
53
        $value = $option->getAttribute('value');
54
        $prefix = '$PROJECT_DIR$/';
55
        if (strpos($value, $prefix) !== 0) {
56
            return null;
57
        }
58
59
        return mb_substr($value, mb_strlen($prefix));
60
    }
61
}
62