Skip to content

Commit 544a21c

Browse files
committed
feat: introduce better-fitting method names
1 parent 759f977 commit 544a21c

File tree

5 files changed

+93
-12
lines changed

5 files changed

+93
-12
lines changed

README.md

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
MySQL Query optimization with the `EXPLAIN` command is unnecessarily complicated: The output contains a lot of cryptic information that is incomprehensible or entirely misleading.
1212

13-
This Larvel package collects many query metrics that will be sent to [mysqlexplain.com](https://mysqlexplain.com) and transformed to be much easier to understand.
13+
This Laravel package collects many query metrics that will be sent to [mysqlexplain.com](https://mysqlexplain.com) and transformed to be much easier to understand.
1414

1515
## Installation
1616

@@ -26,26 +26,28 @@ composer require tpetry/laravel-mysql-explain
2626

2727
Three new methods have been added to the query builder for very easy submission of query plans:
2828

29-
| Type | Action |
30-
|------------------------|---------------------------------------------------------------------|
31-
| `explainForHumans` | returns URL to processed EXPLAIN output |
32-
| `dumpExplainForHumans` | dumps URL to processed EXPLAIN output and continue normal execution |
33-
| `ddExplainForHumans` | dumps URL to processed EXPLAIN output and stops execution |
34-
29+
| Type | Action |
30+
|-------------------------------------------|---------------------------------------------------------------------|
31+
| `visualExplain` | returns URL to processed EXPLAIN output |
32+
| `dumpVisualExplain` | dumps URL to processed EXPLAIN output and continue normal execution |
33+
| `ddVisualExplain` | dumps URL to processed EXPLAIN output and stops execution |
34+
| `explainForHumans` ***(deprecated)*** | returns URL to processed EXPLAIN output |
35+
| `dumpExplainForHumans` ***(deprecated)*** | dumps URL to processed EXPLAIN output and continue normal execution |
36+
| `ddExplainForHumans` ***(deprecated)*** | dumps URL to processed EXPLAIN output and stops execution |
3537

3638
```php
3739
// $url will be e.g. https://mysqlexplain.com/explain/01j2gcrbsjet9r8rav114vgfsy
3840
$url = Film::where('description', 'like', '%astronaut%')
39-
->explainForHumans();
41+
->visualExplain();
4042

4143
// URL to EXPLAIN will be printed to screen
4244
$users = Film::where('description', 'like', '%astronaut%')
43-
->dumpExplainForHumans()
45+
->dumpVisualExplain()
4446
->get();
4547

4648
// URL to EXPLAIN will be printed to screen & execution is stopped
47-
$users = Film::where('description', 'like', '%astronaut%')
48-
->ddExplainForHumans();
49+
Film::where('description', 'like', '%astronaut%')
50+
->ddVisualExplain();
4951
```
5052

5153
### Raw Queries

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tpetry/laravel-mysql-explain",
3-
"description": "Get MySQL EXPLAIN plans that are understandable for humans",
3+
"description": "Get Visual MySQL EXPLAIN for Laravel.",
44
"keywords": [
55
"laravel",
66
"mysql",

src/Mixins/BuilderMixin.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ public function ddExplainForHumans(): Closure
2222
};
2323
}
2424

25+
public function ddVisualExplain(): Closure
26+
{
27+
/** @return never-returns */
28+
return function () {
29+
/** @var \Illuminate\Contracts\Database\Query\Builder $this */
30+
$url = MysqlExplain::submitBuilder($this);
31+
dd($url);
32+
};
33+
}
34+
2535
public function dumpExplainForHumans(): Closure
2636
{
2737
return function () {
@@ -33,6 +43,17 @@ public function dumpExplainForHumans(): Closure
3343
};
3444
}
3545

46+
public function dumpVisualExplain(): Closure
47+
{
48+
return function () {
49+
/** @var \Illuminate\Contracts\Database\Query\Builder $this */
50+
$url = MysqlExplain::submitBuilder($this);
51+
dump($url);
52+
53+
return $this;
54+
};
55+
}
56+
3657
public function explainForHumans(): Closure
3758
{
3859
return function (): string {
@@ -42,4 +63,14 @@ public function explainForHumans(): Closure
4263
return $url;
4364
};
4465
}
66+
67+
public function visualExplain(): Closure
68+
{
69+
return function (): string {
70+
/** @var \Illuminate\Contracts\Database\Query\Builder $this */
71+
$url = MysqlExplain::submitBuilder($this);
72+
73+
return $url;
74+
};
75+
}
4576
}

tests/Mixins/EloquentBuilderTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,21 @@ public function testDdExplainForHumans(): void
1515
$this->markTestSkipped('how to test dump() and not exit PHPUnit on exit() call?');
1616
}
1717

18+
public function testDdVisualExplain(): void
19+
{
20+
$this->markTestSkipped('how to test dump() and not exit PHPUnit on exit() call?');
21+
}
22+
1823
public function testDumpExplainForHumans(): void
1924
{
2025
$this->markTestSkipped('how to test dump()?');
2126
}
2227

28+
public function testDumpVisualExplain(): void
29+
{
30+
$this->markTestSkipped('how to test dump()?');
31+
}
32+
2333
public function testExplainForHumans(): void
2434
{
2535
$model = new class() extends Model {};
@@ -34,4 +44,19 @@ public function testExplainForHumans(): void
3444

3545
$this->assertEquals('https://dummy-url-A5mhRHJXvC.local/I2aifhDBCO', $url);
3646
}
47+
48+
public function testVisualExplain(): void
49+
{
50+
$model = new class() extends Model {};
51+
$builder = $model->newQuery();
52+
53+
MysqlExplain::shouldReceive('submitBuilder')
54+
->once()
55+
->with($builder)
56+
->andReturn('https://dummy-url-A5mhRHJXvC.local/I2aifhDBCO');
57+
58+
$url = $builder->visualExplain();
59+
60+
$this->assertEquals('https://dummy-url-A5mhRHJXvC.local/I2aifhDBCO', $url);
61+
}
3762
}

tests/Mixins/QueryBuilderTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,21 @@ public function testDdExplainForHumans(): void
1515
$this->markTestSkipped('how to test dump() and not exit PHPUnit on exit() call?');
1616
}
1717

18+
public function testDdVisualExplain(): void
19+
{
20+
$this->markTestSkipped('how to test dump() and not exit PHPUnit on exit() call?');
21+
}
22+
1823
public function testDumpExplainForHumans(): void
1924
{
2025
$this->markTestSkipped('how to test dump()?');
2126
}
2227

28+
public function testDumpVisualExplain(): void
29+
{
30+
$this->markTestSkipped('how to test dump()?');
31+
}
32+
2333
public function testExplainForHumans(): void
2434
{
2535
$builder = DB::table('test123');
@@ -32,4 +42,17 @@ public function testExplainForHumans(): void
3242

3343
$this->assertEquals('https://dummy-url-f6V7VImZnz.local', $url);
3444
}
45+
46+
public function testVisualExplain(): void
47+
{
48+
$builder = DB::table('test123');
49+
MysqlExplain::shouldReceive('submitBuilder')
50+
->once()
51+
->with($builder)
52+
->andReturn('https://dummy-url-f6V7VImZnz.local');
53+
54+
$url = $builder->visualExplain();
55+
56+
$this->assertEquals('https://dummy-url-f6V7VImZnz.local', $url);
57+
}
3558
}

0 commit comments

Comments
 (0)