Passed
Pull Request — master (#59)
by Peter
11:55
created

YamlAlphabeticalDataFactory::recursiveKsort()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 10
cc 4
nc 4
nop 3
crap 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace YamlStandards\Model\YamlAlphabetical;
6
7
use YamlStandards\Model\Component\Parser\YamlParser;
8
use YamlStandards\Model\Component\Parser\YamlParserLineData;
9
use YamlStandards\Model\Component\YamlService;
10
11
class YamlAlphabeticalDataFactory
12
{
13
    public const REGEX_KEY_COMMON_LINE_WITH_NUMBER_AT_END = '/' . YamlParserLineData::KEY_COMMON_LINE . '\d+$/';
14
    public const REGEX_KEY_COMMENT_OR_EMPTY_LINE_WITH_NUMBER_AT_END = '/' . YamlParserLineData::KEY_COMMENT_OR_EMPTY_LINE . '\d+$/';
15
    public const REGEX_KEY_DASH_WITH_NUMBER_AT_END = '/' . YamlParserLineData::KEY_DASH . '\d+$/';
16
    public const REGEX_KEY_EMPTY_ARRAY_WITH_NUMBER_AT_END = '/' . YamlParserLineData::KEY_EMPTY_ARRAY . '\d+$/';
17
    public const REGEX_KEY_ARRAY_WITHOUT_KEY_WITH_NUMBER_AT_END = '/^' . YamlParserLineData::KEY_ARRAY_WITHOUT_KEY . '\d+/';
18
    public const REGEX_KEY_CURLY_BRACKETS_WITH_NUMBER_AT_END = '/' . YamlParserLineData::KEY_CURLY_BRACKETS . '\d+$/';
19
20
    public const REGEX_VALUE_EMPTY_LINE = '/' . YamlParserLineData::EMPTY_LINE_DEFAULT_VALUE . '$/';
21
22
    /**
23
     * @var int
24
     */
25
    private static $index;
26
27
    /**
28
     * @param string $pathToYamlFile
29
     * @param int $depth
30
     * @param string[] $prioritizedKeys
31
     * @return string[]
32
     */
33 6
    public static function getCorrectYamlLines(string $pathToYamlFile, int $depth, array $prioritizedKeys): array
34
    {
35 6
        self::$index = 0; // start from 0 in every file
36
37 6
        $yamlArrayData = YamlParser::getYamlParsedDataFromFile($pathToYamlFile);
38 6
        $yamlArrayDataSorted = YamlSortService::sortArray($yamlArrayData, $depth, $prioritizedKeys);
39
40 6
        return self::createRightSortedYamlLines($yamlArrayDataSorted);
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::createRight...s($yamlArrayDataSorted) returns an array which contains values of type array|string[] which are incompatible with the documented value type string.
Loading history...
41
    }
42
43
    /**
44
     * @param string[]|string[][] $sortedYamlData
45
     * @return string[]
46
     */
47 6
    private static function createRightSortedYamlLines(array $sortedYamlData): array
48
    {
49 6
        $rightSortedYamlLines = [];
50
51 6
        foreach ($sortedYamlData as $yamlKey => $yamlValue) {
52 6
            if (is_array($yamlValue) === false && ($yamlValue === YamlParserLineData::EMPTY_LINE_DEFAULT_VALUE || YamlService::isLineComment($yamlValue))) {
0 ignored issues
show
Bug introduced by
$yamlValue of type array is incompatible with the type string expected by parameter $yamlLine of YamlStandards\Model\Comp...ervice::isLineComment(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

52
            if (is_array($yamlValue) === false && ($yamlValue === YamlParserLineData::EMPTY_LINE_DEFAULT_VALUE || YamlService::isLineComment(/** @scrutinizer ignore-type */ $yamlValue))) {
Loading history...
53 6
                $rightSortedYamlLines[] = preg_match(self::REGEX_VALUE_EMPTY_LINE, $yamlValue) === 0 ? $yamlValue : preg_replace(self::REGEX_VALUE_EMPTY_LINE, '', $yamlValue);
0 ignored issues
show
Bug introduced by
$yamlValue of type array is incompatible with the type string expected by parameter $subject of preg_match(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

53
                $rightSortedYamlLines[] = preg_match(self::REGEX_VALUE_EMPTY_LINE, /** @scrutinizer ignore-type */ $yamlValue) === 0 ? $yamlValue : preg_replace(self::REGEX_VALUE_EMPTY_LINE, '', $yamlValue);
Loading history...
54
55 6
                continue;
56
            }
57
58 6
            $yamlKey = is_int($yamlKey) ? (string)$yamlKey : $yamlKey;
59 6
            if (preg_match(self::REGEX_KEY_COMMENT_OR_EMPTY_LINE_WITH_NUMBER_AT_END, $yamlKey) === 0 &&
60 6
                preg_match(self::REGEX_KEY_EMPTY_ARRAY_WITH_NUMBER_AT_END, $yamlKey) === 0
61
            ) {
62 6
                $key = preg_match(self::REGEX_KEY_COMMON_LINE_WITH_NUMBER_AT_END, $yamlKey) === 0 ? $yamlKey : preg_replace(self::REGEX_KEY_COMMON_LINE_WITH_NUMBER_AT_END, '', $yamlKey);
63 6
                $key = preg_match(self::REGEX_KEY_DASH_WITH_NUMBER_AT_END, $key) === 0 ? $key : preg_replace(self::REGEX_KEY_DASH_WITH_NUMBER_AT_END, '-', $key);
64 6
                $key = preg_match(self::REGEX_KEY_CURLY_BRACKETS_WITH_NUMBER_AT_END, $key) === 0 ? $key : preg_replace(self::REGEX_KEY_CURLY_BRACKETS_WITH_NUMBER_AT_END, '', $key);
65 6
                $key = preg_match(self::REGEX_KEY_ARRAY_WITHOUT_KEY_WITH_NUMBER_AT_END, $key) === 0 ? $key : preg_replace(self::REGEX_KEY_ARRAY_WITHOUT_KEY_WITH_NUMBER_AT_END, '', $key);
66 6
                $value = is_array($yamlValue) ? '' : $yamlValue;
67 6
                $rightSortedYamlLines[] = $key . $value;
68
69 6
                self::$index++;
70
            }
71
72 6
            if (is_array($yamlValue)) {
73 6
                $result = self::createRightSortedYamlLines($yamlValue);
74 6
                $rightSortedYamlLines = array_merge($rightSortedYamlLines, $result);
75
            }
76
        }
77
78 6
        return $rightSortedYamlLines;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $rightSortedYamlLines returns an array which contains values of type array|string[] which are incompatible with the documented value type string.
Loading history...
79
    }
80
}
81