From c0c9ea9287ede271f25ee2913beb2a12dd9a91a0 Mon Sep 17 00:00:00 2001 From: odain Date: Tue, 16 Sep 2025 15:38:30 +0200 Subject: [PATCH] =?UTF-8?q?N=C2=B04789=20-=20Parse=20datamodel=20module.xx?= =?UTF-8?q?x.php=20files=20instead=20of=20interpreting=20them=20(#746)=20-?= =?UTF-8?q?=20namespacing=20ModuleFileReader=20classes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/autoload.php | 5 +- lib/composer/autoload_psr4.php | 2 +- lib/composer/autoload_static.php | 4 +- lib/composer/platform_check.php | 5 +- setup/extensionsmap.class.inc.php | 4 ++ setup/modulediscovery.class.inc.php | 2 + setup/modulediscovery/ModuleFileReader.php | 51 ++++++++++++------- .../ModuleFileReaderException.php | 6 ++- setup/runtimeenv.class.inc.php | 2 + .../InstallationFileService.php | 1 + setup/wizardsteps.class.inc.php | 1 + .../modulediscovery/ModuleFileReaderTest.php | 5 +- .../Evaluation/PhpExpressionEvaluatorTest.php | 4 +- 13 files changed, 63 insertions(+), 29 deletions(-) diff --git a/lib/autoload.php b/lib/autoload.php index 9ee03077e..db10dc867 100644 --- a/lib/autoload.php +++ b/lib/autoload.php @@ -14,7 +14,10 @@ if (PHP_VERSION_ID < 50600) { echo $err; } } - throw new RuntimeException($err); + trigger_error( + $err, + E_USER_ERROR + ); } require_once __DIR__ . '/composer/autoload_real.php'; diff --git a/lib/composer/autoload_psr4.php b/lib/composer/autoload_psr4.php index 226bd7184..954bb3d37 100644 --- a/lib/composer/autoload_psr4.php +++ b/lib/composer/autoload_psr4.php @@ -55,7 +55,7 @@ return array( 'Psr\\Cache\\' => array($vendorDir . '/psr/cache/src'), 'PhpParser\\' => array($vendorDir . '/nikic/php-parser/lib/PhpParser'), 'Pelago\\Emogrifier\\' => array($vendorDir . '/pelago/emogrifier/src'), - 'League\\OAuth2\\Client\\' => array($vendorDir . '/league/oauth2-google/src', $vendorDir . '/league/oauth2-client/src'), + 'League\\OAuth2\\Client\\' => array($vendorDir . '/league/oauth2-client/src', $vendorDir . '/league/oauth2-google/src'), 'Laminas\\Validator\\' => array($vendorDir . '/laminas/laminas-validator/src'), 'Laminas\\Stdlib\\' => array($vendorDir . '/laminas/laminas-stdlib/src'), 'Laminas\\ServiceManager\\' => array($vendorDir . '/laminas/laminas-servicemanager/src'), diff --git a/lib/composer/autoload_static.php b/lib/composer/autoload_static.php index b10aa872d..8de7e0ec9 100644 --- a/lib/composer/autoload_static.php +++ b/lib/composer/autoload_static.php @@ -311,8 +311,8 @@ class ComposerStaticInit7f81b4a2a468a061c306af5e447a9a9f ), 'League\\OAuth2\\Client\\' => array ( - 0 => __DIR__ . '/..' . '/league/oauth2-google/src', - 1 => __DIR__ . '/..' . '/league/oauth2-client/src', + 0 => __DIR__ . '/..' . '/league/oauth2-client/src', + 1 => __DIR__ . '/..' . '/league/oauth2-google/src', ), 'Laminas\\Validator\\' => array ( diff --git a/lib/composer/platform_check.php b/lib/composer/platform_check.php index 72145773d..dee74e173 100644 --- a/lib/composer/platform_check.php +++ b/lib/composer/platform_check.php @@ -36,7 +36,8 @@ if ($issues) { echo 'Composer detected issues in your platform:' . PHP_EOL.PHP_EOL . str_replace('You are running '.PHP_VERSION.'.', '', implode(PHP_EOL, $issues)) . PHP_EOL.PHP_EOL; } } - throw new \RuntimeException( - 'Composer detected issues in your platform: ' . implode(' ', $issues) + trigger_error( + 'Composer detected issues in your platform: ' . implode(' ', $issues), + E_USER_ERROR ); } diff --git a/setup/extensionsmap.class.inc.php b/setup/extensionsmap.class.inc.php index 851a63bdd..d0cf51851 100644 --- a/setup/extensionsmap.class.inc.php +++ b/setup/extensionsmap.class.inc.php @@ -1,4 +1,8 @@ createForNewestSupportedVersion(); + $oParser = (new ParserFactory())->createForNewestSupportedVersion(); $aNodes = $oParser->parse(file_get_contents($sModuleFilePath)); } - catch (PhpParser\Error $e) { - throw new \ModuleFileReaderException($e->getMessage(), 0, $e, $sModuleFilePath); + catch (Error $e) { + throw new ModuleFileReaderException($e->getMessage(), 0, $e, $sModuleFilePath); } try { foreach ($aNodes as $sKey => $oNode) { - if ($oNode instanceof \PhpParser\Node\Stmt\Expression) { + if ($oNode instanceof Expression) { $aModuleInfo = $this->GetModuleInformationFromAddModuleCall($sModuleFilePath, $oNode); if (! is_null($aModuleInfo)){ $this->CompleteModuleInfoWithFilePath($aModuleInfo); @@ -64,7 +79,7 @@ class ModuleFileReader { } } - if ($oNode instanceof PhpParser\Node\Stmt\If_) { + if ($oNode instanceof If_) { $aModuleInfo = $this->GetModuleInformationFromIf($sModuleFilePath, $oNode); if (! is_null($aModuleInfo)){ $this->CompleteModuleInfoWithFilePath($aModuleInfo); @@ -186,17 +201,17 @@ class ModuleFileReader { * @param \PhpParser\Node\Expr\Assign $oAssignation * * @return array|null - * @throws \ModuleFileReaderException + * @throws ModuleFileReaderException */ private function GetModuleInformationFromAddModuleCall(string $sModuleFilePath, \PhpParser\Node\Stmt\Expression $oExpression) : ?array { /** @var Assign $oAssignation */ $oAssignation = $oExpression->expr; - if (false === ($oAssignation instanceof PhpParser\Node\Expr\StaticCall)) { + if (false === ($oAssignation instanceof StaticCall)) { return null; } - /** @var PhpParser\Node\Expr\StaticCall $oAssignation */ + /** @var StaticCall $oAssignation */ if ("SetupWebPage" !== $oAssignation?->class?->name) { return null; @@ -212,24 +227,24 @@ class ModuleFileReader { } $oModuleId = $aArgs[1]; - if (false === ($oModuleId instanceof PhpParser\Node\Arg)) { + if (false === ($oModuleId instanceof Arg)) { throw new ModuleFileReaderException("2nd parameter to SetupWebPage::AddModule call issue: " . get_class($oModuleId), 0, null, $sModuleFilePath); } - /** @var PhpParser\Node\Arg $oModuleId */ - if (false === ($oModuleId->value instanceof PhpParser\Node\Scalar\String_)) { + /** @var Arg $oModuleId */ + if (false === ($oModuleId->value instanceof String_)) { throw new ModuleFileReaderException("2nd parameter to SetupWebPage::AddModule not a string: " . get_class($oModuleId->value), 0, null, $sModuleFilePath); } $sModuleId = $this->oPhpExpressionEvaluator->EvaluateExpression($oModuleId->value); $oModuleConfigInfo = $aArgs[2]; - if (false === ($oModuleConfigInfo instanceof PhpParser\Node\Arg)) { + if (false === ($oModuleConfigInfo instanceof Arg)) { throw new ModuleFileReaderException("3rd parameter to SetupWebPage::AddModule call issue: " . get_class($oModuleConfigInfo), 0, null, $sModuleFilePath); } - /** @var PhpParser\Node\Arg $oModuleConfigInfo */ - if (false === ($oModuleConfigInfo->value instanceof PhpParser\Node\Expr\Array_)) { + /** @var Arg $oModuleConfigInfo */ + if (false === ($oModuleConfigInfo->value instanceof Array_)) { throw new ModuleFileReaderException("3rd parameter to SetupWebPage::AddModule not an array: " . get_class($oModuleConfigInfo->value), 0, null, $sModuleFilePath); } @@ -251,14 +266,14 @@ class ModuleFileReader { * @param \PhpParser\Node\Stmt\If_ $oNode * * @return array|null - * @throws \ModuleFileReaderException + * @throws ModuleFileReaderException */ private function GetModuleInformationFromIf(string $sModuleFilePath, \PhpParser\Node\Stmt\If_ $oNode) : ?array { $bCondition = $this->oPhpExpressionEvaluator->EvaluateExpression($oNode->cond); if ($bCondition) { foreach ($oNode->stmts as $oSubNode) { - if ($oSubNode instanceof \PhpParser\Node\Stmt\Expression) { + if ($oSubNode instanceof Expression) { $aModuleConfig = $this->GetModuleInformationFromAddModuleCall($sModuleFilePath, $oSubNode); if (!is_null($aModuleConfig)) { return $aModuleConfig; @@ -271,7 +286,7 @@ class ModuleFileReader { if (! is_null($oNode->elseifs)) { foreach ($oNode->elseifs as $oElseIfSubNode) { - /** @var \PhpParser\Node\Stmt\ElseIf_ $oElseIfSubNode */ + /** @var ElseIf_ $oElseIfSubNode */ $bCondition = $this->oPhpExpressionEvaluator->EvaluateExpression($oElseIfSubNode->cond); if ($bCondition) { return $this->GetModuleConfigurationFromStatement($sModuleFilePath, $oElseIfSubNode->stmts); @@ -289,7 +304,7 @@ class ModuleFileReader { private function GetModuleConfigurationFromStatement(string $sModuleFilePath, array $aStmts) : ?array { foreach ($aStmts as $oSubNode) { - if ($oSubNode instanceof \PhpParser\Node\Stmt\Expression) { + if ($oSubNode instanceof Expression) { $aModuleConfig = $this->GetModuleInformationFromAddModuleCall($sModuleFilePath, $oSubNode); if (!is_null($aModuleConfig)) { return $aModuleConfig; diff --git a/setup/modulediscovery/ModuleFileReaderException.php b/setup/modulediscovery/ModuleFileReaderException.php index 00f074819..44a94279d 100644 --- a/setup/modulediscovery/ModuleFileReaderException.php +++ b/setup/modulediscovery/ModuleFileReaderException.php @@ -1,5 +1,9 @@ $oPrevious?->getMessage(), 'stack' => $e->getTraceAsString()]; if (!is_null($sModuleFile)) { diff --git a/setup/runtimeenv.class.inc.php b/setup/runtimeenv.class.inc.php index fb28ec247..0051ddaaa 100644 --- a/setup/runtimeenv.class.inc.php +++ b/setup/runtimeenv.class.inc.php @@ -25,6 +25,8 @@ */ use Combodo\iTop\PhpParser\Evaluation\PhpExpressionEvaluator; +use Combodo\iTop\Setup\ModuleDiscovery\ModuleFileReader; +use Combodo\iTop\Setup\ModuleDiscovery\ModuleFileReaderException; require_once APPROOT."setup/modulediscovery.class.inc.php"; require_once APPROOT.'setup/modelfactory.class.inc.php'; diff --git a/setup/unattended-install/InstallationFileService.php b/setup/unattended-install/InstallationFileService.php index 700a1cc0a..39ce907dc 100644 --- a/setup/unattended-install/InstallationFileService.php +++ b/setup/unattended-install/InstallationFileService.php @@ -1,6 +1,7 @@ expectException(\ModuleFileReaderException::class); + $this->expectException(ModuleFileReaderException::class); $this->expectExceptionMessage("Syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ',' or ']' or ')' on line 31"); ModuleFileReader::GetInstance()->ReadModuleFileInformation($sModuleFilePath); diff --git a/tests/php-unit-tests/unitary-tests/sources/PhpParser/Evaluation/PhpExpressionEvaluatorTest.php b/tests/php-unit-tests/unitary-tests/sources/PhpParser/Evaluation/PhpExpressionEvaluatorTest.php index 52ef42355..214f58feb 100644 --- a/tests/php-unit-tests/unitary-tests/sources/PhpParser/Evaluation/PhpExpressionEvaluatorTest.php +++ b/tests/php-unit-tests/unitary-tests/sources/PhpParser/Evaluation/PhpExpressionEvaluatorTest.php @@ -162,7 +162,7 @@ class PhpExpressionEvaluatorTest extends ItopDataTestCase { global $oEvaluationFakeClass; $oEvaluationFakeClass = new EvaluationFakeClass(); - $this->expectException(\ModuleFileReaderException::class); + $this->expectException(ModuleFileReaderException::class); $oPhpExpressionEvaluator = new PhpExpressionEvaluator(); $oPhpExpressionEvaluator->ParseAndEvaluateExpression($sExpression); } @@ -172,7 +172,7 @@ class PhpExpressionEvaluatorTest extends ItopDataTestCase { * @param string $sBooleanExpr * * @return mixed - * @throws \ModuleFileReaderException + * @throws ModuleFileReaderException */ private function UnprotectedComputeExpression(string $sExpr) : mixed {