Test Failed
Push — master ( 6bd5d7...53c755 )
by Peter
05:13
created

YamlEmptyLineAtEndFixer   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
eloc 13
c 1
b 0
f 0
dl 0
loc 25
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A fix() 0 20 2
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace YamlStandards\Model\YamlEmptyLineAtEnd;
6
7
use SebastianBergmann\Diff\Differ;
8
use YamlStandards\Command\ProcessOutput;
9
use YamlStandards\Model\Component\YamlService;
10
use YamlStandards\Model\Config\StandardParametersData;
11
use YamlStandards\Model\FixerInterface;
12
use YamlStandards\Result\Result;
13
14
/**
15
 * Fix yaml file has empty line at end of file
16
 */
17
class YamlEmptyLineAtEndFixer implements FixerInterface
18
{
19
    /**
20
     * @inheritDoc
21
     */
22
    public function fix(string $pathToYamlFile, string $pathToDumpFixedFile, StandardParametersData $standardParametersData): Result
23
    {
24
        $yamlContent = file_get_contents($pathToYamlFile);
25
        $yamlContent = str_replace("\r", '', $yamlContent); // remove carriage returns
26
        $yamlLines = explode("\n", $yamlContent);
27
        $filteredYamlLines = array_values(array_filter($yamlLines, [YamlService::class, 'isLineNotBlank']));
28
29
        $filteredYamlLines[] = '';
30
        $correctYamlContent = implode("\n", $filteredYamlLines);
31
32
        if ($yamlContent === $correctYamlContent) {
33
            return new Result($pathToYamlFile, Result::RESULT_CODE_OK, ProcessOutput::STATUS_CODE_OK);
34
        }
35
36
        file_put_contents($pathToDumpFixedFile, $correctYamlContent);
37
38
        $differ = new Differ();
39
        $diffBetweenStrings = $differ->diff($yamlContent, $correctYamlContent);
40
41
        return new Result($pathToYamlFile, Result::RESULT_CODE_OK, ProcessOutput::STATUS_CODE_INVALID_FILE_SYNTAX, $diffBetweenStrings);
42
    }
43
}
44