From ddc5bbd1bbd197fe83f48b796388c3da488cdca0 Mon Sep 17 00:00:00 2001 From: Pierre Goiffon Date: Tue, 10 Aug 2021 15:04:29 +0200 Subject: [PATCH] =?UTF-8?q?:white=5Fcheck=5Fmark:=20N=C2=B03867=20Fix=20wr?= =?UTF-8?q?ite=20config=20now=20removes=20PHP=20vars=20Now=20the=20PhpPars?= =?UTF-8?q?er=20tree=20has=20a=20different=20structure,=20beginning=20with?= =?UTF-8?q?=20a=20PhpParser\Node\Stmt\Expression?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/iTopConfigParser.php | 44 ++++++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/core/iTopConfigParser.php b/core/iTopConfigParser.php index 00b30864e..1e03adb31 100644 --- a/core/iTopConfigParser.php +++ b/core/iTopConfigParser.php @@ -6,6 +6,8 @@ */ use PhpParser\Node\Expr\Assign; +use PhpParser\Node\Expr\Variable; +use PhpParser\Parser; use PhpParser\ParserFactory; use PhpParser\PrettyPrinter\Standard; @@ -80,38 +82,52 @@ class iTopConfigParser * @param \PhpParser\Parser $oParser * @param $sConfig * - * @return \Combodo\iTop\Config\Validator\ConfigNodesVisitor + * @return void */ - private function BrowseFile(\PhpParser\Parser $oParser, $sConfig) + private function BrowseFile(Parser $oParser, $sConfig) { $prettyPrinter = new Standard(); - try - { + try { $aNodes = $oParser->parse($sConfig); } - catch (\Error $e) - { + catch (\Error $e) { $sMessage = Dict::Format('config-parse-error', $e->getMessage(), $e->getLine()); $this->oException = new \Exception($sMessage, 0, $e); } - foreach ($aNodes as $oAssignation) - { - if (! $oAssignation instanceof Assign) - { + foreach ($aNodes as $sKey => $oNode) { + // With PhpParser 3 we had an Assign node at root + // In PhpParser 4 the root node is now an Expression + + if (false === ($oNode instanceof \PhpParser\Node\Stmt\Expression)) { + continue; + } + /** @var \PhpParser\Node\Stmt\Expression $oNode */ + + if (false === ($oNode->expr instanceof Assign)) { + continue; + } + /** @var Assign $oAssignation */ + $oAssignation = $oNode->expr; + + if (false === ($oAssignation->var instanceof Variable)) { + continue; + } + if (false === ($oAssignation->expr instanceof PhpParser\Node\Expr\Array_)) { continue; } $sCurrentRootVar = $oAssignation->var->name; - if (!array_key_exists($sCurrentRootVar, $this->aVarsMap)) - { + if (!array_key_exists($sCurrentRootVar, $this->aVarsMap)) { continue; } $aCurrentRootVarMap =& $this->aVarsMap[$sCurrentRootVar]; - foreach ($oAssignation->expr->items as $oItem) - { + foreach ($oAssignation->expr->items as $oItem) { + if (false === ($oItem->key instanceof PhpParser\Node\Scalar\String_)) { + continue; + } $sValue = $prettyPrinter->prettyPrintExpr($oItem->value); $aCurrentRootVarMap[$oItem->key->value] = $sValue; }