mirror of
https://github.com/Combodo/iTop.git
synced 2026-04-24 02:58:43 +02:00
N°8796 - Add PHP code style validation in iTop and extensions - format whole code base
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Copyright (C) 2013-2024 Combodo SAS
|
||||
*
|
||||
@@ -41,17 +42,14 @@ function GraphvizEscape($s)
|
||||
/**
|
||||
* Helper to generate a Graphviz code for displaying the life cycle of a class
|
||||
* @param string $sClass The class to display
|
||||
* @return string The Graph description in Graphviz/Dot syntax
|
||||
* @return string The Graph description in Graphviz/Dot syntax
|
||||
*/
|
||||
function GraphvizLifecycle($sClass)
|
||||
{
|
||||
$sDotFileContent = "";
|
||||
if (!MetaModel::HasLifecycle($sClass))
|
||||
{
|
||||
if (!MetaModel::HasLifecycle($sClass)) {
|
||||
//$oPage->p("no lifecycle for this class");
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
$aStates = MetaModel::EnumStates($sClass);
|
||||
$aStimuli = MetaModel::EnumStimuli($sClass);
|
||||
$sDotFileContent .= "digraph finite_state_machine {
|
||||
@@ -60,18 +58,15 @@ function GraphvizLifecycle($sClass)
|
||||
node [ fontname=Verdana style=filled fillcolor=\"#ffffff\" ];
|
||||
edge [ fontname=Verdana ];
|
||||
";
|
||||
$aStatesLinks = array();
|
||||
foreach ($aStates as $sStateCode => $aStateDef)
|
||||
{
|
||||
$aStatesLinks[$sStateCode] = array('in' => 0, 'out' => 0);
|
||||
$aStatesLinks = [];
|
||||
foreach ($aStates as $sStateCode => $aStateDef) {
|
||||
$aStatesLinks[$sStateCode] = ['in' => 0, 'out' => 0];
|
||||
}
|
||||
|
||||
foreach ($aStates as $sStateCode => $aStateDef)
|
||||
{
|
||||
|
||||
foreach ($aStates as $sStateCode => $aStateDef) {
|
||||
$sStateLabel = MetaModel::GetStateLabel($sClass, $sStateCode);
|
||||
$sStateDescription = MetaModel::GetStateDescription($sClass, $sStateCode);
|
||||
foreach(MetaModel::EnumTransitions($sClass, $sStateCode) as $sStimulusCode => $aTransitionDef)
|
||||
{
|
||||
foreach (MetaModel::EnumTransitions($sClass, $sStateCode) as $sStimulusCode => $aTransitionDef) {
|
||||
$aStatesLinks[$sStateCode]['out']++;
|
||||
$aStatesLinks[$aTransitionDef['target_state']]['in']++;
|
||||
$sStimulusLabel = $aStimuli[$sStimulusCode]->GetLabel();
|
||||
@@ -79,19 +74,14 @@ function GraphvizLifecycle($sClass)
|
||||
$sDotFileContent .= "\t$sStateCode -> {$aTransitionDef['target_state']} [ label=\"".GraphvizEscape($sStimulusLabel)."\"];\n";
|
||||
}
|
||||
}
|
||||
foreach($aStates as $sStateCode => $aStateDef)
|
||||
{
|
||||
if (($aStatesLinks[$sStateCode]['out'] > 0) || ($aStatesLinks[$sStateCode]['in'] > 0))
|
||||
{
|
||||
foreach ($aStates as $sStateCode => $aStateDef) {
|
||||
if (($aStatesLinks[$sStateCode]['out'] > 0) || ($aStatesLinks[$sStateCode]['in'] > 0)) {
|
||||
// Show only reachable states
|
||||
$sStateLabel = str_replace(' ', '\n', MetaModel::GetStateLabel($sClass, $sStateCode));
|
||||
if ( ($aStatesLinks[$sStateCode]['in'] == 0) || ($aStatesLinks[$sStateCode]['out'] == 0))
|
||||
{
|
||||
if (($aStatesLinks[$sStateCode]['in'] == 0) || ($aStatesLinks[$sStateCode]['out'] == 0)) {
|
||||
// End or Start state, make it look different
|
||||
$sDotFileContent .= "\t$sStateCode [ shape=doublecircle,label=\"".GraphvizEscape($sStateLabel)."\"];\n";
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
$sDotFileContent .= "\t$sStateCode [ shape=circle,label=\"".GraphvizEscape($sStateLabel)."\"];\n";
|
||||
}
|
||||
}
|
||||
@@ -108,39 +98,33 @@ $sModuleDir = dirname($sDeclarationFile);
|
||||
|
||||
$sImageFilePath = $sModuleDir."/lifecycle/".$sClass.".png";
|
||||
$sDotExecutable = MetaModel::GetConfig()->Get('graphviz_path');
|
||||
if (file_exists($sDotExecutable))
|
||||
{
|
||||
if (file_exists($sDotExecutable)) {
|
||||
// create the file with Graphviz
|
||||
$sImageFilePath = utils::GetDataPath()."lifecycle/".$sClass.".svg";
|
||||
if (!is_dir(utils::GetDataPath()))
|
||||
{
|
||||
if (!is_dir(utils::GetDataPath())) {
|
||||
@mkdir(utils::GetDataPath());
|
||||
}
|
||||
if (!is_dir(utils::GetDataPath()."lifecycle"))
|
||||
{
|
||||
if (!is_dir(utils::GetDataPath()."lifecycle")) {
|
||||
@mkdir(utils::GetDataPath()."lifecycle");
|
||||
}
|
||||
$sDotDescription = GraphvizLifecycle($sClass);
|
||||
$sDotFilePath = utils::GetDataPath()."lifecycle/{$sClass}.dot";
|
||||
|
||||
|
||||
$rFile = @fopen($sDotFilePath, "w");
|
||||
@fwrite($rFile, $sDotDescription);
|
||||
@fclose($rFile);
|
||||
$aOutput = array();
|
||||
$aOutput = [];
|
||||
$CommandLine = "\"$sDotExecutable\" -v -Tsvg < \"$sDotFilePath\" -o \"$sImageFilePath\" 2>&1";
|
||||
|
||||
|
||||
exec($CommandLine, $aOutput, $iRetCode);
|
||||
if ($iRetCode != 0)
|
||||
{
|
||||
if ($iRetCode != 0) {
|
||||
header('Content-type: text/html');
|
||||
echo "<p><b>Error:</b></p>";
|
||||
echo "<p>The command: <pre>$CommandLine</pre> returned $iRetCode</p>";
|
||||
echo "<p>The output of the command is:<pre>\n".implode("\n", $aOutput)."</pre></p>";
|
||||
echo "<hr>";
|
||||
echo "<p>Content of the '".basename($sDotFilePath)."' file:<pre>\n$sDotDescription</pre>";
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
header('Content-type: image/svg+xml');
|
||||
header('Content-Disposition: inline; filename="'.$sClass.'.svg"');
|
||||
readfile($sImageFilePath);
|
||||
@@ -148,9 +132,7 @@ if (file_exists($sDotExecutable))
|
||||
@unlink($sDotFilePath);
|
||||
// Image file is removed as well as there is no cache system yet
|
||||
@unlink($sImageFilePath);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
header('Content-type: image/png');
|
||||
header('Content-Disposition: inline; filename="'.$sClass.'.png"');
|
||||
readfile($sImageFilePath);
|
||||
|
||||
Reference in New Issue
Block a user