mirror of
https://github.com/Combodo/iTop.git
synced 2026-04-24 11:08:45 +02:00
N°8796 - Add PHP code style validation in iTop and extensions - format whole code base
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2024 Combodo SAS
|
||||
* @license http://opensource.org/licenses/AGPL-3.0
|
||||
*/
|
||||
|
||||
|
||||
class OQLClassTreeBuilder
|
||||
{
|
||||
/** @var \DBObjectSearch */
|
||||
@@ -29,8 +29,7 @@ class OQLClassTreeBuilder
|
||||
$this->oDBObjectSearch = $oDBObjetSearch;
|
||||
$this->sClass = $oDBObjetSearch->GetFirstJoinedClass();
|
||||
$this->sClassAlias = $oDBObjetSearch->GetFirstJoinedClassAlias();
|
||||
if (empty($this->sClassAlias))
|
||||
{
|
||||
if (empty($this->sClassAlias)) {
|
||||
$this->sClassAlias = $oBuild->GetEmptyClassAlias();
|
||||
}
|
||||
$this->oOQLClassNode = new OQLClassNode($oBuild, $this->sClass, $this->sClassAlias);
|
||||
@@ -89,10 +88,8 @@ class OQLClassTreeBuilder
|
||||
*/
|
||||
private function AddExternalKeysFromSearch()
|
||||
{
|
||||
foreach ($this->oDBObjectSearch->GetCriteria_PointingTo() as $sKeyAttCode => $aPointingTo)
|
||||
{
|
||||
if (array_key_exists(TREE_OPERATOR_EQUALS, $aPointingTo))
|
||||
{
|
||||
foreach ($this->oDBObjectSearch->GetCriteria_PointingTo() as $sKeyAttCode => $aPointingTo) {
|
||||
if (array_key_exists(TREE_OPERATOR_EQUALS, $aPointingTo)) {
|
||||
$this->oOQLClassNode->AddExternalKey($sKeyAttCode);
|
||||
}
|
||||
}
|
||||
@@ -109,69 +106,53 @@ class OQLClassTreeBuilder
|
||||
// array of (attcode => fieldexpression)
|
||||
$aExpectedAttributes = $this->oBuild->m_oQBExpressions->GetUnresolvedFields($this->sClassAlias);
|
||||
|
||||
$aPolymorphicJoinAlias = array(); // array of (subclass => alias)
|
||||
foreach ($aExpectedAttributes as $sExpectedAttCode => $oExpression)
|
||||
{
|
||||
if (!MetaModel::IsValidAttCode($this->sClass, $sExpectedAttCode))
|
||||
{
|
||||
$aPolymorphicJoinAlias = []; // array of (subclass => alias)
|
||||
foreach ($aExpectedAttributes as $sExpectedAttCode => $oExpression) {
|
||||
if (!MetaModel::IsValidAttCode($this->sClass, $sExpectedAttCode)) {
|
||||
continue;
|
||||
}
|
||||
$oAttDef = MetaModel::GetAttributeDef($this->sClass, $sExpectedAttCode);
|
||||
if ($oAttDef->IsBasedOnOQLExpression())
|
||||
{
|
||||
if ($oAttDef->IsBasedOnOQLExpression()) {
|
||||
// To optimize: detect a restriction on child classes in the condition expression
|
||||
// e.g. SELECT FunctionalCI WHERE finalclass IN ('Server', 'VirtualMachine')
|
||||
$oExpression = DBObjectSearch::GetPolymorphicExpression($this->sClass, $sExpectedAttCode);
|
||||
|
||||
$aRequiredFields = array();
|
||||
$aRequiredFields = [];
|
||||
$oExpression->GetUnresolvedFields('', $aRequiredFields);
|
||||
$aTranslateFields = array();
|
||||
foreach ($aRequiredFields as $sSubClass => $aFields)
|
||||
{
|
||||
foreach ($aFields as $sAttCode => $oField)
|
||||
{
|
||||
$aTranslateFields = [];
|
||||
foreach ($aRequiredFields as $sSubClass => $aFields) {
|
||||
foreach ($aFields as $sAttCode => $oField) {
|
||||
$oAttDef = MetaModel::GetAttributeDef($sSubClass, $sAttCode);
|
||||
if ($oAttDef->IsExternalKey())
|
||||
{
|
||||
if ($oAttDef->IsExternalKey()) {
|
||||
$sClassOfAttribute = MetaModel::GetAttributeOrigin($sSubClass, $sAttCode);
|
||||
if (MetaModel::IsParentClass($sClassOfAttribute, $this->sClass))
|
||||
{
|
||||
if (MetaModel::IsParentClass($sClassOfAttribute, $this->sClass)) {
|
||||
$this->oOQLClassNode->AddExternalKey($sAttCode);
|
||||
}
|
||||
}
|
||||
elseif ($oAttDef->IsExternalField())
|
||||
{
|
||||
} elseif ($oAttDef->IsExternalField()) {
|
||||
$sKeyAttCode = $oAttDef->GetKeyAttCode();
|
||||
$sClassOfAttribute = MetaModel::GetAttributeOrigin($sSubClass, $sKeyAttCode);
|
||||
if (MetaModel::IsParentClass($sClassOfAttribute, $this->sClass))
|
||||
{
|
||||
if (MetaModel::IsParentClass($sClassOfAttribute, $this->sClass)) {
|
||||
$this->oOQLClassNode->AddExternalField($sKeyAttCode, $sAttCode, $oAttDef);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
$sClassOfAttribute = MetaModel::GetAttributeOrigin($sSubClass, $sAttCode);
|
||||
}
|
||||
|
||||
if (MetaModel::IsParentClass($sClassOfAttribute, $this->sClass))
|
||||
{
|
||||
if (MetaModel::IsParentClass($sClassOfAttribute, $this->sClass)) {
|
||||
// The attribute is part of the standard query
|
||||
//
|
||||
$sAliasForAttribute = $this->sClassAlias;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
// The attribute will be available from an additional outer join
|
||||
// For each subclass (table) one single join is enough
|
||||
//
|
||||
if (!array_key_exists($sClassOfAttribute, $aPolymorphicJoinAlias))
|
||||
{
|
||||
$sAliasForAttribute = $this->oBuild->GenerateClassAlias($this->sClassAlias.'_poly_'.$sClassOfAttribute,
|
||||
$sClassOfAttribute);
|
||||
if (!array_key_exists($sClassOfAttribute, $aPolymorphicJoinAlias)) {
|
||||
$sAliasForAttribute = $this->oBuild->GenerateClassAlias(
|
||||
$this->sClassAlias.'_poly_'.$sClassOfAttribute,
|
||||
$sClassOfAttribute
|
||||
);
|
||||
$aPolymorphicJoinAlias[$sClassOfAttribute] = $sAliasForAttribute;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
$sAliasForAttribute = $aPolymorphicJoinAlias[$sClassOfAttribute];
|
||||
}
|
||||
}
|
||||
@@ -181,7 +162,7 @@ class OQLClassTreeBuilder
|
||||
}
|
||||
$oExpression = $oExpression->Translate($aTranslateFields, false);
|
||||
|
||||
$aTranslateNow = array();
|
||||
$aTranslateNow = [];
|
||||
$aTranslateNow[$this->sClassAlias][$sExpectedAttCode] = $oExpression;
|
||||
$this->oBuild->m_oQBExpressions->Translate($aTranslateNow, false);
|
||||
}
|
||||
@@ -200,12 +181,9 @@ class OQLClassTreeBuilder
|
||||
// array of (attcode => fieldexpression)
|
||||
$aExpectedAttributes = $this->oBuild->m_oQBExpressions->GetUnresolvedFields($this->sClassAlias);
|
||||
|
||||
foreach (MetaModel::ListAttributeDefs($this->sClass) as $sAttCode => $oAttDef)
|
||||
{
|
||||
if ($oAttDef->IsExternalField())
|
||||
{
|
||||
if (array_key_exists($sAttCode, $aExpectedAttributes))
|
||||
{
|
||||
foreach (MetaModel::ListAttributeDefs($this->sClass) as $sAttCode => $oAttDef) {
|
||||
if ($oAttDef->IsExternalField()) {
|
||||
if (array_key_exists($sAttCode, $aExpectedAttributes)) {
|
||||
// Add the external attribute
|
||||
$sKeyAttCode = $oAttDef->GetKeyAttCode();
|
||||
$this->oOQLClassNode->AddExternalField($sKeyAttCode, $sAttCode, $oAttDef);
|
||||
@@ -225,16 +203,13 @@ class OQLClassTreeBuilder
|
||||
$aAllPointingTo = $this->oDBObjectSearch->GetCriteria_PointingTo();
|
||||
|
||||
// Add filters from external keys
|
||||
foreach (array_keys($this->oOQLClassNode->GetExternalKeys()) as $sKeyAttCode)
|
||||
{
|
||||
if (!MetaModel::IsValidAttCode($this->sClass, $sKeyAttCode))
|
||||
{
|
||||
foreach (array_keys($this->oOQLClassNode->GetExternalKeys()) as $sKeyAttCode) {
|
||||
if (!MetaModel::IsValidAttCode($this->sClass, $sKeyAttCode)) {
|
||||
continue;
|
||||
} // Not defined in the class, skip it
|
||||
$oKeyAttDef = MetaModel::GetAttributeDef($this->sClass, $sKeyAttCode);
|
||||
$aPointingTo = isset($aAllPointingTo[$sKeyAttCode]) ? $aAllPointingTo[$sKeyAttCode] : array();
|
||||
if (!array_key_exists(TREE_OPERATOR_EQUALS, $aPointingTo))
|
||||
{
|
||||
$aPointingTo = isset($aAllPointingTo[$sKeyAttCode]) ? $aAllPointingTo[$sKeyAttCode] : [];
|
||||
if (!array_key_exists(TREE_OPERATOR_EQUALS, $aPointingTo)) {
|
||||
// The join was not explicitly defined in the filter,
|
||||
// we need to do it now
|
||||
$sKeyClass = $oKeyAttDef->GetTargetClass();
|
||||
@@ -246,14 +221,10 @@ class OQLClassTreeBuilder
|
||||
}
|
||||
|
||||
$oQBContextExpressions = $this->oBuild->m_oQBExpressions;
|
||||
foreach ($aAllPointingTo as $sKeyAttCode => $aPointingTo)
|
||||
{
|
||||
foreach ($aPointingTo as $iOperatorCode => $aFilter)
|
||||
{
|
||||
foreach ($aFilter as $oExtFilter)
|
||||
{
|
||||
if (!MetaModel::IsValidAttCode($this->sClass, $sKeyAttCode))
|
||||
{
|
||||
foreach ($aAllPointingTo as $sKeyAttCode => $aPointingTo) {
|
||||
foreach ($aPointingTo as $iOperatorCode => $aFilter) {
|
||||
foreach ($aFilter as $oExtFilter) {
|
||||
if (!MetaModel::IsValidAttCode($this->sClass, $sKeyAttCode)) {
|
||||
continue;
|
||||
} // Not defined in the class, skip it
|
||||
// The aliases should not conflict because normalization occurred while building the filter
|
||||
@@ -263,34 +234,29 @@ class OQLClassTreeBuilder
|
||||
|
||||
// Note: there is no search condition in $oExtFilter, because normalization did merge the condition onto the top of the filter tree
|
||||
|
||||
if ($iOperatorCode == TREE_OPERATOR_EQUALS)
|
||||
{
|
||||
if ($this->oOQLClassNode->HasExternalKey($sKeyAttCode))
|
||||
{
|
||||
if ($iOperatorCode == TREE_OPERATOR_EQUALS) {
|
||||
if ($this->oOQLClassNode->HasExternalKey($sKeyAttCode)) {
|
||||
// Specify expected attributes for the target class query
|
||||
// ... and use the current alias !
|
||||
$aTranslateNow = array(); // Translation for external fields - must be performed before the join is done (recursion...)
|
||||
foreach ($this->oOQLClassNode->GetExternalKey($sKeyAttCode) as $sAttCode => $oAtt)
|
||||
{
|
||||
$aTranslateNow = []; // Translation for external fields - must be performed before the join is done (recursion...)
|
||||
foreach ($this->oOQLClassNode->GetExternalKey($sKeyAttCode) as $sAttCode => $oAtt) {
|
||||
$oExtAttDef = $oAtt->GetExtAttDef();
|
||||
if ($oExtAttDef->IsBasedOnOQLExpression())
|
||||
{
|
||||
if ($oExtAttDef->IsBasedOnOQLExpression()) {
|
||||
$sExtAttCode = $oExtAttDef->GetCode();
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
$sExtAttCode = $oAtt->GetExtAttCode();
|
||||
}
|
||||
// Translate mainclass.extfield => remoteclassalias.remotefieldcode
|
||||
$aTranslateNow[$this->sClassAlias][$sAttCode] = new FieldExpression($sExtAttCode, $sKeyClassAlias);
|
||||
}
|
||||
|
||||
if ($oKeyAttDef instanceof AttributeObjectKey)
|
||||
{
|
||||
if ($oKeyAttDef instanceof AttributeObjectKey) {
|
||||
// Add the condition: `$sTargetAlias`.$sClassAttCode IN (subclasses of $sKeyClass')
|
||||
$sClassAttCode = $oKeyAttDef->Get('class_attcode');
|
||||
$oClassListExpr = ListExpression::FromScalars(MetaModel::EnumChildClasses($sKeyClass,
|
||||
ENUM_CHILD_CLASSES_ALL));
|
||||
$oClassListExpr = ListExpression::FromScalars(MetaModel::EnumChildClasses(
|
||||
$sKeyClass,
|
||||
ENUM_CHILD_CLASSES_ALL
|
||||
));
|
||||
$oClassExpr = new FieldExpression($sClassAttCode, $this->sClassAlias);
|
||||
$oClassRestriction = new BinaryExpression($oClassExpr, 'IN', $oClassListExpr);
|
||||
$oQBContextExpressions->AddCondition($oClassRestriction);
|
||||
@@ -304,24 +270,18 @@ class OQLClassTreeBuilder
|
||||
$oOQLClassTreeBuilder = new OQLClassTreeBuilder($oExtFilter, $this->oBuild);
|
||||
$oSelectExtKey = $oOQLClassTreeBuilder->DevelopOQLClassNode();
|
||||
|
||||
if ($oKeyAttDef->IsNullAllowed())
|
||||
{
|
||||
if ($oKeyAttDef->IsNullAllowed()) {
|
||||
$this->oOQLClassNode->AddLeftJoin($oSelectExtKey, $sKeyAttCode, $sExternalKeyField, true);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
$this->oOQLClassNode->AddInnerJoin($oSelectExtKey, $sKeyAttCode, $sExternalKeyField, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
elseif (MetaModel::GetAttributeOrigin($sKeyClass, $sKeyAttCode) == $this->sClass)
|
||||
{
|
||||
} elseif (MetaModel::GetAttributeOrigin($sKeyClass, $sKeyAttCode) == $this->sClass) {
|
||||
$sExternalKeyField = $sKeyAttCode;
|
||||
|
||||
$oOQLClassTreeBuilder = new OQLClassTreeBuilder($oExtFilter, $this->oBuild);
|
||||
$oSelectExtKey = $oOQLClassTreeBuilder->DevelopOQLClassNode();
|
||||
|
||||
|
||||
$this->oOQLClassNode->AddInnerJoinTree($oSelectExtKey, $sKeyAttCode, $sExternalKeyField, true, $iOperatorCode);
|
||||
}
|
||||
}
|
||||
@@ -336,25 +296,22 @@ class OQLClassTreeBuilder
|
||||
*/
|
||||
private function JoinClassesReferencedBy()
|
||||
{
|
||||
foreach ($this->oDBObjectSearch->GetCriteria_ReferencedBy() as $sForeignClass => $aReferences)
|
||||
{
|
||||
foreach ($aReferences as $sForeignExtKeyAttCode => $aFiltersByOperator)
|
||||
{
|
||||
foreach ($aFiltersByOperator as $iOperatorCode => $aFilters)
|
||||
{
|
||||
foreach ($aFilters as $oForeignFilter)
|
||||
{
|
||||
foreach ($this->oDBObjectSearch->GetCriteria_ReferencedBy() as $sForeignClass => $aReferences) {
|
||||
foreach ($aReferences as $sForeignExtKeyAttCode => $aFiltersByOperator) {
|
||||
foreach ($aFiltersByOperator as $iOperatorCode => $aFilters) {
|
||||
foreach ($aFilters as $oForeignFilter) {
|
||||
$oForeignKeyAttDef = MetaModel::GetAttributeDef($sForeignClass, $sForeignExtKeyAttCode);
|
||||
|
||||
$sForeignClassAlias = $oForeignFilter->GetFirstJoinedClassAlias();
|
||||
|
||||
if ($oForeignKeyAttDef instanceof AttributeObjectKey)
|
||||
{
|
||||
if ($oForeignKeyAttDef instanceof AttributeObjectKey) {
|
||||
$sClassAttCode = $oForeignKeyAttDef->Get('class_attcode');
|
||||
|
||||
// Add the condition: `$sForeignClassAlias`.$sClassAttCode IN (subclasses of $sClass')
|
||||
$oClassListExpr = ListExpression::FromScalars(MetaModel::EnumChildClasses($this->sClass,
|
||||
ENUM_CHILD_CLASSES_ALL));
|
||||
$oClassListExpr = ListExpression::FromScalars(MetaModel::EnumChildClasses(
|
||||
$this->sClass,
|
||||
ENUM_CHILD_CLASSES_ALL
|
||||
));
|
||||
$oClassExpr = new FieldExpression($sClassAttCode, $sForeignClassAlias);
|
||||
$oClassRestriction = new BinaryExpression($oClassExpr, 'IN', $oClassListExpr);
|
||||
$this->oBuild->m_oQBExpressions->AddCondition($oClassRestriction);
|
||||
@@ -363,12 +320,9 @@ class OQLClassTreeBuilder
|
||||
$oOQLClassTreeBuilder = new OQLClassTreeBuilder($oForeignFilter, $this->oBuild);
|
||||
$oSelectForeign = $oOQLClassTreeBuilder->DevelopOQLClassNode();
|
||||
|
||||
if ($iOperatorCode == TREE_OPERATOR_EQUALS)
|
||||
{
|
||||
if ($iOperatorCode == TREE_OPERATOR_EQUALS) {
|
||||
$this->oOQLClassNode->AddInnerJoin($oSelectForeign, 'id', $sForeignExtKeyAttCode, false);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
// Hierarchical key
|
||||
$this->oOQLClassNode->AddInnerJoinTree($oSelectForeign, $sForeignExtKeyAttCode, $sForeignExtKeyAttCode, false, $iOperatorCode, true);
|
||||
}
|
||||
@@ -387,8 +341,7 @@ class OQLClassTreeBuilder
|
||||
*/
|
||||
private function JoinClassesForPolymorphicExpressions($aPolymorphicJoinAlias)
|
||||
{
|
||||
foreach ($aPolymorphicJoinAlias as $sSubClass => $sSubClassAlias)
|
||||
{
|
||||
foreach ($aPolymorphicJoinAlias as $sSubClass => $sSubClassAlias) {
|
||||
$oSubClassFilter = new DBObjectSearch($sSubClass, $sSubClassAlias);
|
||||
$oOQLClassTreeBuilder = new OQLClassTreeBuilder($oSubClassFilter, $this->oBuild);
|
||||
$oSelectPoly = $oOQLClassTreeBuilder->DevelopOQLClassNode();
|
||||
|
||||
Reference in New Issue
Block a user