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,9 +1,10 @@
<?php
// Copyright (C) 2010-2024 Combodo SAS
//
// This file is part of iTop.
//
// iTop is free software; you can redistribute it and/or modify
// iTop is free software; you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
@@ -37,7 +38,7 @@ class WizardController
protected $sInitialStepClass;
protected $sInitialState;
protected $aParameters;
/**
* Initiailization of the wizard controller
* @param string $sInitialStepClass Class of the initial step/page of the wizard
@@ -47,10 +48,10 @@ class WizardController
{
$this->sInitialStepClass = $sInitialStepClass;
$this->sInitialState = $sInitialState;
$this->aParameters = array();
$this->aSteps = array();
$this->aParameters = [];
$this->aSteps = [];
}
/**
* Pushes information about the current step onto the stack
* @param hash $aStepInfo Array('class' => , 'state' => )
@@ -59,7 +60,7 @@ class WizardController
{
array_push($this->aSteps, $aStepInfo);
}
/**
* Removes information about the previous step from the stack
* @return hash Array('class' => , 'state' => )
@@ -68,7 +69,7 @@ class WizardController
{
return array_pop($this->aSteps);
}
/**
* Reads a "persistent" parameter from the wizard's context
* @param string $sParamCode The code identifying this parameter
@@ -91,7 +92,7 @@ class WizardController
public function GetParamForConfigArray(): array
{
/** @noinspection PhpUnnecessaryLocalVariableInspection */
$aParamValues = array(
$aParamValues = [
'db_server' => $this->GetParameter('db_server', ''),
'db_user' => $this->GetParameter('db_user', ''),
'db_pwd' => $this->GetParameter('db_pwd', ''),
@@ -99,7 +100,7 @@ class WizardController
'db_prefix' => $this->GetParameter('db_prefix', ''),
'db_tls_enabled' => $this->GetParameter('db_tls_enabled', false),
'db_tls_ca' => $this->GetParameter('db_tls_ca', ''),
);
];
return $aParamValues;
}
@@ -114,7 +115,7 @@ class WizardController
{
$this->aParameters[$sParamCode] = $value;
}
/**
* Stores the value of the page's parameter in a "persistent" parameter in the wizard's context
* @param string $sParamCode The code identifying this parameter
@@ -126,7 +127,7 @@ class WizardController
$value = utils::ReadParam($sParamCode, $defaultValue, false, $sSanitizationFilter);
$this->aParameters[$sParamCode] = $value;
}
/**
* Starts the wizard by displaying it in its initial state
*/
@@ -146,23 +147,17 @@ class WizardController
$sCurrentState = utils::ReadParam('_state', $this->sInitialState);
/** @var \WizardStep $oStep */
$oStep = new $sCurrentStepClass($this, $sCurrentState);
if ($oStep->ValidateParams())
{
$this->PushStep(array('class' => $sCurrentStepClass, 'state' => $sCurrentState));
if ($oStep->ValidateParams()) {
$this->PushStep(['class' => $sCurrentStepClass, 'state' => $sCurrentState]);
$aPossibleSteps = $oStep->GetPossibleSteps();
$aNextStepInfo = $oStep->ProcessParams(true); // true => moving forward
if (in_array($aNextStepInfo['class'], $aPossibleSteps))
{
if (in_array($aNextStepInfo['class'], $aPossibleSteps)) {
$oNextStep = new $aNextStepInfo['class']($this, $aNextStepInfo['state']);
$this->DisplayStep($oNextStep);
}
else
{
} else {
throw new Exception("Internal error: Unexpected next step '{$aNextStepInfo['class']}'. The possible next steps are: ".implode(', ', $aPossibleSteps));
}
}
else
{
} else {
$this->DisplayStep($oStep);
}
}
@@ -176,13 +171,13 @@ class WizardController
$sCurrentState = utils::ReadParam('_state', $this->sInitialState);
$oStep = new $sCurrentStepClass($this, $sCurrentState);
$aNextStepInfo = $oStep->ProcessParams(false); // false => Moving backwards
// Display the previous step
$aCurrentStepInfo = $this->PopStep();
$oStep = new $aCurrentStepInfo['class']($this, $aCurrentStepInfo['state']);
$this->DisplayStep($oStep);
}
/**
* Displays the specified 'step' of the wizard
* @param WizardStep $oStep The 'step' to display
@@ -190,14 +185,11 @@ class WizardController
protected function DisplayStep(WizardStep $oStep)
{
$oPage = new SetupPage($oStep->GetTitle());
if ($oStep->RequiresWritableConfig())
{
if ($oStep->RequiresWritableConfig()) {
$sConfigFile = utils::GetConfigFilePath();
if (file_exists($sConfigFile))
{
if (file_exists($sConfigFile)) {
// The configuration file already exists
if (!is_writable($sConfigFile))
{
if (!is_writable($sConfigFile)) {
SetupUtils::ExitReadOnlyMode(false); // Reset readonly mode in case of problem
SetupUtils::EraseSetupToken();
$sRelativePath = utils::GetConfigFilePathRelative();
@@ -215,7 +207,7 @@ HTML;
// Prevent token creation
exit;
}
}
}
}
$oPage->LinkScriptFromAppRoot('setup/setup.js');
$oPage->add_script("function CanMoveForward()\n{\n".$oStep->JSCanMoveForward()."\n}\n");
@@ -247,7 +239,7 @@ HTML;
// Hack to have the "Next >>" button, be the default button, since the first submit button in the form is the default one
$oPage->add_ready_script(
<<<EOF
<<<EOF
$('form').each(function () {
var thisform = $(this);
@@ -292,24 +284,23 @@ on the page's parameters
$oContextTag = new ContextTag(ContextTag::TAG_SETUP);
$sOperation = utils::ReadParam('operation');
$this->aParameters = utils::ReadParam('_params', array(), false, 'raw_data');
$this->aParameters = utils::ReadParam('_params', [], false, 'raw_data');
$this->aSteps = json_decode(utils::ReadParam('_steps', '[]', false, 'raw_data'), true /* bAssoc */);
switch($sOperation)
{
switch ($sOperation) {
case 'next':
$this->Next();
break;
$this->Next();
break;
case 'back':
$this->Back();
break;
$this->Back();
break;
default:
$this->Start();
$this->Start();
}
}
/**
* Provides information about the structure/workflow of the wizard by listing
* the possible list of 'steps' and their dependencies
@@ -318,22 +309,24 @@ on the page's parameters
*/
public function DumpStructure($sStep = '', $aAllSteps = null)
{
if ($aAllSteps == null) $aAllSteps = array();
if ($sStep == '') $sStep = $this->sInitialStepClass;
if ($aAllSteps == null) {
$aAllSteps = [];
}
if ($sStep == '') {
$sStep = $this->sInitialStepClass;
}
$oStep = new $sStep($this, '');
$aAllSteps[$sStep] = $oStep->GetPossibleSteps();
foreach($aAllSteps[$sStep] as $sNextStep)
{
if (!array_key_exists($sNextStep, $aAllSteps))
{
$aAllSteps = $this->DumpStructure($sNextStep , $aAllSteps);
foreach ($aAllSteps[$sStep] as $sNextStep) {
if (!array_key_exists($sNextStep, $aAllSteps)) {
$aAllSteps = $this->DumpStructure($sNextStep, $aAllSteps);
}
}
return $aAllSteps;
}
/**
* Dump the wizard's structure as a string suitable to produce a chart
* using graphviz's "dot" program
@@ -345,25 +338,20 @@ on the page's parameters
$sOutput = "digraph finite_state_machine {\n";
//$sOutput .= "\trankdir=LR;";
$sOutput .= "\tsize=\"10,12\"\n";
$aDeadEnds = array($this->sInitialStepClass);
foreach($aAllSteps as $sStep => $aNextSteps)
{
if (count($aNextSteps) == 0)
{
$aDeadEnds = [$this->sInitialStepClass];
foreach ($aAllSteps as $sStep => $aNextSteps) {
if (count($aNextSteps) == 0) {
$aDeadEnds[] = $sStep;
}
}
$sOutput .= "\tnode [shape = doublecircle]; ".implode(' ', $aDeadEnds).";\n";
$sOutput .= "\tnode [shape = box];\n";
foreach($aAllSteps as $sStep => $aNextSteps)
{
foreach ($aAllSteps as $sStep => $aNextSteps) {
$oStep = new $sStep($this, '');
$sOutput .= "\t$sStep [ label = \"".$oStep->GetTitle()."\"];\n";
if (count($aNextSteps) > 0)
{
foreach($aNextSteps as $sNextStep)
{
if (count($aNextSteps) > 0) {
foreach ($aNextSteps as $sNextStep) {
$sOutput .= "\t$sStep -> $sNextStep;\n";
}
}
@@ -394,18 +382,18 @@ abstract class WizardStep
* @var string
*/
protected $sCurrentState;
public function __construct(WizardController $oWizard, $sCurrentState)
{
$this->oWizard = $oWizard;
$this->sCurrentState = $sCurrentState;
}
public function GetState()
{
return $this->sCurrentState;
}
/**
* Displays the wizard page for the current class/state
* The page can contain any number of "<input/>" fields, but no "<form>...</form>" tag
@@ -445,7 +433,7 @@ abstract class WizardStep
* @return string The title of the wizard page for the current step
*/
abstract public function GetTitle();
/**
* Tells whether the parameters are Ok to move forward
* @return boolean True to move forward, false to stey on the same step
@@ -454,7 +442,7 @@ abstract class WizardStep
{
return true;
}
/**
* Tells whether this step/state is the last one of the wizard (dead-end)
* @return boolean True if the 'Next >>' button should be displayed
@@ -463,7 +451,7 @@ abstract class WizardStep
{
return true;
}
/**
* Tells whether the "Next" button should be enabled interactively
* @return string A piece of javascript code returning either true or false
@@ -472,7 +460,7 @@ abstract class WizardStep
{
return 'return true;';
}
/**
* Returns the label for the " Next >> " button
* @return string The label for the button
@@ -481,7 +469,7 @@ abstract class WizardStep
{
return 'Next';
}
/**
* Tells whether this step/state allows to go back or not
* @return boolean True if the '<< Back' button should be displayed
@@ -490,7 +478,7 @@ abstract class WizardStep
{
return true;
}
/**
* Tells whether the "Back" button should be enabled interactively
* @return string A piece of javascript code returning either true or false
@@ -523,9 +511,9 @@ abstract class WizardStep
* Example of a simple Setup Wizard with some parameters to store
* the installation mode (install | upgrade) and a simple asynchronous
* (AJAX) action.
*
*
* The setup wizard is executed by the following code:
*
*
* $oWizard = new WizardController('Step1');
* $oWizard->Run();
*
@@ -535,12 +523,12 @@ class Step1 extends WizardStep
{
return 'Welcome';
}
public function GetPossibleSteps()
{
return array('Step2', 'Step2bis');
}
public function ProcessParams($bMoveForward = true)
{
$sNextStep = '';
@@ -554,11 +542,11 @@ class Step1 extends WizardStep
{
$this->oWizard->SetParameter('install_mode', 'upgrade');
$sNextStep = 'Step2bis';
}
return array('class' => $sNextStep, 'state' => '');
}
public function Display(WebPage $oPage)
{
$oPage->p('This is Step 1!');
@@ -576,17 +564,17 @@ class Step2 extends WizardStep
{
return 'Installation Parameters';
}
public function GetPossibleSteps()
{
return array('Step3');
}
public function ProcessParams($bMoveForward = true)
{
return array('class' => 'Step3', 'state' => '');
}
public function Display(WebPage $oPage)
{
$oPage->p('This is Step 2! (Installation)');
@@ -599,12 +587,12 @@ class Step2bis extends WizardStep
{
return 'Upgrade Parameters';
}
public function GetPossibleSteps()
{
return array('Step2ter');
}
public function ProcessParams($bMoveForward = true)
{
$sUpgradeInfo = utils::ReadParam('upgrade_info');
@@ -613,7 +601,7 @@ class Step2bis extends WizardStep
$this->oWizard->SetParameter('additional_upgrade_info', $sAdditionalUpgradeInfo);
return array('class' => 'Step2ter', 'state' => '');
}
public function Display(WebPage $oPage)
{
$oPage->p('This is Step 2bis! (Upgrade)');
@@ -621,18 +609,18 @@ class Step2bis extends WizardStep
$oPage->p('Type your name here: <input type="text" id="upgrade_info" name="upgrade_info" value="'.$sUpgradeInfo.'" size="20"/><span id="v_upgrade_info"></span>');
$sAdditionalUpgradeInfo = $this->oWizard->GetParameter('additional_upgrade_info', '');
$oPage->p('The installer replies: <input type="text" name="additional_upgrade_info" value="'.$sAdditionalUpgradeInfo.'" size="20"/>');
$oPage->add_ready_script("$('#upgrade_info').change(function() {
$('#v_upgrade_info').html('<img src=\"../images/indicator.gif\"/>');
WizardAsyncAction('', { upgrade_info: $('#upgrade_info').val() }); });");
}
public function AsyncAction(WebPage $oPage, $sCode, $aParameters)
{
usleep(300000); // 300 ms
$sName = $aParameters['upgrade_info'];
$sReply = addslashes("Hello ".$sName);
$oPage->add_ready_script(
<<<EOF
$("#v_upgrade_info").html('');
@@ -648,17 +636,17 @@ class Step2ter extends WizardStep
{
return 'Additional Upgrade Info';
}
public function GetPossibleSteps()
{
return array('Step3');
}
public function ProcessParams($bMoveForward = true)
{
return array('class' => 'Step3', 'state' => '');
}
public function Display(WebPage $oPage)
{
$oPage->p('This is Step 2ter! (Upgrade)');
@@ -671,22 +659,22 @@ class Step3 extends WizardStep
{
return 'Installation Complete';
}
public function GetPossibleSteps()
{
return array();
}
public function ProcessParams($bMoveForward = true)
{
return array('class' => '', 'state' => '');
}
public function Display(WebPage $oPage)
{
$oPage->p('This is the FINAL Step');
}
public function CanMoveForward()
{
return false;