Skip to content

Commit 4639f6b

Browse files
authored
Merge pull request #4 from ilzrv/lib_revision
Revision of the entire library
2 parents 08c3ceb + 948e36a commit 4639f6b

File tree

8 files changed

+104
-120
lines changed

8 files changed

+104
-120
lines changed

.github/workflows/tests.yml

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,40 +9,8 @@ jobs:
99

1010
strategy:
1111
matrix:
12-
php: [7.1, 7.2, 7.3, 7.4, 8.0]
13-
laravel: [5.6.*, 5.7.*, 5.8.*, 6.*, 7.*, 8.*]
14-
include:
15-
- laravel: 5.6.*
16-
testbench: 3.6.*
17-
- laravel: 5.7.*
18-
testbench: 3.7.*
19-
- laravel: 5.8.*
20-
testbench: 3.8.*
21-
- laravel: 6.*
22-
testbench: 4.*
23-
- laravel: 7.*
24-
testbench: 5.*
25-
- laravel: 8.*
26-
testbench: 6.*
27-
exclude:
28-
- laravel: 5.6.*
29-
php: 8.0
30-
- laravel: 5.7.*
31-
php: 8.0
32-
- laravel: 5.8.*
33-
php: 8.0
34-
- laravel: 6.*
35-
php: 7.1
36-
- laravel: 7.*
37-
php: 7.1
38-
- laravel: 8.*
39-
php: 7.1
40-
- laravel: 6.*
41-
php: 7.2
42-
- laravel: 7.*
43-
php: 7.2
44-
- laravel: 8.*
45-
php: 7.2
12+
php: [8.0, 8.1]
13+
laravel: [9.*]
4614

4715
steps:
4816
- name: Checkout Code
@@ -55,7 +23,7 @@ jobs:
5523

5624
- name: Install Dependencies
5725
run: |
58-
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
26+
composer require "laravel/framework:${{ matrix.laravel }}" orchestra/testbench --no-interaction --no-update
5927
composer update --prefer-dist --no-interaction --no-progress --no-suggest
6028
6129
- name: Execute tests

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ This package allows you to detect controller methods in the code that make a lot
88

99
## Requirements
1010

11-
* Laravel 5.6+
11+
* PHP 8.0 / 8.1
12+
* Laravel 9
1213

1314
## Installation
1415

composer.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"name": "ilzrv/laravel-slow-query-detector",
33
"description": "Laravel Slow DB Query Detector",
44
"require": {
5-
"illuminate/support": "^5.6|^6.0|^7.0|^8.0"
5+
"php": "^8.0",
6+
"illuminate/support": "^9.0"
67
},
78
"license": "MIT",
89
"authors": [
@@ -29,8 +30,8 @@
2930
}
3031
},
3132
"require-dev": {
32-
"phpunit/phpunit": "^7.0 || ^8.0 || ^9.0",
33-
"orchestra/testbench": "^3.0 || ^4.0 || ^5.0 || ^6.0",
34-
"timacdonald/log-fake": "^1.6"
33+
"phpunit/phpunit": "^9.5",
34+
"orchestra/testbench": "^7.0",
35+
"timacdonald/log-fake": "^2.0"
3536
}
3637
}

src/Http/Middleware/SlowQueryDetectorMiddleware.php

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,41 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Ilzrv\LaravelSlowQueryDetector\Http\Middleware;
46

57
use Closure;
6-
use Illuminate\Support\Facades\Log;
8+
use Illuminate\Database\Connection;
9+
use Illuminate\Database\Events\QueryExecuted;
10+
use Illuminate\Http\Request;
711
use Illuminate\Support\Str;
12+
use Psr\Log\LoggerInterface;
813

9-
class SlowQueryDetectorMiddleware
14+
final class SlowQueryDetectorMiddleware
1015
{
11-
protected $queriesCount = 0;
12-
protected $heavyQueryCount = 0;
13-
protected $heaviestQueryTime = 0;
14-
protected $heaviestQuery = '';
15-
protected $executionTime = 0;
16-
17-
/**
18-
* Handle an incoming request.
19-
*
20-
* @param \Illuminate\Http\Request $request
21-
* @param \Closure $next
22-
* @return mixed
23-
*/
24-
public function handle($request, Closure $next)
25-
{
16+
protected int $queriesCount = 0;
17+
18+
protected int $heavyQueryCount = 0;
19+
20+
protected float $heaviestQueryTime = 0;
21+
22+
protected string $heaviestQuery = '';
23+
24+
protected float $executionTime = 0;
25+
26+
public function __construct(
27+
private Connection $dbConnection,
28+
private LoggerInterface $logger,
29+
) {
30+
}
31+
32+
public function handle(
33+
Request $request,
34+
Closure $next,
35+
): mixed {
2636
$this->executionTime = -round(microtime(true) * 1000);
2737

28-
\DB::listen(function ($query) {
38+
$this->dbConnection->listen(function (QueryExecuted $query) {
2939
$this->queriesCount++;
3040
if ($query->time > config('sqd.query.max_time')) {
3141
$this->heavyQueryCount++;
@@ -47,14 +57,14 @@ public function handle($request, Closure $next)
4757
return $next;
4858
}
4959

50-
protected function needNotify()
60+
protected function needNotify(): bool
5161
{
5262
return $this->queriesCount > config('sqd.code.max_queries')
5363
|| $this->executionTime > config('sqd.code.max_time')
5464
|| $this->heaviestQueryTime > config('sqd.query.max_time');
5565
}
5666

57-
protected function notify($request)
67+
protected function notify(Request $request): void
5868
{
5969
$data = [
6070
'SQD' => [
@@ -66,14 +76,16 @@ protected function notify($request)
6676
'Heaviest Query' => [
6777
'Query' => $this->heaviestQuery,
6878
'Time' => $this->heaviestQueryTime . ' ms.',
69-
]
70-
]
79+
],
80+
],
7181
];
7282

73-
Log::critical(print_r($data, true));
83+
$this->logger->critical(
84+
print_r($data, true)
85+
);
7486
}
7587

76-
protected function getQuery($query)
88+
protected function getQuery(QueryExecuted $query): string
7789
{
7890
return config('sqd.query.with_bindings')
7991
? Str::replaceArray('?', $query->bindings, $query->sql)

src/ServiceProvider.php

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,23 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Ilzrv\LaravelSlowQueryDetector;
46

57
use Ilzrv\LaravelSlowQueryDetector\Http\Middleware\SlowQueryDetectorMiddleware;
68

7-
class ServiceProvider extends \Illuminate\Support\ServiceProvider
9+
final class ServiceProvider extends \Illuminate\Support\ServiceProvider
810
{
9-
/**
10-
* Register services.
11-
*
12-
* @return void
13-
*/
14-
public function register()
11+
public function register(): void
1512
{
1613
$this->mergeConfigFrom(__DIR__ . '/../config/sqd.php', 'sqd');
1714
}
1815

19-
/**
20-
* Bootstrap services.
21-
*
22-
* @return void
23-
*/
24-
public function boot()
16+
public function boot(): void
2517
{
2618
$this->publishes([__DIR__ . '/../config/sqd.php' => config_path('sqd.php')]);
2719

28-
if (config('sqd.enabled')) {
20+
if (config('sqd.enabled') === true) {
2921
$this->app['router']->pushMiddlewareToGroup('web', SlowQueryDetectorMiddleware::class);
3022
}
3123
}

tests/SlowCodeTest.php

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,27 @@
44

55
use Illuminate\Support\Facades\Log;
66
use Illuminate\Support\Str;
7+
use TiMacDonald\Log\LogEntry;
78

89
class SlowCodeTest extends TestCase
910
{
10-
public function testBeNotifiedWhenCodeExceededMaxTime()
11+
public function testBeNotifiedWhenCodeExceededMaxTime(): void
1112
{
12-
$this->request(function () {
13-
usleep(1000001); // 1 s
14-
});
13+
$this->request(fn() => usleep(1000000)); // 1 s
1514

16-
Log::assertLogged('critical', function ($message, $context) {
17-
return Str::contains($message, 'SQD');
18-
});
15+
Log::assertLogged(
16+
fn(LogEntry $log) => $log->level === 'critical' && Str::contains($log->message, 'SQD')
17+
);
1918
}
2019

21-
public function testBeSilentWhenCodeRunsFine()
20+
public function testBeSilentWhenCodeRunsFine(): void
2221
{
23-
$this->request(function () {
24-
usleep(900000); // 0.9 s
25-
});
22+
$this->request(fn() => usleep(900000)); // 0.9 s
2623

27-
Log::assertNotLogged('critical');
24+
Log::assertNotLogged(fn(LogEntry $log) => $log->level === 'critical');
2825
}
2926

30-
public function testBeNotifiedWhenCodeHasTooManyQueries()
27+
public function testBeNotifiedWhenCodeHasTooManyQueries(): void
3128
{
3229
$this->request(function () {
3330
$connection = $this->getConnectionWithSleepFunction();
@@ -36,12 +33,12 @@ public function testBeNotifiedWhenCodeHasTooManyQueries()
3633
}
3734
});
3835

39-
Log::assertLogged('critical', function ($message, $context) {
40-
return Str::contains($message, 'SQD');
41-
});
36+
Log::assertLogged(
37+
fn(LogEntry $log) => $log->level === 'critical' && Str::contains($log->message, 'SQD')
38+
);
4239
}
4340

44-
public function testBeSilentWhenCodeHasFineCountQueries()
41+
public function testBeSilentWhenCodeHasFineCountQueries(): void
4542
{
4643
$this->request(function () {
4744
$connection = $this->getConnectionWithSleepFunction();
@@ -50,6 +47,6 @@ public function testBeSilentWhenCodeHasFineCountQueries()
5047
}
5148
});
5249

53-
Log::assertNotLogged('critical');
50+
Log::assertNotLogged(fn(LogEntry $log) => $log->level === 'critical');
5451
}
5552
}

tests/SlowQueryTest.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Ilzrv\LaravelSlowQueryDetector\Tests;
46

57
use Illuminate\Support\Facades\Log;
68
use Illuminate\Support\Str;
9+
use TiMacDonald\Log\LogEntry;
710

8-
class SlowQueryTest extends TestCase
11+
final class SlowQueryTest extends TestCase
912
{
10-
public function testBeNotifiedWhenQueryExceededMaxTime()
13+
public function testBeNotifiedWhenQueryExceededMaxTime(): void
1114
{
1215
$this->request(function () {
1316
$connection = $this->getConnectionWithSleepFunction();
@@ -16,12 +19,12 @@ public function testBeNotifiedWhenQueryExceededMaxTime()
1619
return response('slow');
1720
});
1821

19-
Log::assertLogged('critical', function ($message, $context) {
20-
return Str::contains($message, 'SQD');
21-
});
22+
Log::assertLogged(
23+
fn(LogEntry $log) => $log->level === 'critical' && Str::contains($log->message, 'SQD')
24+
);
2225
}
2326

24-
public function testBeSilentWhenQueryNotExceededMaxTime()
27+
public function testBeSilentWhenQueryNotExceededMaxTime(): void
2528
{
2629
$this->request(function () {
2730
$connection = $this->getConnectionWithSleepFunction();
@@ -30,6 +33,6 @@ public function testBeSilentWhenQueryNotExceededMaxTime()
3033
return response('fast');
3134
});
3235

33-
Log::assertNotLogged('critical');
36+
Log::assertNotLogged(fn(LogEntry $log) => $log->level === 'critical');
3437
}
3538
}

0 commit comments

Comments
 (0)