Passed
Pull Request — master (#74)
by Peter
08:25 queued 01:31
created

YamlServiceAliasingFixer::fix()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3

Importance

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