Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 36 | class SingleTablePersister extends AbstractEntityInheritancePersister |
||
| 37 | { |
||
| 38 | /** |
||
| 39 | * {@inheritdoc} |
||
| 40 | */ |
||
| 41 | 176 | protected function getDiscriminatorColumnTableName() |
|
| 45 | |||
| 46 | /** |
||
| 47 | * {@inheritdoc} |
||
| 48 | */ |
||
| 49 | 42 | protected function getSelectColumnsSQL() |
|
| 50 | { |
||
| 51 | 42 | if ($this->currentPersisterContext->selectColumnListSql !== null) { |
|
| 52 | 20 | return $this->currentPersisterContext->selectColumnListSql; |
|
| 53 | } |
||
| 54 | |||
| 55 | 42 | $columnList[] = parent::getSelectColumnsSQL(); |
|
|
|
|||
| 56 | |||
| 57 | 42 | $rootClass = $this->em->getClassMetadata($this->class->rootEntityName); |
|
| 58 | 42 | $tableAlias = $this->getSQLTableAlias($rootClass->name); |
|
| 59 | |||
| 60 | // Append discriminator column |
||
| 61 | 42 | $discrColumn = $this->class->discriminatorColumn['name']; |
|
| 62 | 42 | $discrColumnType = $this->class->discriminatorColumn['type']; |
|
| 63 | |||
| 64 | 42 | $columnList[] = $tableAlias . '.' . $discrColumn; |
|
| 65 | |||
| 66 | 42 | $resultColumnName = $this->platform->getSQLResultCasing($discrColumn); |
|
| 67 | |||
| 68 | 42 | $this->currentPersisterContext->rsm->setDiscriminatorColumn('r', $resultColumnName); |
|
| 69 | 42 | $this->currentPersisterContext->rsm->addMetaResult('r', $resultColumnName, $discrColumn, false, $discrColumnType); |
|
| 70 | |||
| 71 | // Append subclass columns |
||
| 72 | 42 | View Code Duplication | foreach ($this->class->subClasses as $subClassName) { |
| 73 | 27 | $subClass = $this->em->getClassMetadata($subClassName); |
|
| 74 | |||
| 75 | // Regular columns |
||
| 76 | 27 | foreach ($subClass->fieldMappings as $fieldName => $mapping) { |
|
| 77 | 27 | if (isset($mapping['inherited'])) { |
|
| 78 | 27 | continue; |
|
| 79 | } |
||
| 80 | |||
| 81 | 15 | $columnList[] = $this->getSelectColumnSQL($fieldName, $subClass); |
|
| 82 | } |
||
| 83 | |||
| 84 | // Foreign key columns |
||
| 85 | 27 | foreach ($subClass->associationMappings as $assoc) { |
|
| 86 | 24 | if ( ! $assoc['isOwningSide'] || ! ($assoc['type'] & ClassMetadata::TO_ONE) || isset($assoc['inherited'])) { |
|
| 87 | 23 | continue; |
|
| 88 | } |
||
| 89 | |||
| 90 | 3 | $targetClass = $this->em->getClassMetadata($assoc['targetEntity']); |
|
| 91 | |||
| 92 | 3 | foreach ($assoc['joinColumns'] as $joinColumn) { |
|
| 93 | 3 | $columnList[] = $this->getSelectJoinColumnSQL( |
|
| 94 | 3 | $tableAlias, |
|
| 95 | 3 | $joinColumn['name'], |
|
| 96 | 3 | $this->quoteStrategy->getJoinColumnName($joinColumn, $subClass, $this->platform), |
|
| 97 | 27 | PersisterHelper::getTypeOfColumn($joinColumn['referencedColumnName'], $targetClass, $this->em) |
|
| 98 | ); |
||
| 99 | } |
||
| 100 | } |
||
| 101 | } |
||
| 102 | |||
| 103 | 42 | $this->currentPersisterContext->selectColumnListSql = implode(', ', $columnList); |
|
| 104 | |||
| 105 | 42 | return $this->currentPersisterContext->selectColumnListSql; |
|
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * {@inheritdoc} |
||
| 110 | */ |
||
| 111 | 176 | protected function getInsertColumnList() |
|
| 112 | { |
||
| 113 | 176 | $columns = parent::getInsertColumnList(); |
|
| 114 | |||
| 115 | // Add discriminator column to the INSERT SQL |
||
| 116 | 176 | $columns[] = $this->class->discriminatorColumn['name']; |
|
| 117 | |||
| 118 | 176 | return $columns; |
|
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * {@inheritdoc} |
||
| 123 | */ |
||
| 124 | 44 | protected function getSQLTableAlias($className, $assocName = '') |
|
| 125 | { |
||
| 126 | 44 | return parent::getSQLTableAlias($this->class->rootEntityName, $assocName); |
|
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * {@inheritdoc} |
||
| 131 | */ |
||
| 132 | 42 | protected function getSelectConditionSQL(array $criteria, $assoc = null) |
|
| 133 | { |
||
| 134 | 42 | $conditionSql = parent::getSelectConditionSQL($criteria, $assoc); |
|
| 135 | |||
| 136 | 42 | if ($conditionSql) { |
|
| 137 | 37 | $conditionSql .= ' AND '; |
|
| 138 | } |
||
| 139 | |||
| 140 | 42 | return $conditionSql . $this->getSelectConditionDiscriminatorValueSQL(); |
|
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * {@inheritdoc} |
||
| 145 | */ |
||
| 146 | 4 | protected function getSelectConditionCriteriaSQL(Criteria $criteria) |
|
| 147 | { |
||
| 148 | 4 | $conditionSql = parent::getSelectConditionCriteriaSQL($criteria); |
|
| 149 | |||
| 150 | 3 | if ($conditionSql) { |
|
| 151 | 3 | $conditionSql .= ' AND '; |
|
| 152 | } |
||
| 153 | |||
| 154 | 3 | return $conditionSql . $this->getSelectConditionDiscriminatorValueSQL(); |
|
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @return string |
||
| 159 | */ |
||
| 160 | 44 | protected function getSelectConditionDiscriminatorValueSQL() |
|
| 161 | { |
||
| 162 | 44 | $values = []; |
|
| 163 | |||
| 164 | 44 | if ($this->class->discriminatorValue !== null) { // discriminators can be 0 |
|
| 165 | 30 | $values[] = $this->conn->quote($this->class->discriminatorValue); |
|
| 166 | } |
||
| 167 | |||
| 168 | 44 | $discrValues = array_flip($this->class->discriminatorMap); |
|
| 169 | |||
| 170 | 44 | foreach ($this->class->subClasses as $subclassName) { |
|
| 171 | 28 | $values[] = $this->conn->quote($discrValues[$subclassName]); |
|
| 172 | } |
||
| 173 | |||
| 174 | 44 | $values = implode(', ', $values); |
|
| 175 | 44 | $discColumn = $this->class->discriminatorColumn['name']; |
|
| 176 | 44 | $tableAlias = $this->getSQLTableAlias($this->class->name); |
|
| 177 | |||
| 178 | 44 | return $tableAlias . '.' . $discColumn . ' IN (' . $values . ')'; |
|
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * {@inheritdoc} |
||
| 183 | */ |
||
| 184 | 44 | protected function generateFilterConditionSQL(ClassMetadata $targetEntity, $targetTableAlias) |
|
| 192 | } |
||
| 193 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.