| Conditions | 6 |
| Paths | 4 |
| Total Lines | 64 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 109 | protected function getFirstTimeValueForEvent(string $tableDef, int $now): int |
||
| 110 | { |
||
| 111 | $now = (int)$now; |
||
| 112 | $result = PHP_INT_MAX; |
||
| 113 | list($tableName, $pid) = GeneralUtility::trimExplode(':', $tableDef); |
||
| 114 | |||
| 115 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) |
||
| 116 | ->getQueryBuilderForTable($tableName); |
||
| 117 | $queryBuilder->getRestrictions() |
||
| 118 | ->removeByType(StartTimeRestriction::class) |
||
| 119 | ->removeByType(EndTimeRestriction::class); |
||
| 120 | $timeFields = ['registration_startdate', 'registration_deadline']; |
||
| 121 | $timeConditions = $queryBuilder->expr()->orX(); |
||
| 122 | foreach ($timeFields as $field) { |
||
| 123 | $queryBuilder->addSelectLiteral( |
||
| 124 | 'MIN(' |
||
| 125 | . 'CASE WHEN ' |
||
| 126 | . $queryBuilder->expr()->lte( |
||
| 127 | $field, |
||
| 128 | $queryBuilder->createNamedParameter($now, \PDO::PARAM_INT) |
||
| 129 | ) |
||
| 130 | . ' THEN NULL ELSE ' . $queryBuilder->quoteIdentifier($field) . ' END' |
||
| 131 | . ') AS ' . $queryBuilder->quoteIdentifier($field) |
||
| 132 | ); |
||
| 133 | $timeConditions->add( |
||
| 134 | $queryBuilder->expr()->gt( |
||
| 135 | $field, |
||
| 136 | $queryBuilder->createNamedParameter($now, \PDO::PARAM_INT) |
||
| 137 | ) |
||
| 138 | ); |
||
| 139 | } |
||
| 140 | |||
| 141 | // Only consider events where registration is enabled and that have not started yet. |
||
| 142 | // Also include PID and timeConditions |
||
| 143 | $row = $queryBuilder |
||
| 144 | ->from($tableName) |
||
| 145 | ->where( |
||
| 146 | $queryBuilder->expr()->eq( |
||
| 147 | 'pid', |
||
| 148 | $queryBuilder->createNamedParameter($pid, \PDO::PARAM_INT) |
||
| 149 | ), |
||
| 150 | $queryBuilder->expr()->eq( |
||
| 151 | 'enable_registration', |
||
| 152 | $queryBuilder->createNamedParameter(1, \PDO::PARAM_INT) |
||
| 153 | ), |
||
| 154 | $queryBuilder->expr()->gt( |
||
| 155 | 'startdate', |
||
| 156 | $queryBuilder->createNamedParameter($now, \PDO::PARAM_INT) |
||
| 157 | ), |
||
| 158 | $timeConditions |
||
| 159 | ) |
||
| 160 | ->execute() |
||
| 161 | ->fetch(); |
||
| 162 | |||
| 163 | if ($row) { |
||
| 164 | foreach ($timeFields as $timeField) { |
||
| 165 | if ($row[$timeField] !== null && (int)$row[$timeField] > $now) { |
||
| 166 | $result = min($result, (int)$row[$timeField]); |
||
| 167 | } |
||
| 168 | } |
||
| 169 | } |
||
| 170 | |||
| 171 | return $result; |
||
| 172 | } |
||
| 173 | } |
||
| 174 |