Skip to content

Commit a359936

Browse files
del72683Krinkle
authored andcommitted
Fix "Using null as the key parameter for array_key_exists" PHP 8.5 warning
Our implementation diverged from upstream Less.js 3.13.1 by using an associative array instead of a list array. Change this so that null values naturally work, without creating an amguity between null and the empty string (since only strings can be valid array keys). This change triggered a Phan warning, because Phan creates a simplified analysis of the parseMixinArgs()['args'] array that mixes up the types of the 'name' and 'value' fields, because it ignored the keys. ``` lib/Less/Tree/Mixin/Definition.php:45 PhanTypeMismatchProperty Assigning ($p['name'] as a field) of type non-empty-array<int,?\Less_Tree|?string> to property but \Less_Tree_Mixin_Definition->optionalParameters is string[] ``` Fix this by declaring the array shape. Bug: T410596 Change-Id: Ic0b5274febea3d2318282619b5f762d11d2530f9
1 parent 3a8e5ed commit a359936

2 files changed

Lines changed: 8 additions & 4 deletions

File tree

lib/Less/Parser.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1853,6 +1853,7 @@ private function parseMixinCallElements() {
18531853

18541854
/**
18551855
* @param bool $isCall
1856+
* @return array{args:array<array{name?:string,value?:mixed,variadic?:bool}>,variadic:bool}
18561857
* @see less-2.5.3.js#parsers.mixin.args
18571858
*/
18581859
private function parseMixinArgs( $isCall ) {

lib/Less/Tree/Mixin/Definition.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Less_Tree_Mixin_Definition extends Less_Tree_Ruleset {
2323
public $condition;
2424
/** @var bool */
2525
public $variadic;
26-
/** @var array<string,true> */
26+
/** @var string[] */
2727
public $optionalParameters = [];
2828

2929
public function __construct( $name, $params, $rules, $condition, $variadic = false, $frames = [] ) {
@@ -38,10 +38,11 @@ public function __construct( $name, $params, $rules, $condition, $variadic = fal
3838
if ( $params ) {
3939
$this->arity = count( $params );
4040
foreach ( $params as $p ) {
41-
if ( !isset( $p['name'] ) || ( $p['name'] && !isset( $p['value'] ) ) ) {
41+
// NOTE: Less.js 3.13.1 does a !p.name check in the second half that we omit, because it is impossible.
42+
if ( !isset( $p['name'] ) || !isset( $p['value'] ) ) {
4243
$this->required++;
4344
} else {
44-
$this->optionalParameters[ (string)$p['name'] ] = true;
45+
$this->optionalParameters[] = $p['name'];
4546
}
4647
}
4748
}
@@ -239,7 +240,9 @@ public function matchArgs( $args, $env = null ) {
239240
$allArgsCnt = count( $args );
240241
$requiredArgsCnt = 0;
241242
foreach ( $args as $arg ) {
242-
if ( !array_key_exists( $arg['name'], $this->optionalParameters ) ) {
243+
// NOTE: A positional mixin arg will have a name of null in Less_Tree_Mixin_Call::compile,
244+
// which is never in the optionalParameters array.
245+
if ( !in_array( $arg['name'], $this->optionalParameters, true ) ) {
243246
$requiredArgsCnt++;
244247
}
245248
}

0 commit comments

Comments
 (0)