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

This commit is contained in:
odain
2025-11-07 15:39:53 +01:00
parent 12f23113f5
commit 890a2568c8
2110 changed files with 53099 additions and 63885 deletions

View File

@@ -1,4 +1,5 @@
<?php
/**
* Created by Bruno DA SILVA, working for Combodo
* Date: 31/12/2019
@@ -7,17 +8,16 @@
namespace Combodo\iTop\Config\Validator;
use PhpParser\Node;
use PhpParser\NodeVisitorAbstract;
class ConfigNodesVisitor extends NodeVisitorAbstract
{
private $aAllowedNodeClasses = array();
private $aAllowedNodeClasses = [];
public function __construct()
{
$this->aAllowedNodeClasses = array(
$this->aAllowedNodeClasses = [
Node\Scalar::class,
Node\Name::class,
@@ -53,7 +53,7 @@ class ConfigNodesVisitor extends NodeVisitorAbstract
Node\Stmt\Const_::class,
Node\Stmt\Global_::class,
);
];
}
/**
@@ -74,10 +74,8 @@ class ConfigNodesVisitor extends NodeVisitorAbstract
*/
public function ValidateNode(Node $node)
{
foreach ($this->aAllowedNodeClasses as $sAllowedNodeClass)
{
if ($node instanceof $sAllowedNodeClass)
{
foreach ($this->aAllowedNodeClasses as $sAllowedNodeClass) {
if ($node instanceof $sAllowedNodeClass) {
return;
}
}
@@ -92,47 +90,37 @@ class ConfigNodesVisitor extends NodeVisitorAbstract
*/
private function ThrowInvalidConf(Node $node)
{
if (in_array('name', $node->getSubNodeNames()))
{
if (in_array('name', $node->getSubNodeNames())) {
$sMessage = sprintf(
"Invalid configuration: %s of type %s is forbidden in line %d",
$node->name,
$node->getType(),
$node->getLine()
);
}
elseif (in_array('class', $node->getSubNodeNames()))
{
} elseif (in_array('class', $node->getSubNodeNames())) {
if (in_array('name', $node->class->getSubNodeNames()))
{
if (in_array('name', $node->class->getSubNodeNames())) {
$sMessage = sprintf(
"Invalid configuration: usage of the class '%s' (%s) is forbidden in line %d",
is_object($node->class) ? $node->class->name : $node->class,
$node->getType(),
$node->getLine()
);
}
else
{
} else {
$sMessage = sprintf(
"Invalid configuration: usage of %s is forbidden in line %d",
$node->getType(),
$node->getLine()
);
}
}
elseif ($node->hasAttribute('name'))
{
} elseif ($node->hasAttribute('name')) {
$sMessage = sprintf(
"Invalid configuration: %s of type %s is forbidden in line %d",
$node->getAttribute('name'),
$node->getType(),
$node->getLine()
);
}
else
{
} else {
$sMessage = sprintf(
"Invalid configuration: %s is forbidden in line %d",
$node->getType(),
@@ -142,4 +130,4 @@ class ConfigNodesVisitor extends NodeVisitorAbstract
throw new \Exception($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;
@@ -30,10 +30,10 @@ class iTopConfigAstValidator
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, 0, $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, 0, $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
*
@@ -17,40 +17,31 @@ class iTopConfigSyntaxValidator
*/
public function Validate($sRawConfig)
{
try
{
try {
ini_set('display_errors', 1);
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());
}
finally
{
} finally {
ob_end_clean();
}
if (strlen($sNoise) > 0)
{
if (preg_match("/(Error|Parse error|Notice|Warning): (.+) in \S+ : eval\(\)'d code on line (\d+)/i", strip_tags($sNoise), $aMatches))
{
if (strlen($sNoise) > 0) {
if (preg_match("/(Error|Parse error|Notice|Warning): (.+) in \S+ : eval\(\)'d code on line (\d+)/i", strip_tags($sNoise), $aMatches)) {
$sMessage = $aMatches[2];
$sLine = $aMatches[3];
$sMessage = \Dict::Format('config-parse-error', $sMessage, $sLine);
throw new \Exception($sMessage);
}
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>');
}
}
}
}
}