YamlServiceAliasingChecker::check()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3

Importance

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