N°8796 - Add PHP code style validation in iTop and extensions - format whole code base

This commit is contained in:
odain
2025-11-07 20:39:38 +01:00
parent 7681c157ec
commit b0a792afab
369 changed files with 22041 additions and 26866 deletions

View File

@@ -1,4 +1,5 @@
<?php
/*
* @copyright Copyright (C) 2010-2025 Combodo SAS
* @license http://opensource.org/licenses/AGPL-3.0
@@ -17,24 +18,25 @@ use utils;
class ConfigEditorController extends Controller
{
public const ROUTE_NAMESPACE = 'config_editor';
public const MODULE_NAME = "itop-config";
public const ROUTE_NAMESPACE = 'config_editor';
public const MODULE_NAME = "itop-config";
protected array $aWarnings = [];
protected array $aInfo = [];
protected array $aErrors = [];
protected array $aSuccesses = [];
public function __construct() {
public function __construct()
{
parent::__construct(MODULESROOT.static::MODULE_NAME.'/templates', static::MODULE_NAME);
}
public function OperationEdit() : void
{
public function OperationEdit(): void
{
$bShowEditor = true;
$sConfigChecksum = '';
$sCurrentConfig = '';
try {
try {
$sOperation = utils::ReadParam('edit_operation');
if (MetaModel::GetConfig()->Get('demo_mode')) {
throw new Exception(Dict::S('config-not-allowed-in-demo'), iTopConfigValidator::CONFIG_INFO);
@@ -52,8 +54,7 @@ class ConfigEditorController extends Controller
try {
if ($sOperation == 'revert') {
$this->AddAlert(Dict::S('config-reverted'), iTopConfigValidator::CONFIG_WARNING);
}
else if ($sOperation == 'save') {
} elseif ($sOperation == 'save') {
$sTransactionId = utils::ReadParam('transaction_id', '', false, 'transaction_id');
if (!utils::IsTransactionValid($sTransactionId)) {
throw new Exception(Dict::S('config-error-transaction'), iTopConfigValidator::CONFIG_ERROR);
@@ -99,24 +100,21 @@ class ConfigEditorController extends Controller
$this->AddAlert($oValidator->CheckAsyncTasksRetryConfig($oTempConfig), iTopConfigValidator::CONFIG_WARNING);
// Read the config from disk after save
$sCurrentConfig = file_get_contents($sConfigFile);
$sConfigChecksum = md5($sCurrentConfig);
}
}
catch (Exception $e) {
} catch (Exception $e) {
$this->AddAlertFromException($e);
}
$this->AddAceScripts();
}
catch (Exception $e) {
} catch (Exception $e) {
$bShowEditor = false;
$this->AddAlertFromException($e);
}
// display page
// display page
$this->DisplayPage([
'aErrors' => $this->aErrors,
'aWarnings' => $this->aWarnings,
@@ -127,7 +125,7 @@ class ConfigEditorController extends Controller
'sChecksum' => $sConfigChecksum,
'sPrevConfig' => $sCurrentConfig,
'sNewConfig' => $sCurrentConfig,
]);
]);
}
/**
@@ -143,7 +141,6 @@ class ConfigEditorController extends Controller
$this->AddLinkedScript(utils::GetAbsoluteUrlAppRoot().$sAceDir.'ext-searchbox.js');
}
public function AddAlertFromException(Exception $e): void
{
$this->AddAlert($e->getMessage(), $e->getCode());
@@ -158,18 +155,18 @@ class ConfigEditorController extends Controller
return;
}
switch ($iLevel) {
case iTopConfigValidator::CONFIG_SUCCESS :
case iTopConfigValidator::CONFIG_SUCCESS:
$this->aSuccesses[] = $sMessage;
break;
case iTopConfigValidator::CONFIG_WARNING :
case iTopConfigValidator::CONFIG_WARNING:
$this->aWarnings[] = $sMessage;
break;
case iTopConfigValidator::CONFIG_INFO :
case iTopConfigValidator::CONFIG_INFO:
$this->aInfo[] = $sMessage;
break;
default :
default:
$this->aErrors[] = $sMessage;
}
}
}
}

View File

@@ -1,4 +1,5 @@
<?php
/**
* Created by Bruno DA SILVA, working for Combodo
* Date: 31/12/2019
@@ -7,7 +8,6 @@
namespace Combodo\iTop\Config\Validator;
use PhpParser\NodeTraverser;
use PhpParser\ParserFactory;
@@ -23,17 +23,17 @@ class iTopConfigAstValidator
*/
public function Validate($sConfig)
{
$oParser = (new ParserFactory())->createForNewestSupportedVersion();
$oParser = (new ParserFactory())->createForNewestSupportedVersion();
$oNodeVisitor = new ConfigNodesVisitor();
try {
$aInitialNodes = $oParser->parse($sConfig);
} catch (\Error $e) {
$sMessage = 'Invalid configuration: '. \Dict::Format('config-parse-error', $e->getMessage(), $e->getLine());
$sMessage = 'Invalid configuration: '.\Dict::Format('config-parse-error', $e->getMessage(), $e->getLine());
throw new \Exception($sMessage, iTopConfigValidator::CONFIG_ERROR, $e);
}catch (\Exception $e) {
$sMessage = 'Invalid configuration: '. \Dict::Format('config-parse-error', $e->getMessage(), $e->getLine());
} catch (\Exception $e) {
$sMessage = 'Invalid configuration: '.\Dict::Format('config-parse-error', $e->getMessage(), $e->getLine());
throw new \Exception($sMessage, iTopConfigValidator::CONFIG_ERROR, $e);
}
@@ -41,4 +41,4 @@ class iTopConfigAstValidator
$oTraverser->addVisitor($oNodeVisitor);
$oTraverser->traverse($aInitialNodes);
}
}
}

View File

@@ -1,4 +1,5 @@
<?php
/**
* Created by Bruno DA SILVA, working for Combodo
* Date: 31/12/2019
@@ -9,7 +10,6 @@ namespace Combodo\iTop\Config\Validator;
class iTopConfigSyntaxValidator
{
/**
* @param $sRawConfig
*
@@ -22,15 +22,13 @@ class iTopConfigSyntaxValidator
ob_start();
// in PHP < 7.0.0 syntax errors are in output
// in PHP >= 7.0.0 syntax errors are thrown as Error
$sConfig = preg_replace(array('#^\s*<\?php#', '#\?>\s*$#'), '', $sRawConfig);
$sConfig = preg_replace(['#^\s*<\?php#', '#\?>\s*$#'], '', $sRawConfig);
eval('if(0){'.trim($sConfig).'}');
$sNoise = trim(ob_get_contents());
}
catch (\Error $e) {
} catch (\Error $e) {
// ParseError only thrown in PHP7
throw new \Exception('Error in configuration: '.$e->getMessage().' at line '.$e->getLine(), iTopConfigValidator::CONFIG_ERROR);
}
finally {
} finally {
ob_end_clean();
}
@@ -40,11 +38,10 @@ class iTopConfigSyntaxValidator
$sLine = $aMatches[3];
$sMessage = \Dict::Format('config-parse-error', $sMessage, $sLine);
throw new \Exception($sMessage, iTopConfigValidator::CONFIG_ERROR);
}
else {
} else {
// Note: sNoise is an html output, but so far it was ok for me (e.g. showing the entire call stack)
throw new \Exception('Syntax error in configuration file: <tt>'.$sNoise.'</tt>', iTopConfigValidator::CONFIG_ERROR);
}
}
}
}
}

View File

@@ -5,18 +5,19 @@ namespace Combodo\iTop\Config\Validator;
use AsyncTask;
use ReflectionClass;
class iTopConfigValidator {
const CONFIG_ERROR = 0;
const CONFIG_WARNING = 1;
const CONFIG_INFO = 2;
const CONFIG_SUCCESS = 3;
class iTopConfigValidator
{
public const CONFIG_ERROR = 0;
public const CONFIG_WARNING = 1;
public const CONFIG_INFO = 2;
public const CONFIG_SUCCESS = 3;
/**
* @param $sRawConfig
*
* @throws \Exception
*/
public function Validate($sRawConfig):void
public function Validate($sRawConfig): void
{
$oiTopConfigValidator = new iTopConfigAstValidator();
$oiTopConfigValidator->Validate($sRawConfig);
@@ -26,7 +27,7 @@ class iTopConfigValidator {
$oiTopConfigValidator->Validate($sRawConfig);
}
function DBPasswordIsOk($sPassword):bool
public function DBPasswordIsOk($sPassword): bool
{
$bIsWindows = (array_key_exists('WINDIR', $_SERVER) || array_key_exists('windir', $_SERVER));
@@ -37,7 +38,6 @@ class iTopConfigValidator {
return true;
}
public function CheckAsyncTasksRetryConfig(\Config $oTempConfig): array
{
$aWarnings = [];
@@ -56,4 +56,4 @@ class iTopConfigValidator {
return $aWarnings;
}
}
}