Skip to content

Commit bdada4a

Browse files
committed
Try fixing PHP5 comp.
1 parent b0e2f74 commit bdada4a

File tree

3 files changed

+72
-8
lines changed

3 files changed

+72
-8
lines changed

.travis.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,9 @@ matrix:
2121
before_script:
2222
- composer install --prefer-dist --no-interaction
2323

24-
- if [[ $CHECKS != 1 ]]; then composer require --dev phpunit/phpunit:"^5.0|^6.0"; fi
25-
2624
script:
2725
- if [[ $CHECKS == 1 ]]; then composer phpstan-setup && composer phpstan ; fi
28-
- if [[ $CHECKS == 1 ]]; then composer cs-check ; fi
26+
- composer cs-check
2927

3028
notifications:
3129
email: false

PSR2R/Sniffs/Commenting/DocBlockTypeOrderSniff.php

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ class DocBlockTypeOrderSniff extends AbstractSniff {
3434
'false',
3535
];
3636

37+
/**
38+
* @var string[]
39+
*/
40+
protected $sortOrder;
41+
3742
/**
3843
* @inheritDoc
3944
*/
@@ -88,7 +93,7 @@ protected function assertOrder(File $phpCsFile, array $docBlockParams) {
8893
}
8994
$expectedTypes = implode('|', $expectedOrder);
9095

91-
$fix = $phpCsFile->addFixableError('Nullish/falsely value should be the last element, expected `' . $expectedTypes . '`', $docBlockParam['index'], 'WrongOrder');
96+
$fix = $phpCsFile->addFixableError('Nullish/falsely value in `' . $docBlockParam['type'] . '` should be the last element, expected `' . $expectedTypes . '`', $docBlockParam['index'], 'WrongOrder');
9297
if (!$fix) {
9398
continue;
9499
}
@@ -110,29 +115,90 @@ protected function assertOrder(File $phpCsFile, array $docBlockParams) {
110115
* @return string[]
111116
*/
112117
protected function getExpectedOrder(array $elements) {
113-
global $sortOrder;
118+
if (version_compare(PHP_VERSION, '7.0') < 0) {
119+
return $this->getExpectedOrderLegacy($elements);
120+
}
114121

115122
$sortOrder = array_reverse($this->sortMap);
123+
$this->sortOrder = $sortOrder;
124+
116125
usort($elements, [$this, 'compare']);
117126

118127
return $elements;
119128
}
120129

130+
/**
131+
* For PHP 5 we need a custom fallback sort.
132+
*
133+
* @param string[] $elements
134+
*
135+
* @return string[]
136+
*/
137+
protected function getExpectedOrderLegacy(array $elements) {
138+
$sortOrder = array_reverse($this->sortMap);
139+
$sortOrder = array_flip($sortOrder);
140+
foreach ($elements as $element) {
141+
if (!isset($sortOrder[$element])) {
142+
$sortOrder[$element] = -1;
143+
}
144+
}
145+
146+
$array = [];
147+
foreach ($elements as $element) {
148+
$array[$element] = $sortOrder[$element];
149+
}
150+
151+
static::asort($array);
152+
153+
$elements = array_keys($array);
154+
155+
return $elements;
156+
}
157+
158+
/**
159+
* asort() but with a/b of same value to keep existing order.
160+
*
161+
* Required for PHP5, as the order can be inverse here.
162+
*
163+
* @param array $array
164+
* @param int $sortFlags
165+
*
166+
* @return bool
167+
*/
168+
protected static function asort(array &$array, $sortFlags = SORT_REGULAR) {
169+
$index = 0;
170+
foreach ($array as &$item) {
171+
$item = [$index++, $item];
172+
}
173+
$result = uasort($array, function($a, $b) use($sortFlags) {
174+
if ($a[1] == $b[1]) {
175+
return $a[0] - $b[0];
176+
}
177+
$set = [-1 => $a[1], 1 => $b[1]];
178+
asort($set, $sortFlags);
179+
reset($set);
180+
return key($set);
181+
});
182+
foreach ($array as &$item) {
183+
$item = $item[1];
184+
}
185+
return $result;
186+
}
187+
121188
/**
122189
* @param string $a
123190
* @param string $b
124191
*
125192
* @return int
126193
*/
127194
protected function compare($a, $b) {
128-
global $sortOrder;
195+
$sortOrder = $this->sortOrder;
129196

130197
$aIndex = array_search($a, $sortOrder, true);
131198
$bIndex = array_search($b, $sortOrder, true);
132199
if ($aIndex === false) {
133200
return -1;
134201
}
135-
136202
if ($bIndex === false) {
137203
return 1;
138204
}

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"docs": "php docs/generate.php",
3737
"docs-listing": "phpcs -e --standard=PSR2R/ruleset.xml",
3838
"cs-check": "phpcs --standard=PSR2R/ruleset.xml -p -s --ignore=/tests/files/,*.inc,*.fixed PSR2R/",
39-
"cs-fix": "phpcbf --standard=PSR2R/ruleset.xml --ignore=/tests/files/,*.inc,*.fixed PSR2R/",
39+
"cs-fix": "phpcbf --standard=PSR2R/ruleset.xml -p --ignore=/tests/files/,*.inc,*.fixed PSR2R/",
4040
"test": "php phpunit.phar",
4141
"test-setup": "[ ! -f phpunit.phar ] && wget https://phar.phpunit.de/phpunit-6.5.13.phar && mv phpunit-6.5.13.phar phpunit.phar || true",
4242
"test-setup-mac": "[ ! -f phpunit.phar ] && curl -OL https://phar.phpunit.de/phpunit-6.5.13.phar && mv phpunit-6.5.13.phar phpunit.phar || true",

0 commit comments

Comments
 (0)