Skip to content

Commit 2fe346a

Browse files
committed
Fix PHP 7.4 regression, changed behavior of get_declared_classes()
Since PHP 7.4 get_declared_classes() does not guarantee any order. That implies that parent classes aren't the first any more, rendering the array_reverse() technique futile for the loop & break code that follows. So, additionally, let's try to reduce the list of candidates by removing all the classes known to be "parents". That way, at the end, only the "main" class just included with remain. Upstream PR: squizlabs/PHP_CodeSniffer#3130 Source: https://raw.githubusercontent.com/php/php-src/PHP-7.4/UPGRADING Text: Previously get_declared_classes() always returned parent classes before child classes. This is no longer the case. No particular order is guaranteed for the get_declared_classes() return value.
1 parent ef0c4c4 commit 2fe346a

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

phpcs/autoload.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,22 @@ public static function loadFile($path)
168168

169169
$className = null;
170170
$newClasses = array_reverse(array_diff(get_declared_classes(), $classes));
171+
// Since PHP 7.4 get_declared_classes() does not guarantee any order. That
172+
// implies that parent classes aren't the first any more, rendering the
173+
// array_reverse() technique futile for the loop & break code that follows.
174+
// So, additionally, let's try to reduce the list of candidates by removing all
175+
// the classes known to be "parents". That way, at the end, only the "main"
176+
// class just included with remain.
177+
$newClasses = array_reduce(
178+
$newClasses,
179+
function ($remaining, $current) {
180+
return array_diff($remaining, class_parents($current));
181+
},
182+
$newClasses
183+
);
184+
$newClasses = array_reduce($newClasses, function($remaining, $current) {
185+
return array_diff($remaining, class_parents($current));
186+
}, $newClasses);
171187
foreach ($newClasses as $name) {
172188
if (isset(self::$loadedFiles[$name]) === false) {
173189
$className = $name;

readme_moodle.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Current checkout:
1515

1616
Local modifications (only allowed if there is a PR upstream backing it):
1717

18-
- none right now
18+
- PHP 7.4 fix: https://github.com/squizlabs/PHP_CodeSniffer/pull/3130
1919

2020
===== ===== ===== ===== ===== ===== =====
2121

0 commit comments

Comments
 (0)