| Conditions | 6 |
| Paths | 9 |
| Total Lines | 87 |
| Code Lines | 41 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 9 | ||
| Bugs | 0 | Features | 2 |
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 |
||
| 70 | public function collect() |
||
| 71 | { |
||
| 72 | $timeNow = microtime(true); |
||
| 73 | |||
| 74 | // check if record already exists and get current values |
||
| 75 | try { |
||
| 76 | $query = ' |
||
| 77 | SELECT requestNum, expired |
||
| 78 | FROM ' . $this->getTableName() . ' |
||
| 79 | WHERE `key` = :key |
||
| 80 | FOR UPDATE'; |
||
| 81 | |||
| 82 | $stmt = $this->pdo->prepare($query); |
||
| 83 | $result = @$stmt->execute(array( |
||
| 84 | ':key' => $this->key, |
||
| 85 | )); |
||
| 86 | |||
| 87 | if(!$result) { |
||
| 88 | throw new \PDOException('Table not exists'); |
||
| 89 | } |
||
| 90 | |||
| 91 | |||
| 92 | } catch (\PDOException $e) { |
||
| 93 | // table yet not created |
||
| 94 | $this->createTable(); |
||
| 95 | $this->collect(); |
||
| 96 | return; |
||
| 97 | } |
||
| 98 | |||
| 99 | $row = $stmt->fetch(\PDO::FETCH_ASSOC); |
||
| 100 | |||
| 101 | if(!$row) { |
||
| 102 | // record not exists |
||
| 103 | $query = ' |
||
| 104 | INSERT INTO ' . $this->getTableName() . '(`key`, `requestNum`, `expired`) |
||
| 105 | VALUES (:key, 1, :expired) |
||
| 106 | ON DUPLICATE KEY UPDATE requestNum = requestNum + 1 |
||
| 107 | '; |
||
| 108 | |||
| 109 | $stmt = $this->pdo->prepare($query); |
||
| 110 | $stmt->bindValue(':key', $this->key); |
||
| 111 | $stmt->bindValue(':expired', $timeNow + $this->timeInterval); |
||
| 112 | $stmt->execute(); |
||
| 113 | |||
| 114 | return; |
||
| 115 | |||
| 116 | } |
||
| 117 | |||
| 118 | $expiredTimestamp = (float) $row['expired']; |
||
| 119 | |||
| 120 | if($timeNow <= $expiredTimestamp) { |
||
| 121 | // in time slot - increment |
||
| 122 | $query = ' |
||
| 123 | UPDATE ' . $this->getTableName() . ' |
||
| 124 | SET requestNum = requestNum + 1 |
||
| 125 | WHERE `key` = :key |
||
| 126 | '; |
||
| 127 | $parameters = array( |
||
| 128 | ':key' => $this->key, |
||
| 129 | ); |
||
| 130 | } else { |
||
| 131 | //outside time slot - set new |
||
| 132 | $query = ' |
||
| 133 | UPDATE ' . $this->getTableName() . ' |
||
| 134 | SET |
||
| 135 | requestNum = 1, |
||
| 136 | expired = :expired |
||
| 137 | WHERE `key` = :key |
||
| 138 | '; |
||
| 139 | $parameters = array( |
||
| 140 | ':key' => $this->key, |
||
| 141 | ':expired' => $timeNow + $this->timeInterval, |
||
| 142 | ); |
||
| 143 | } |
||
| 144 | |||
| 145 | $stmt = $this->pdo->prepare($query); |
||
| 146 | $stmt->execute($parameters); |
||
| 147 | |||
| 148 | |||
| 149 | // garbage collector |
||
| 150 | if($timeNow % $this->gcCheckInterval === 0) { |
||
| 151 | $query = ' |
||
|
|
|||
| 152 | DELETE FROM ' . $this->getTableName() . ' |
||
| 153 | WHERE expired < ' . ($timeNow - $this->gcSessionInterval); |
||
| 154 | } |
||
| 155 | |||
| 156 | } |
||
| 157 | } |
||
| 158 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.