YamlSpacesBetweenGroupsChecker::check()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 14
c 0
b 0
f 0
nc 4
nop 2
dl 0
loc 23
ccs 15
cts 15
cp 1
crap 3
rs 9.7998
1
<?php
2
3
declare(strict_types=1);
4
5
namespace YamlStandards\Model\YamlSpacesBetweenGroups;
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 yaml file have space between groups
16
 */
17
class YamlSpacesBetweenGroupsChecker extends AbstractChecker
18
{
19
    /**
20
     * @inheritDoc
21
     */
22 4
    public function check(string $pathToFile, StandardParametersData $standardParametersData): Result
23
    {
24 4
        $yamlContent = file_get_contents($pathToFile);
25 4
        $yamlContent = str_replace("\r", '', $yamlContent); // remove carriage returns
26 4
        $yamlLines = explode("\n", $yamlContent);
27 4
        $lastYamlElement = end($yamlLines);
28 4
        $filteredYamlLines = array_values(array_filter($yamlLines, [YamlService::class, 'isLineNotBlank']));
29 4
        $yamlSpacesBetweenGroupsDataFactory = new YamlSpacesBetweenGroupsDataFactory();
30
31 4
        $correctYamlContent = $yamlSpacesBetweenGroupsDataFactory->getCorrectYamlContentWithSpacesBetweenGroups($filteredYamlLines, $standardParametersData->getLevel());
32
33 4
        if (YamlService::isLineBlank($lastYamlElement)) {
34 4
            $correctYamlContent .= "\n";
35
        }
36
37 4
        if ($yamlContent === $correctYamlContent) {
38 3
            return new Result($pathToFile, Result::RESULT_CODE_OK);
39
        }
40
41 1
        $differ = new Differ(new UnifiedDiffOutputBuilder());
42 1
        $diffBetweenStrings = $differ->diff($yamlContent, $correctYamlContent);
43
44 1
        return new Result($pathToFile, Result::RESULT_CODE_INVALID_FILE_SYNTAX, $diffBetweenStrings, true);
45
    }
46
}
47