YamlServiceAliasingFixer   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 14
c 1
b 0
f 0
dl 0
loc 28
ccs 14
cts 14
cp 1
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 SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder;
9
use YamlStandards\Model\AbstractFixer;
10
use YamlStandards\Model\Component\YamlService;
11
use YamlStandards\Model\Config\StandardParametersData;
12
use YamlStandards\Result\Result;
13
14
/**
15
 * Fix service file has uniform aliasing
16
 */
17
class YamlServiceAliasingFixer extends AbstractFixer
18
{
19
    /**
20
     * @inheritDoc
21
     */
22 3
    public function fix(string $pathToFile, string $pathToDumpFixedFile, StandardParametersData $standardParametersData): Result
23
    {
24 3
        $yamlContent = file_get_contents($pathToFile);
25 3
        $yamlContent = str_replace("\r", '', $yamlContent); // remove carriage returns
26 3
        $yamlLines = explode("\n", $yamlContent);
27
28 3
        if (YamlServiceAliasingDataFactory::existsServicesInHighestParent($yamlLines) === false) {
29 1
            return new Result($pathToFile, Result::RESULT_CODE_OK);
30
        }
31
32 3
        $correctYamlLines = YamlServiceAliasingDataFactory::getCorrectYamlLines($yamlLines, YamlService::getYamlData($pathToFile), $standardParametersData);
33 3
        $correctYamlContent = implode("\n", $correctYamlLines);
34
35 3
        if ($yamlContent === $correctYamlContent) {
36 1
            return new Result($pathToFile, Result::RESULT_CODE_OK);
37
        }
38
39 2
        file_put_contents($pathToDumpFixedFile, $correctYamlContent);
40
41 2
        $differ = new Differ(new UnifiedDiffOutputBuilder());
42 2
        $diffBetweenStrings = $differ->diff($yamlContent, $correctYamlContent);
43
44 2
        return new Result($pathToFile, Result::RESULT_CODE_FIXED_INVALID_FILE_SYNTAX, $diffBetweenStrings);
45
    }
46
}
47