Conditions | 7 |
Paths | 8 |
Total Lines | 52 |
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 | public function setOnline(string $id, bool $loggedIn): void |
||
110 | { |
||
111 | $now = time(); |
||
112 | $id = $this->getShortendedId((string)$id); |
||
113 | |||
114 | $user = $this->find()->where(['uuid' => $id])->first(); |
||
115 | |||
116 | if ($user) { |
||
117 | /// [Performance] Only hit database if timestamp is about to get outdated. |
||
118 | $updateIfOlderThan = $now - (int)$this->timeUntilOffline * 0.75; |
||
119 | if ($user->get('time') < $updateIfOlderThan) { |
||
120 | $user->set('time', $now); |
||
121 | $this->save($user); |
||
122 | } |
||
123 | |||
124 | return; |
||
125 | } |
||
126 | |||
127 | $data = ['logged_in' => $loggedIn, 'time' => $now, 'uuid' => $id]; |
||
128 | if ($loggedIn) { |
||
129 | $data['user_id'] = (int)$id; |
||
130 | } |
||
131 | $user = $this->newEntity($data); |
||
132 | |||
133 | try { |
||
134 | $this->save($user); |
||
135 | } catch (\PDOException $e) { |
||
136 | // We saw that some mobile browsers occasionaly send two requests at |
||
137 | // the same time. On of the two requests was always the status-ping. |
||
138 | // Working theory: cause is a power-coalesced status-ping now |
||
139 | // bundled with a page reload on tab-"resume" esp. with http/2. |
||
140 | // |
||
141 | // When the second request arrives (ns later) the first request |
||
142 | // hasn't persistet its save to the DB yet (which happens in the ms |
||
143 | // range). So the second request doesn't see the user online and |
||
144 | // tries to save the same "uuid" again. The DB will not have any of |
||
145 | // that nonsense after it set the "uuid" from the first request on a |
||
146 | // unique column, and raises an error which may be experienced by |
||
147 | // the user. |
||
148 | // |
||
149 | // Since the first request did the necessary work of marking the |
||
150 | // user online, we suppress this error, assuming it will only happen |
||
151 | // in this particular situation. *knocks on wood* |
||
152 | if ($e->getCode() == 23000 && strstr($e->getMessage(), 'uuid')) { |
||
153 | $this->log( |
||
154 | sprintf('Cought duplicate uuid-key %s exception in UserOnline::setOnline.', $id), |
||
155 | LogLevel::INFO, |
||
156 | 'saito.info' |
||
157 | ); |
||
158 | } |
||
159 | } |
||
160 | } |
||
161 | |||
225 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.