Passed
Pull Request — master (#74)
by Peter
06:51
created

YamlSpacesBetweenGroupsFixer   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 16
c 1
b 0
f 0
dl 0
loc 30
ccs 16
cts 16
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A fix() 0 25 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace YamlStandards\Model\YamlSpacesBetweenGroups;
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 yaml file have space between groups
15
 */
16
class YamlSpacesBetweenGroupsFixer extends AbstractFixer
17
{
18
    /**
19
     * @inheritDoc
20
     */
21 2
    public function fix(string $pathToFile, string $pathToDumpFixedFile, StandardParametersData $standardParametersData): Result
22
    {
23 2
        $yamlContent = file_get_contents($pathToFile);
24 2
        $yamlContent = str_replace("\r", '', $yamlContent); // remove carriage returns
25 2
        $yamlLines = explode("\n", $yamlContent);
26 2
        $lastYamlElement = end($yamlLines);
27 2
        $filteredYamlLines = array_values(array_filter($yamlLines, [YamlService::class, 'isLineNotBlank']));
28 2
        $yamlSpacesBetweenGroupsDataFactory = new YamlSpacesBetweenGroupsDataFactory();
29
30 2
        $correctYamlContent = $yamlSpacesBetweenGroupsDataFactory->getCorrectYamlContentWithSpacesBetweenGroups($filteredYamlLines, $standardParametersData->getLevel());
31
32 2
        if (YamlService::isLineBlank($lastYamlElement)) {
33 2
            $correctYamlContent .= "\n";
34
        }
35
36 2
        if ($yamlContent === $correctYamlContent) {
37 1
            return new Result($pathToFile, Result::RESULT_CODE_OK);
38
        }
39
40 1
        file_put_contents($pathToDumpFixedFile, $correctYamlContent);
41
42 1
        $differ = new Differ();
43 1
        $diffBetweenStrings = $differ->diff($yamlContent, $correctYamlContent);
44
45 1
        return new Result($pathToFile, Result::RESULT_CODE_FIXED_INVALID_FILE_SYNTAX, $diffBetweenStrings);
46
    }
47
}
48