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

YamlEmptyLineAtEndFixer   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

2 Methods

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