Completed
Push — master ( b209b1...84fcc2 )
by Peter
05:42
created

YamlServiceAliasingFixer   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Test Coverage

Coverage 85.71%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 14
c 1
b 0
f 0
dl 0
loc 28
ccs 12
cts 14
cp 0.8571
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A fix() 0 23 3
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace YamlStandards\Model\YamlServiceAliasing;
6
7
use SebastianBergmann\Diff\Differ;
8
use YamlStandards\Command\ProcessOutput;
9
use YamlStandards\Model\Component\YamlService;
10
use YamlStandards\Model\Config\StandardParametersData;
11
use YamlStandards\Model\FixerInterface;
12
use YamlStandards\Result\Result;
13
14
/**
15
 * Fix service file has uniform aliasing
16
 */
17
class YamlServiceAliasingFixer implements FixerInterface
18
{
19
    /**
20
     * @inheritDoc
21
     */
22 2
    public function fix(string $pathToYamlFile, string $pathToDumpFixedFile, StandardParametersData $standardParametersData): Result
23
    {
24 2
        $yamlContent = file_get_contents($pathToYamlFile);
25 2
        $yamlContent = str_replace("\r", '', $yamlContent); // remove carriage returns
26 2
        $yamlLines = explode("\n", $yamlContent);
27
28 2
        if (YamlServiceAliasingDataFactory::existsServicesInHighestParent($yamlLines) === false) {
29
            return new Result($pathToYamlFile, Result::RESULT_CODE_OK, ProcessOutput::STATUS_CODE_OK);
30
        }
31
32 2
        $correctYamlLines = YamlServiceAliasingDataFactory::getCorrectYamlLines($yamlLines, YamlService::getYamlData($pathToYamlFile), $standardParametersData);
33 2
        $correctYamlContent = implode("\n", $correctYamlLines);
34
35 2
        if ($yamlContent === $correctYamlContent) {
36
            return new Result($pathToYamlFile, Result::RESULT_CODE_OK, ProcessOutput::STATUS_CODE_OK);
37
        }
38
39 2
        file_put_contents($pathToDumpFixedFile, $correctYamlContent);
40
41 2
        $differ = new Differ();
42 2
        $diffBetweenStrings = $differ->diff($yamlContent, $correctYamlContent);
43
44 2
        return new Result($pathToYamlFile, Result::RESULT_CODE_OK, ProcessOutput::STATUS_CODE_INVALID_FILE_SYNTAX, $diffBetweenStrings);
45
    }
46
}
47