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

YamlEmptyLineAtEndChecker   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A check() 0 17 2
A runCheckYamlFileIsValid() 0 2 1
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\AbstractChecker;
10
use YamlStandards\Model\Config\StandardParametersData;
11
use YamlStandards\Result\Result;
12
13
/**
14
 * Check yaml file has empty line at end of file
15
 */
16
class YamlEmptyLineAtEndChecker extends AbstractChecker
17
{
18
    /**
19
     * @inheritDoc
20
     */
21 5
    public function check(string $pathToYamlFile, StandardParametersData $standardParametersData): Result
22
    {
23 5
        $yamlContent = file_get_contents($pathToYamlFile);
24 5
        $yamlContent = str_replace("\r", '', $yamlContent); // remove carriage returns
25 5
        $yamlLines = explode("\n", $yamlContent);
26
27 5
        $correctYamlLines = YamlEmptyLineAtEndDataFactory::getCorrectYamlContent($yamlLines);
28 5
        $correctYamlContent = implode("\n", $correctYamlLines);
29
30 5
        if ($yamlContent === $correctYamlContent) {
31 4
            return new Result($pathToYamlFile, Result::RESULT_CODE_OK, ProcessOutput::STATUS_CODE_OK);
32
        }
33
34 1
        $differ = new Differ();
35 1
        $diffBetweenStrings = $differ->diff($yamlContent, $correctYamlContent);
36
37 1
        return new Result($pathToYamlFile, Result::RESULT_CODE_INVALID_FILE_SYNTAX, ProcessOutput::STATUS_CODE_INVALID_FILE_SYNTAX, $diffBetweenStrings, true);
38
    }
39
40
    /**
41
     * @inheritDoc
42
     */
43 3
    protected function runCheckYamlFileIsValid(string $pathToYamlFile): void
44
    {
45 3
    }
46
}
47