From 1cfb52d220e5ace41eaad4e682e82c8998e54ec4 Mon Sep 17 00:00:00 2001 From: Pierre Goiffon Date: Mon, 19 Oct 2020 11:57:53 +0200 Subject: [PATCH] :bug: Fix CoreException constructor generating a warning on PHP >= 7.2 In the CoreException constructor, we're using the $aContextData parameter to do a count(), a foreach(), and uses values as string. Only a null check was done. Now we are also checking that the value is_array(). As others checks (Countable, Iterable, __toString() impl) are quite difficult depending on the PHP version we're running, we didn't add any other checks. The call in \MatchExpression::__construct (added in 05a0d612) was passing directly an Expression object. We could embed it in an array, but the object hierarchy isn't implementing __toString so we would have another bug. In consequence we removed this parameter. --- core/coreexception.class.inc.php | 40 ++++++++++++++++++------------- core/oql/expression.class.inc.php | 2 +- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/core/coreexception.class.inc.php b/core/coreexception.class.inc.php index 7188446c7..cf6469807 100644 --- a/core/coreexception.class.inc.php +++ b/core/coreexception.class.inc.php @@ -28,31 +28,39 @@ class CoreException extends Exception { + /** + * CoreException constructor. + * + * @param string $sIssue error message + * @param array|null $aContextData key/value array, value MUST implements _toString + * @param string $sImpact + * @param Exception|null $oPrevious + */ public function __construct($sIssue, $aContextData = null, $sImpact = '', $oPrevious = null) { $this->m_sIssue = $sIssue; $this->m_sImpact = $sImpact; - $this->m_aContextData = $aContextData ? $aContextData : array(); - + + if (is_array($aContextData)) { + $this->m_aContextData = $aContextData; + } else { + $this->m_aContextData = []; + } + $sMessage = $sIssue; - if (!empty($sImpact)) $sMessage .= "($sImpact)"; - if (count($this->m_aContextData) > 0) - { + if (!empty($sImpact)) { + $sMessage .= "($sImpact)"; + } + if (count($this->m_aContextData) > 0) { $sMessage .= ": "; $aContextItems = array(); - foreach($this->m_aContextData as $sKey => $value) - { - if (is_array($value)) - { + foreach ($this->m_aContextData as $sKey => $value) { + if (is_array($value)) { $aPairs = array(); - foreach($value as $key => $val) - { - if (is_array($val)) - { + foreach ($value as $key => $val) { + if (is_array($val)) { $aPairs[] = $key.'=>('.implode(', ', $val).')'; - } - else - { + } else { $aPairs[] = $key.'=>'.$val; } } diff --git a/core/oql/expression.class.inc.php b/core/oql/expression.class.inc.php index 1f65b5c5b..141bf4198 100644 --- a/core/oql/expression.class.inc.php +++ b/core/oql/expression.class.inc.php @@ -872,7 +872,7 @@ class MatchExpression extends BinaryExpression public function __construct(FieldExpression $oLeftExpr, Expression $oRightExpr) { if (!$oRightExpr instanceof ScalarExpression && !$oRightExpr instanceof VariableExpression) { - throw new CoreException('Only instance of ScalarExpression or VariableExpression are allowed in MATCHES '.get_class( $oRightExpr).' found', $oRightExpr); + throw new CoreException('Only instance of ScalarExpression or VariableExpression are allowed in MATCHES '.get_class($oRightExpr).' found'); } parent::__construct($oLeftExpr, 'MATCHES', $oRightExpr); }