Skip to content

Commit

Permalink
[PHP] Fix some PHP 7.2 deprecations (ezsystems#1382)
Browse files Browse the repository at this point in the history
* [PHP] Change use of create_function for closure

* [PHP] Fix some usage of count

* CS

* Add symfony/polyfill-php73 in order to use is_countable() function

* Use ternary operator to shorten closure like others

* CS
  • Loading branch information
andrerom committed Aug 15, 2018
1 parent 0afc0ae commit 6321a92
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 21 deletions.
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -40,6 +40,7 @@
"ezsystems/ezprestapiprovider-ls": "~5.3",
"ezsystems/ezstarrating-ls-extension": "~5.3",
"ezsystems/ezwt-ls-extension": "~5.3",
"symfony/polyfill-php73": "^1.9",
"zetacomponents/archive": "~1.5",
"zetacomponents/authentication": "~1.4",
"zetacomponents/authentication-database-tiein": "~1.2",
Expand Down
9 changes: 8 additions & 1 deletion kernel/classes/datatypes/ezmultioption/ezmultioption.php
Expand Up @@ -173,7 +173,14 @@ function addOption( $newID, $OptionID, $optionValue, $optionAdditionalPrice )
*/
function sortMultiOptions()
{
usort( $this->Options, create_function( '$a, $b', 'if ( $a["priority"] == $b["priority"] ) { return 0; } return ( $a["priority"] < $b["priority"] ) ? -1 : 1;' ) );
usort( $this->Options, function( $a, $b ) {
if ( $a['priority'] == $b['priority'] )
{
return 0;
}

return ( $a['priority'] < $b['priority'] ) ? -1 : 1;
});
$this->changeMultiOptionID();
}

Expand Down
4 changes: 1 addition & 3 deletions kernel/classes/ezdatatype.php
Expand Up @@ -187,9 +187,7 @@ static function registeredDataTypes()
$GLOBALS["eZDataTypeObjects"][$dataTypeString] = new $className();
}
}
uasort( $GLOBALS["eZDataTypeObjects"],
create_function( '$a, $b',
'return strcmp( $a->Name, $b->Name);' ) );
uasort( $GLOBALS["eZDataTypeObjects"], function ( $a, $b ) { return strcmp( $a->Name, $b->Name); } );
return $GLOBALS["eZDataTypeObjects"];
}
return null;
Expand Down
11 changes: 6 additions & 5 deletions kernel/classes/ezrssexportitem.php
Expand Up @@ -202,11 +202,12 @@ static function getAttributeMappings( $rssSources )

// sort the array so nodes with deeper path are first
// for class attribute to RSS field mapping
usort( $attributeMappings,
create_function( '$a, $b',
'$a_cnt = count( $a[1]->attribute( \'path_array\' ) );' .
'$b_cnt = count( $b[1]->attribute( \'path_array\' ) );' .
'return ( $a_cnt == $b_cnt ) ? 0 : ( ( $a_cnt > $b_cnt ) ? 1 : -1 );' ) );
usort( $attributeMappings, function ( $a, $b ) {
$a_cnt = count( $a[1]->attribute( 'path_array' ) );
$b_cnt = count( $b[1]->attribute( 'path_array' ) );

return ( $a_cnt == $b_cnt ) ? 0 : ( ( $a_cnt > $b_cnt ) ? 1 : -1 );
});
}

return $attributeMappings;
Expand Down
2 changes: 1 addition & 1 deletion kernel/classes/ezsslzone.php
Expand Up @@ -405,7 +405,7 @@ static function checkObject( $module, $view, $object )
*/

// "flatten" the array
array_walk( $pathStringList, create_function( '&$a', '$a = $a[\'path_string\'];' ) );
array_walk( $pathStringList, function( &$a ) { $a = $a['path_string']; } );
}
else
{
Expand Down
Expand Up @@ -728,7 +728,9 @@ function cleanupAfterRemoving( $attr = array() )
// $IDArray will contain IDs of "Excluded user groups"
$IDArray = explode( ',', $groupID[ 'data_text2' ] );
// $newIDArray will contain array without $contentObjectID
$newIDArray = array_filter( $IDArray, create_function( '$v', 'return ( $v != ' . $contentObjectID .' );' ) );
$newIDArray = array_filter( $IDArray, function ( $v ) use ( $contentObjectID ) {
return $v != $contentObjectID;
});
$newValues = $db->escapeString( implode( ',', $newIDArray ) );
$db->query( "UPDATE ezworkflow_event
SET data_text2 = '$newValues'
Expand Down
14 changes: 11 additions & 3 deletions kernel/private/classes/clusterfilehandlers/ezdfsfilehandler.php
Expand Up @@ -153,9 +153,17 @@ public function loadMetaData( $force = false )
if ( isset( $GLOBALS['eZClusterInfo'] ) &&
count( $GLOBALS['eZClusterInfo'] ) >= self::INFOCACHE_MAX )
{
usort( $GLOBALS['eZClusterInfo'],
create_function( '$a, $b',
'$a=$a["cnt"]; $b=$b["cnt"]; if ( $a > $b ) return -1; else if ( $a == $b ) return 0; else return 1;' ) );
usort( $GLOBALS['eZClusterInfo'], function ( $a, $b ) {
$a = $a['cnt'];
$b = $b['cnt'];

if ( $a == $b )
{
return 0;
}

return $a > $b ? -1 : 1;
});
array_pop( $GLOBALS['eZClusterInfo'] );
}
$GLOBALS['eZClusterInfo'][$this->filePath] = array( 'cnt' => 1,
Expand Down
6 changes: 4 additions & 2 deletions kernel/shop/editvatrule.php
Expand Up @@ -158,8 +158,10 @@ function checkEnteredData( $country, $categories, $vatType, $productCategories,

// Modify chosen categories array
// so that it can be saved into the VAT rule.
$addID = create_function('$i', "return array( 'id' => \$i ) ;" );
$chosenCategories = array_map( $addID, $chosenCategories );
$chosenCategories = array_map(
function( $i ) { return array( 'id' => $i ); },
$chosenCategories
);

$vatRule->setAttribute( 'country_code', $chosenCountry );
$vatRule->setAttribute( 'product_categories', $chosenCategories );
Expand Down
2 changes: 1 addition & 1 deletion lib/eztemplate/classes/eztemplatecompiler.php
Expand Up @@ -1197,7 +1197,7 @@ static function processElementTransformationList( $tpl, &$node, $elements, &$pri
static function processElementTransformationChild( $useComments, $php, $tpl, &$node,
$elementTree, $elementList, &$resourceData )
{
$count = count( $elementList );
$count = is_countable( $elementList ) ? count( $elementList ) : 0;
$lastElement = null;
$newElementList = array();
for ( $i = 0; $i < $count; ++$i )
Expand Down
2 changes: 1 addition & 1 deletion lib/eztemplate/classes/eztemplatenodetool.php
Expand Up @@ -276,7 +276,7 @@ static function isVariableElement( $elements )
*/
static function isPHPVariableElement( $elements )
{
return count( $elements ) === 1 && isset( $elements[0][0] ) && $elements[0][0] == eZTemplate::TYPE_PHP_VARIABLE;
return is_countable( $elements ) && count( $elements ) === 1 && isset( $elements[0][0] ) && $elements[0][0] == eZTemplate::TYPE_PHP_VARIABLE;
}

/*!
Expand Down
8 changes: 5 additions & 3 deletions lib/ezutils/classes/ezphpcreator.php
Expand Up @@ -565,7 +565,7 @@ function thisVariableText( $value, $column = 0, $iteration = 0, $maxIterations =
$column += strlen( $text );
$valueKeys = array_keys( $value );
$isIndexed = true;
for ( $i = 0; $i < count( $valueKeys ); ++$i )
for ( $i = 0, $count = count( $valueKeys ); $i < $count; ++$i )
{
if ( $i !== $valueKeys[$i] )
{
Expand Down Expand Up @@ -676,7 +676,7 @@ static function variableText( $value, $column = 0, $iteration = 0, $maxIteration
$column += strlen( $text );
$valueKeys = array_keys( $value );
$isIndexed = true;
for ( $i = 0; $i < count( $valueKeys ); ++$i )
for ( $i = 0, $count = count( $valueKeys ); $i < $count; ++$i )
{
if ( $i !== $valueKeys[$i] )
{
Expand Down Expand Up @@ -1282,7 +1282,9 @@ function writeVariableUnsetList( $element )
if ( isset( $parameters['spacing'] ) and $this->Spacing )
$spacing = $parameters['spacing'];
$text = 'unset( ';
array_walk( $variableNames, create_function( '&$variableName,$key', '$variableName = "\$" . $variableName;') );
array_walk( $variableNames, function ( &$variableName ) {
$variableName = "\$" . $variableName;
});
$text .= join( ', ', $variableNames );
$text .= " );\n";
$text = eZPHPCreator::prependSpacing( $text, $spacing );
Expand Down

0 comments on commit 6321a92

Please sign in to comment.