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

ConfigurationOptionFinder   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 39.29%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 53
ccs 11
cts 28
cp 0.3929
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A findUsedDockerImage() 0 12 2
B findWebpackConfigPath() 0 30 6
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