From 45d13ac998217a20939400fa5367382d03c482af Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Wed, 25 Dec 2019 09:57:11 +0100 Subject: [PATCH] Metrics: Strict types and type hinting --- src/Controllers/Metrics/Stats.php | 94 ++++++++------------ tests/Unit/Controllers/Metrics/StatsTest.php | 9 +- 2 files changed, 43 insertions(+), 60 deletions(-) diff --git a/src/Controllers/Metrics/Stats.php b/src/Controllers/Metrics/Stats.php index 91ee4853..718bcb39 100644 --- a/src/Controllers/Metrics/Stats.php +++ b/src/Controllers/Metrics/Stats.php @@ -1,15 +1,20 @@ getQuery('users') - ->join('users_state', 'user_id', '=', 'id') - ->where('arrived', '=', 1); + $query = State::whereArrived(true); if (!is_null($working)) { // @codeCoverageIgnoreStart $query - ->leftJoin('UserWorkLog', 'UserWorkLog.user_id', '=', 'users.id') - ->leftJoin('ShiftEntry', 'ShiftEntry.UID', '=', 'users.id') + ->leftJoin('UserWorkLog', 'UserWorkLog.user_id', '=', 'users_state.user_id') + ->leftJoin('ShiftEntry', 'ShiftEntry.UID', '=', 'users_state.user_id') ->distinct(); $query->where(function ($query) use ($working) { @@ -62,8 +63,7 @@ class Stats // @codeCoverageIgnoreEnd } - return $query - ->count('users.id'); + return $query->count('users_state.user_id'); } /** @@ -73,11 +73,7 @@ class Stats */ public function newUsers(): int { - return $this - ->getQuery('users') - ->join('users_state', 'user_id', '=', 'id') - ->where('arrived', '=', 0) - ->count(); + return State::whereArrived(false)->count(); } /** @@ -91,11 +87,11 @@ class Stats /** * The number of currently working users * - * @param null $freeloaded + * @param bool|null $freeloaded * @return int * @codeCoverageIgnore */ - public function currentlyWorkingUsers($freeloaded = null): int + public function currentlyWorkingUsers(bool $freeloaded = null): int { $query = $this ->getQuery('users') @@ -116,9 +112,7 @@ class Stats */ public function vouchers(): int { - return $this - ->getQuery('users_state') - ->sum('got_voucher'); + return (int)State::query()->sum('got_voucher'); } /** @@ -126,16 +120,13 @@ class Stats */ public function tshirts(): int { - return $this - ->getQuery('users_state') - ->where('got_shirt', '=', true) - ->count(); + return State::whereGotShirt(true)->count(); } /** - * @return \Illuminate\Support\Collection + * @return Collection */ - public function tshirtSizes() + public function tshirtSizes(): Collection { return $this ->getQuery('users_personal_data') @@ -150,7 +141,7 @@ class Stats * @return int * @codeCoverageIgnore */ - public function licenses($vehicle = null) + public function licenses(string $vehicle = null): int { $mapping = [ 'forklift' => 'has_license_forklift', @@ -178,7 +169,7 @@ class Stats * @return int * @codeCoverageIgnore */ - public function workSeconds($done = null, $freeloaded = null): int + public function workSeconds(bool $done = null, bool $freeloaded = null): int { $query = $this ->getQuery('ShiftEntry') @@ -192,25 +183,25 @@ class Stats $query->where('end', ($done == true ? '<' : '>='), time()); } - return $query->sum($this->raw('end - start')); + return (int)$query->sum($this->raw('end - start')); } /** * @return int * @codeCoverageIgnore */ - public function worklogSeconds() + public function worklogSeconds(): int { - return round($this + return (int)$this ->getQuery('UserWorkLog') - ->sum($this->raw('work_hours * 60*60'))); + ->sum($this->raw('work_hours * 60*60')); } /** * @return int * @codeCoverageIgnore */ - public function shifts() + public function shifts(): int { return $this ->getQuery('Shifts') @@ -218,10 +209,10 @@ class Stats } /** - * @param bool $meeting + * @param bool|null $meeting * @return int */ - public function announcements($meeting = null) + public function announcements(bool $meeting = null): int { $query = is_null($meeting) ? News::query() : News::whereIsMeeting($meeting); @@ -229,10 +220,10 @@ class Stats } /** - * @param bool $answered + * @param bool|null $answered * @return int */ - public function questions($answered = null) + public function questions(bool $answered = null): int { $query = Question::query(); if (!is_null($answered)) { @@ -250,7 +241,7 @@ class Stats * @return int * @codeCoverageIgnore */ - public function messages() + public function messages(): int { return $this ->getQuery('Messages') @@ -260,7 +251,7 @@ class Stats /** * @return int */ - public function sessions() + public function sessions(): int { return $this ->getQuery('sessions') @@ -270,7 +261,7 @@ class Stats /** * @return float */ - public function databaseRead() + public function databaseRead(): float { $start = microtime(true); @@ -282,7 +273,7 @@ class Stats /** * @return float */ - public function databaseWrite() + public function databaseWrite(): float { $config = (new EventConfig())->findOrNew('last_metrics'); $config @@ -297,17 +288,12 @@ class Stats } /** - * @param string $level + * @param string|null $level * @return int */ - public function logEntries($level = null) + public function logEntries(string $level = null): int { - $query = $this - ->getQuery('log_entries'); - - if (!is_null($level)) { - $query->where('level', '=', $level); - } + $query = is_null($level) ? LogEntry::query() : LogEntry::whereLevel($level); return $query->count(); } @@ -315,11 +301,9 @@ class Stats /** * @return int */ - public function passwordResets() + public function passwordResets(): int { - return $this - ->getQuery('password_resets') - ->count(); + return PasswordReset::query()->count(); } /** @@ -337,7 +321,7 @@ class Stats * @param mixed $value * @return QueryExpression */ - protected function raw($value) + protected function raw($value): QueryExpression { return $this->db->getConnection()->raw($value); } diff --git a/tests/Unit/Controllers/Metrics/StatsTest.php b/tests/Unit/Controllers/Metrics/StatsTest.php index 2c974db3..d13c2693 100644 --- a/tests/Unit/Controllers/Metrics/StatsTest.php +++ b/tests/Unit/Controllers/Metrics/StatsTest.php @@ -13,7 +13,6 @@ use Engelsystem\Models\User\State; use Engelsystem\Models\User\User; use Engelsystem\Test\Unit\HasDatabase; use Engelsystem\Test\Unit\TestCase; -use Illuminate\Support\Collection; use Illuminate\Support\Str; use Psr\Log\LogLevel; @@ -23,7 +22,6 @@ class StatsTest extends TestCase /** * @covers \Engelsystem\Controllers\Metrics\Stats::__construct - * @covers \Engelsystem\Controllers\Metrics\Stats::getQuery * @covers \Engelsystem\Controllers\Metrics\Stats::newUsers */ public function testNewUsers() @@ -57,8 +55,9 @@ class StatsTest extends TestCase } /** - * @covers \Engelsystem\Controllers\Metrics\Stats::raw * @covers \Engelsystem\Controllers\Metrics\Stats::tshirtSizes + * @covers \Engelsystem\Controllers\Metrics\Stats::raw + * @covers \Engelsystem\Controllers\Metrics\Stats::getQuery */ public function testTshirtSizes() { @@ -67,10 +66,10 @@ class StatsTest extends TestCase $stats = new Stats($this->database); $sizes = $stats->tshirtSizes(); $this->assertCount(2, $sizes); - $this->assertEquals(new Collection([ + $this->assertEquals([ (object)['shirt_size' => 'L', 'count' => 2], (object)['shirt_size' => 'XXL', 'count' => 1], - ]), $sizes); + ], $sizes->toArray()); }