From b3b00009ea042954fa1afd915fae5925b5f34b53 Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Thu, 2 May 2024 12:51:58 +0100 Subject: [PATCH] Less_Tree: Minor rewrite of internal compare() logic Follows-up I657e7a950a (6d652ed90c) for T357160, where I synced this with upstream, but I kept it more minimal, by inverting the logic and thus doing the instanceof checks only once. In change If7d0ea1940, we find that this might be behaving different than upstream. To rule it out, let's just do it the same way and accept the more verbose code. Change-Id: I86221f2833a4ef6c8cbba322bc744092ba2106b7 --- lib/Less/Tree.php | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/lib/Less/Tree.php b/lib/Less/Tree.php index 12bf54df..7b16a2dc 100644 --- a/lib/Less/Tree.php +++ b/lib/Less/Tree.php @@ -75,19 +75,27 @@ public function accept( $visitor ) { */ public static function nodeCompare( $a, $b ) { // Less_Tree subclasses that implement compare() are: - // Anonymous, Color, Dimension, Keyword, Quoted, Unit - if ( $b instanceof Less_Tree_Quoted || $b instanceof Less_Tree_Anonymous ) { + // Anonymous, Color, Dimension, Quoted, Unit + $aHasCompare = ( $a instanceof Less_Tree_Anonymous || $a instanceof Less_Tree_Color + || $a instanceof Less_Tree_Dimension || $a instanceof Less_Tree_Quoted || $a instanceof Less_Tree_Unit + ); + $bHasCompare = ( $b instanceof Less_Tree_Anonymous || $b instanceof Less_Tree_Color + || $b instanceof Less_Tree_Dimension || $b instanceof Less_Tree_Quoted || $b instanceof Less_Tree_Unit + ); + + if ( $aHasCompare && + !( $b instanceof Less_Tree_Quoted || $b instanceof Less_Tree_Anonymous ) + ) { // for "symmetric results" force toCSS-based comparison via b.compare() // of Quoted or Anonymous if either value is one of those + // @phan-suppress-next-line PhanUndeclaredMethod + return $a->compare( $b ); + } elseif ( $bHasCompare ) { + $res = $b->compare( $a ); // In JS, `-undefined` produces NAN, which, just like undefined // will enter the the default/false branch of Less_Tree_Condition#compile. // In PHP, `-null` is 0. To ensure parity, preserve the null. - $res = $b->compare( $a ); return $res !== null ? -$res : null; - } elseif ( $a instanceof Less_Tree_Anonymous || $a instanceof Less_Tree_Color - || $a instanceof Less_Tree_Dimension || $a instanceof Less_Tree_Quoted || $a instanceof Less_Tree_Unit - ) { - return $a->compare( $b ); } elseif ( get_class( $a ) !== get_class( $b ) ) { return null; } @@ -95,11 +103,12 @@ public static function nodeCompare( $a, $b ) { // Less_Tree subclasses that have an array value: Less_Tree_Expression, Less_Tree_Value // @phan-suppress-next-line PhanUndeclaredProperty $aval = $a->value ?? []; - // @phan-suppress-next-line PhanUndeclaredProperty $bval = $b->value ?? []; if ( !( $a instanceof Less_Tree_Expression || $a instanceof Less_Tree_Value ) ) { return $aval === $bval ? 0 : null; } + '@phan-var Less_Tree[] $aval'; + '@phan-var Less_Tree[] $bval'; if ( count( $aval ) !== count( $bval ) ) { return null; }