@@ -19,16 +19,20 @@ public function __construct(
1919 private Database $ db
2020 ) {
2121 $ this ->ip = filter_input (INPUT_SERVER , 'HTTP_X_FORWARDED_FOR ' ) ?? null ;
22+ $ this ->doLimit ();
23+ }
24+
25+ private function doLimit () : void {
26+ /** @psalm-suppress RiskyTruthyFalsyComparison */
2227 if (! $ this ->ip ) {
28+ /* without a client IP we cannot proceed here */
2329 return ;
2430 }
25- $ this ->doLimit ();
26- }
2731
28- private function doLimit () {
2932 $ data = $ this ->db ->getOne ('SELECT hits
3033 FROM rate_limit
3134 WHERE ip = ? AND timerange = ? ' , $ this ->ip , $ this ->timerange );
35+ /** @psalm-suppress RiskyTruthyFalsyComparison */
3236 if ($ data && $ data ['hits ' ] > $ this ->limit ) {
3337 $ this ->dbExec (
3438 'UPDATE rate_limit
@@ -47,8 +51,11 @@ private function doLimit() {
4751 ' , $ this ->ip , $ this ->timerange );
4852 }
4953
50- private function dbExec ($ query , ...$ params ) {
54+ private function dbExec (string $ query , string | int ...$ params ) : bool {
5155 $ statement = $ this ->db ->prepare ($ query );
56+ if (! $ statement ) {
57+ return false ;
58+ }
5259 return $ statement ->execute ($ params );
5360 }
5461
@@ -57,7 +64,7 @@ private function dbExec($query, ...$params) {
5764 *
5865 * Meant to be run in a cronjob.
5966 */
60- public static function clearStale (Database $ db ) {
67+ public static function clearStale (Database $ db ) : void {
6168 $ db ->query ('DELETE FROM rate_limit WHERE last_seen < DATE_SUB(now(), INTERVAL timerange SECOND) ' );
6269 }
6370
0 commit comments