| Conditions | 9 |
| Paths | 18 |
| Total Lines | 93 |
| Code Lines | 56 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 137 | public function buildExpression(array $queriedTables, ExpressionBuilder $expressionBuilder): CompositeExpression |
||
| 138 | { |
||
| 139 | $constraints = []; |
||
| 140 | |||
| 141 | if ($this->allowedFields) { |
||
| 142 | $constraints = [ |
||
| 143 | $expressionBuilder->orX( |
||
| 144 | // broken link is in page and page is editable |
||
| 145 | $expressionBuilder->andX( |
||
| 146 | $expressionBuilder->eq( |
||
| 147 | 'tx_linkvalidator_link.table_name', |
||
| 148 | $this->queryBuilder->createNamedParameter('pages') |
||
| 149 | ), |
||
| 150 | QueryHelper::stripLogicalOperatorPrefix($GLOBALS['BE_USER']->getPagePermsClause(Permission::PAGE_EDIT)) |
||
| 151 | ), |
||
| 152 | // OR broken link is in content and content is editable |
||
| 153 | $expressionBuilder->andX( |
||
| 154 | $expressionBuilder->neq( |
||
| 155 | 'tx_linkvalidator_link.table_name', |
||
| 156 | $this->queryBuilder->createNamedParameter('pages') |
||
| 157 | ), |
||
| 158 | QueryHelper::stripLogicalOperatorPrefix($GLOBALS['BE_USER']->getPagePermsClause(Permission::CONTENT_EDIT)) |
||
| 159 | ) |
||
| 160 | ) |
||
| 161 | ]; |
||
| 162 | |||
| 163 | // check if fields are editable |
||
| 164 | $additionalWhere = []; |
||
| 165 | foreach ($this->allowedFields as $table => $fields) { |
||
| 166 | foreach ($fields as $field => $value) { |
||
| 167 | $additionalWhere[] = $expressionBuilder->andX( |
||
| 168 | $expressionBuilder->eq( |
||
| 169 | 'tx_linkvalidator_link.table_name', |
||
| 170 | $this->queryBuilder->createNamedParameter($table) |
||
| 171 | ), |
||
| 172 | $expressionBuilder->eq( |
||
| 173 | 'tx_linkvalidator_link.field', |
||
| 174 | $this->queryBuilder->createNamedParameter($field) |
||
| 175 | ) |
||
| 176 | ); |
||
| 177 | } |
||
| 178 | } |
||
| 179 | if ($additionalWhere) { |
||
| 180 | $constraints[] = $expressionBuilder->orX(...$additionalWhere); |
||
| 181 | } |
||
| 182 | } else { |
||
| 183 | // add a constraint that will always return zero records because there are NO allowed fields |
||
| 184 | $constraints[] = $expressionBuilder->isNull('tx_linkvalidator_link.table_name'); |
||
| 185 | } |
||
| 186 | |||
| 187 | foreach ($this->explicitAllowFields as $table => $field) { |
||
| 188 | $additionalWhere = []; |
||
| 189 | $additionalWhere[] = $expressionBuilder->andX( |
||
| 190 | $expressionBuilder->eq( |
||
| 191 | 'tx_linkvalidator_link.table_name', |
||
| 192 | $this->queryBuilder->createNamedParameter($table) |
||
| 193 | ), |
||
| 194 | $expressionBuilder->in( |
||
| 195 | 'tx_linkvalidator_link.element_type', |
||
| 196 | $this->queryBuilder->createNamedParameter( |
||
| 197 | array_unique(current($field)), |
||
| 198 | Connection::PARAM_STR_ARRAY |
||
| 199 | ) |
||
| 200 | ) |
||
| 201 | ); |
||
| 202 | $additionalWhere[] = $expressionBuilder->neq( |
||
| 203 | 'tx_linkvalidator_link.table_name', |
||
| 204 | $this->queryBuilder->createNamedParameter($table) |
||
| 205 | ); |
||
| 206 | if ($additionalWhere) { |
||
| 207 | $constraints[] = $expressionBuilder->orX(...$additionalWhere); |
||
| 208 | } |
||
| 209 | } |
||
| 210 | |||
| 211 | if ($this->allowedLanguages) { |
||
| 212 | $additionalWhere = []; |
||
| 213 | foreach ($this->allowedLanguages as $langId) { |
||
| 214 | $additionalWhere[] = $expressionBuilder->orX( |
||
| 215 | $expressionBuilder->eq( |
||
| 216 | 'tx_linkvalidator_link.language', |
||
| 217 | $this->queryBuilder->createNamedParameter($langId, \PDO::PARAM_INT) |
||
| 218 | ), |
||
| 219 | $expressionBuilder->eq( |
||
| 220 | 'tx_linkvalidator_link.language', |
||
| 221 | $this->queryBuilder->createNamedParameter(-1, \PDO::PARAM_INT) |
||
| 222 | ) |
||
| 223 | ); |
||
| 224 | } |
||
| 225 | $constraints[] = $expressionBuilder->orX(...$additionalWhere); |
||
| 226 | } |
||
| 227 | // If allowed languages is empty: all languages are allowed, so no constraint in this case |
||
| 228 | |||
| 229 | return $expressionBuilder->andX(...$constraints); |
||
| 230 | } |
||
| 232 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.