YamlIndentChecker   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 27
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A check() 0 22 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace YamlStandards\Model\YamlIndent;
6
7
use SebastianBergmann\Diff\Differ;
8
use SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder;
9
use YamlStandards\Model\AbstractChecker;
10
use YamlStandards\Model\Config\StandardParametersData;
11
use YamlStandards\Result\Result;
12
13
/**
14
 * Check yaml file complies right count of indent
15
 */
16
class YamlIndentChecker extends AbstractChecker
17
{
18
    /**
19
     * @inheritDoc
20
     */
21 7
    public function check(string $pathToFile, StandardParametersData $standardParametersData): Result
22
    {
23 7
        $fileContent = file_get_contents($pathToFile);
24 7
        $fileContent = str_replace("\r", '', $fileContent); // remove carriage returns
25 7
        $fileLines = explode("\n", $fileContent);
26 7
        $yamlIndentDataFactory = new YamlIndentDataFactory();
27 7
        $rightFileLines = [];
28
29 7
        foreach ($fileLines as $key => $fileLine) {
30 7
            $rightFileLines[] = $yamlIndentDataFactory->getRightFileLines($fileLines, $key, $standardParametersData, $fileLine);
31
        }
32
33 7
        $rightFileContent = implode("\n", $rightFileLines);
34
35 7
        if ($fileContent === $rightFileContent) {
36 5
            return new Result($pathToFile, Result::RESULT_CODE_OK);
37
        }
38
39 2
        $differ = new Differ(new UnifiedDiffOutputBuilder());
40 2
        $diffBetweenStrings = $differ->diff($fileContent, $rightFileContent);
41
42 2
        return new Result($pathToFile, Result::RESULT_CODE_INVALID_FILE_SYNTAX, $diffBetweenStrings, true);
43
    }
44
}
45