Skip to content

Commit

Permalink
Start using interface instead of property_exists
Browse files Browse the repository at this point in the history
Less parser uses property_exists to distguish one
nodes from other. PHP is OOP langauge and we
could utilize interfaces to improve code readability
and hopefully improve the performance a bit as
calls to property_exists() are pretty slow.

Bug: T327082
Change-Id: I76b6d2b3cb28bfcbe3f51b0fbc7c34a45da609c3
  • Loading branch information
polishdeveloper committed Aug 18, 2023
1 parent ef389f9 commit 853a9db
Show file tree
Hide file tree
Showing 24 changed files with 35 additions and 35 deletions.
14 changes: 6 additions & 8 deletions lib/Less/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -697,9 +697,7 @@ private function _minmax( $isMin, $args ) {
for ( $i = 0; $i < $arg_count; $i++ ) {
$current = $args[$i];
if ( !( $current instanceof Less_Tree_Dimension ) ) {
// @phan-suppress-next-line PhanUndeclaredProperty Checked Less_Tree->value
if ( property_exists( $args[$i], 'value' ) && is_array( $args[$i]->value ) ) {
// @phan-suppress-next-line PhanUndeclaredProperty Checked Less_Tree->value
if ( $args[$i] instanceof Less_Tree_HasValueProperty && is_array( $args[$i]->value ) ) {
$args[] = $args[$i]->value;
}
continue;
Expand Down Expand Up @@ -842,8 +840,7 @@ public function isem( $n ) {
* @param Less_Tree|string $unit
*/
public function isunit( $n, $unit ) {
if ( is_object( $unit ) && property_exists( $unit, 'value' ) ) {
// @phan-suppress-next-line PhanUndeclaredProperty Checked Less_Tree->value
if ( $unit instanceof Less_Tree_HasValueProperty ) {
$unit = $unit->value;
}

Expand All @@ -870,7 +867,7 @@ public function extract( $values, $index ) {
$index = (int)$index->value - 1; // (1-based index)
// handle non-array values as an array of length 1
// return 'undefined' if index is invalid
if ( property_exists( $values, 'value' ) && is_array( $values->value ) ) {
if ( $values instanceof Less_Tree_HasValueProperty && is_array( $values->value ) ) {
if ( isset( $values->value[$index] ) ) {
return $values->value[$index];
}
Expand All @@ -884,7 +881,8 @@ public function extract( $values, $index ) {
}

public function length( $values ) {
$n = ( property_exists( $values, 'value' ) && is_array( $values->value ) ) ? count( $values->value ) : 1;
$n = ( $values instanceof Less_Tree_HasValueProperty && is_array( $values->value ) ) ?
count( $values->value ) : 1;
return new Less_Tree_Dimension( $n );
}

Expand Down Expand Up @@ -1005,7 +1003,7 @@ public function svggradient( $direction ) {
'<' . $gradientType . 'Gradient id="gradient" gradientUnits="userSpaceOnUse" ' . $gradientDirectionSvg . '>';

for ( $i = 0; $i < count( $stops ); $i++ ) {
if ( is_object( $stops[$i] ) && property_exists( $stops[$i], 'value' ) ) {
if ( $stops[$i] instanceof Less_Tree_HasValueProperty ) {
$color = $stops[$i]->value[0];
$position = $stops[$i]->value[1];
} else {
Expand Down
2 changes: 1 addition & 1 deletion lib/Less/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -1508,7 +1508,7 @@ private function parseMixinArgs( $isCall ) {

if ( $isCall ) {
// Variable
if ( property_exists( $arg, 'value' ) && count( $arg->value ) == 1 ) {
if ( $value instanceof Less_Tree_HasValueProperty && count( $arg->value ) == 1 ) {
$val = $arg->value[0];
}
} else {
Expand Down
6 changes: 0 additions & 6 deletions lib/Less/Tree.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@

/**
* Tree
*
* TODO: Callers often use `property_exists(, 'value')` to distinguish
* tree nodes that are considerd value-holding. Refactor this to move
* the 'value' property that most subclasses implement to there, and use
* something else (special value, method, or intermediate class?) to
* signal whether a subclass is considered value-holding.
*/
class Less_Tree {

Expand Down
2 changes: 1 addition & 1 deletion lib/Less/Tree/Alpha.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* @private
*/
class Less_Tree_Alpha extends Less_Tree {
class Less_Tree_Alpha extends Less_Tree implements Less_Tree_HasValueProperty {
public $value;

public function __construct( $val ) {
Expand Down
2 changes: 1 addition & 1 deletion lib/Less/Tree/Anonymous.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* @private
*/
class Less_Tree_Anonymous extends Less_Tree {
class Less_Tree_Anonymous extends Less_Tree implements Less_Tree_HasValueProperty {
public $value;
public $quote;
public $index;
Expand Down
2 changes: 1 addition & 1 deletion lib/Less/Tree/Assignment.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* @private
*/
class Less_Tree_Assignment extends Less_Tree {
class Less_Tree_Assignment extends Less_Tree implements Less_Tree_HasValueProperty {

public $key;
public $value;
Expand Down
2 changes: 1 addition & 1 deletion lib/Less/Tree/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* @private
*/
class Less_Tree_Attribute extends Less_Tree {
class Less_Tree_Attribute extends Less_Tree implements Less_Tree_HasValueProperty {

public $key;
public $op;
Expand Down
2 changes: 1 addition & 1 deletion lib/Less/Tree/Call.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @private
* @see less.tree.Call in less.js 3.0.0 https://github.com/less/less.js/blob/v3.0.0/dist/less.js#L6336
*/
class Less_Tree_Call extends Less_Tree {
class Less_Tree_Call extends Less_Tree implements Less_Tree_HasValueProperty {
public $value;

public $name;
Expand Down
2 changes: 1 addition & 1 deletion lib/Less/Tree/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* @private
*/
class Less_Tree_Comment extends Less_Tree {
class Less_Tree_Comment extends Less_Tree implements Less_Tree_HasValueProperty {

public $value;
public $silent;
Expand Down
2 changes: 1 addition & 1 deletion lib/Less/Tree/Dimension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* @private
*/
class Less_Tree_Dimension extends Less_Tree {
class Less_Tree_Dimension extends Less_Tree implements Less_Tree_HasValueProperty {

public $value;
public $unit;
Expand Down
2 changes: 1 addition & 1 deletion lib/Less/Tree/Directive.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* @private
*/
class Less_Tree_Directive extends Less_Tree {
class Less_Tree_Directive extends Less_Tree implements Less_Tree_HasValueProperty {

public $name;
public $value;
Expand Down
2 changes: 1 addition & 1 deletion lib/Less/Tree/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* @private
*/
class Less_Tree_Element extends Less_Tree {
class Less_Tree_Element extends Less_Tree implements Less_Tree_HasValueProperty {

/** @var string */
public $combinator;
Expand Down
2 changes: 1 addition & 1 deletion lib/Less/Tree/Expression.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* @private
*/
class Less_Tree_Expression extends Less_Tree {
class Less_Tree_Expression extends Less_Tree implements Less_Tree_HasValueProperty {
/** @var array */
public $value = [];
public $parens = false;
Expand Down
8 changes: 8 additions & 0 deletions lib/Less/Tree/HasValueProperty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
/**
* @private
* @property mixed $value
*/
interface Less_Tree_HasValueProperty {

}
2 changes: 1 addition & 1 deletion lib/Less/Tree/Keyword.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* @private
*/
class Less_Tree_Keyword extends Less_Tree {
class Less_Tree_Keyword extends Less_Tree implements Less_Tree_HasValueProperty {

public $value;

Expand Down
2 changes: 1 addition & 1 deletion lib/Less/Tree/NameValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* @private
*/
class Less_Tree_NameValue extends Less_Tree {
class Less_Tree_NameValue extends Less_Tree implements Less_Tree_HasValueProperty {

public $name;
public $value;
Expand Down
2 changes: 1 addition & 1 deletion lib/Less/Tree/Negative.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* @private
*/
class Less_Tree_Negative extends Less_Tree {
class Less_Tree_Negative extends Less_Tree implements Less_Tree_HasValueProperty {

public $value;

Expand Down
2 changes: 1 addition & 1 deletion lib/Less/Tree/Paren.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* @private
*/
class Less_Tree_Paren extends Less_Tree {
class Less_Tree_Paren extends Less_Tree implements Less_Tree_HasValueProperty {

/** @var Less_Tree */
public $value;
Expand Down
2 changes: 1 addition & 1 deletion lib/Less/Tree/Quoted.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* @private
*/
class Less_Tree_Quoted extends Less_Tree {
class Less_Tree_Quoted extends Less_Tree implements Less_Tree_HasValueProperty {
public $escaped;
public $value;
public $quote;
Expand Down
2 changes: 1 addition & 1 deletion lib/Less/Tree/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* @private
*/
class Less_Tree_Rule extends Less_Tree {
class Less_Tree_Rule extends Less_Tree implements Less_Tree_HasValueProperty {

public $name;
/** @var Less_Tree */
Expand Down
2 changes: 1 addition & 1 deletion lib/Less/Tree/Selector.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function CacheElements() {
continue;
}

if ( !property_exists( $v->value, 'value' ) || !is_string( $v->value->value ) ) {
if ( !( $v->value instanceof Less_Tree_HasValueProperty ) || !is_string( $v->value->value ) ) {
$this->cacheable = false;
return;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Less/Tree/UnicodeDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* @private
*/
class Less_Tree_UnicodeDescriptor extends Less_Tree {
class Less_Tree_UnicodeDescriptor extends Less_Tree implements Less_Tree_HasValueProperty {

public $value;

Expand Down
2 changes: 1 addition & 1 deletion lib/Less/Tree/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* @private
*/
class Less_Tree_Url extends Less_Tree {
class Less_Tree_Url extends Less_Tree implements Less_Tree_HasValueProperty {

public $attrs;
public $value;
Expand Down
2 changes: 1 addition & 1 deletion lib/Less/Tree/Value.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* @private
*/
class Less_Tree_Value extends Less_Tree {
class Less_Tree_Value extends Less_Tree implements Less_Tree_HasValueProperty {

public $value;

Expand Down

0 comments on commit 853a9db

Please sign in to comment.