Passed
Branch master (e09f8d)
by Peter
26:53
created

YamlEmptyLineAtEndFixer::runCheckYamlFileIsValid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 0
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 2
ccs 0
cts 1
cp 0
crap 2
rs 10
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\AbstractFixer;
10
use YamlStandards\Model\Config\StandardParametersData;
11
use YamlStandards\Result\Result;
12
13
/**
14
 * Fix yaml file has empty line at end of file
15
 */
16
class YamlEmptyLineAtEndFixer extends AbstractFixer
17
{
18
    /**
19
     * @inheritDoc
20
     */
21 1
    public function fix(string $pathToYamlFile, string $pathToDumpFixedFile, StandardParametersData $standardParametersData): Result
22
    {
23 1
        $yamlContent = file_get_contents($pathToYamlFile);
24 1
        $yamlContent = str_replace("\r", '', $yamlContent); // remove carriage returns
25 1
        $yamlLines = explode("\n", $yamlContent);
26
27 1
        $correctYamlLines = YamlEmptyLineAtEndDataFactory::getCorrectYamlContent($yamlLines);
28 1
        $correctYamlContent = implode("\n", $correctYamlLines);
29
30 1
        if ($yamlContent === $correctYamlContent) {
31
            return new Result($pathToYamlFile, Result::RESULT_CODE_OK, ProcessOutput::STATUS_CODE_OK);
32
        }
33
34 1
        file_put_contents($pathToDumpFixedFile, $correctYamlContent);
35
36 1
        $differ = new Differ();
37 1
        $diffBetweenStrings = $differ->diff($yamlContent, $correctYamlContent);
38
39 1
        return new Result($pathToYamlFile, Result::RESULT_CODE_OK, ProcessOutput::STATUS_CODE_INVALID_FILE_SYNTAX, $diffBetweenStrings);
40
    }
41
42
    /**
43
     * @inheritDoc
44
     */
45
    protected function runCheckYamlFileIsValid(string $pathToYamlFile): void
46
    {
47
    }
48
}
49