|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace YamlStandards\Model\YamlSpacesBetweenGroups; |
|
6
|
|
|
|
|
7
|
|
|
use SebastianBergmann\Diff\Differ; |
|
8
|
|
|
use SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder; |
|
9
|
|
|
use YamlStandards\Model\AbstractChecker; |
|
10
|
|
|
use YamlStandards\Model\Component\YamlService; |
|
11
|
|
|
use YamlStandards\Model\Config\StandardParametersData; |
|
12
|
|
|
use YamlStandards\Result\Result; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Check yaml file have space between groups |
|
16
|
|
|
*/ |
|
17
|
|
|
class YamlSpacesBetweenGroupsChecker extends AbstractChecker |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @inheritDoc |
|
21
|
|
|
*/ |
|
22
|
4 |
|
public function check(string $pathToFile, StandardParametersData $standardParametersData): Result |
|
23
|
|
|
{ |
|
24
|
4 |
|
$yamlContent = file_get_contents($pathToFile); |
|
25
|
4 |
|
$yamlContent = str_replace("\r", '', $yamlContent); // remove carriage returns |
|
26
|
4 |
|
$yamlLines = explode("\n", $yamlContent); |
|
27
|
4 |
|
$lastYamlElement = end($yamlLines); |
|
28
|
4 |
|
$filteredYamlLines = array_values(array_filter($yamlLines, [YamlService::class, 'isLineNotBlank'])); |
|
29
|
4 |
|
$yamlSpacesBetweenGroupsDataFactory = new YamlSpacesBetweenGroupsDataFactory(); |
|
30
|
|
|
|
|
31
|
4 |
|
$correctYamlContent = $yamlSpacesBetweenGroupsDataFactory->getCorrectYamlContentWithSpacesBetweenGroups($filteredYamlLines, $standardParametersData->getLevel()); |
|
32
|
|
|
|
|
33
|
4 |
|
if (YamlService::isLineBlank($lastYamlElement)) { |
|
34
|
4 |
|
$correctYamlContent .= "\n"; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
4 |
|
if ($yamlContent === $correctYamlContent) { |
|
38
|
3 |
|
return new Result($pathToFile, Result::RESULT_CODE_OK); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
1 |
|
$differ = new Differ(new UnifiedDiffOutputBuilder()); |
|
42
|
1 |
|
$diffBetweenStrings = $differ->diff($yamlContent, $correctYamlContent); |
|
43
|
|
|
|
|
44
|
1 |
|
return new Result($pathToFile, Result::RESULT_CODE_INVALID_FILE_SYNTAX, $diffBetweenStrings, true); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|