Skip to content

Commit 9391281

Browse files
minor #46090 [DoctrineBridge] Restore PHP8 features for Doctrine debug middlewares (l-vo)
This PR was submitted for the 6.0 branch but it was merged into the 6.1 branch instead. Discussion ---------- [DoctrineBridge] Restore PHP8 features for Doctrine debug middlewares | Q | A | ------------- | --- | Branch? | 6.1 | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | | License | MIT | Doc PR | To merge #45491 in 5.4, the code has been changed to be 7.2 compliant. This PR restores the original code for Symfony versions that support php >= 8.0 (see #45491 (comment)). Commits ------- f4bdd82 Restore PHP8 features for Doctrine debug middlewares
2 parents f99f54a + f4bdd82 commit 9391281

File tree

8 files changed

+71
-159
lines changed

8 files changed

+71
-159
lines changed

src/Symfony/Bridge/Doctrine/DataCollector/DoctrineDataCollector.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,20 @@
2929
*/
3030
class DoctrineDataCollector extends DataCollector
3131
{
32-
private ManagerRegistry $registry;
3332
private array $connections;
3433
private array $managers;
35-
private ?DebugDataHolder $debugDataHolder;
3634

3735
/**
3836
* @var array<string, DebugStack>
3937
*/
4038
private array $loggers = [];
4139

42-
public function __construct(ManagerRegistry $registry, DebugDataHolder $debugDataHolder = null)
43-
{
44-
$this->registry = $registry;
40+
public function __construct(
41+
private ManagerRegistry $registry,
42+
private ?DebugDataHolder $debugDataHolder = null,
43+
) {
4544
$this->connections = $registry->getConnectionNames();
4645
$this->managers = $registry->getManagerNames();
47-
$this->debugDataHolder = $debugDataHolder;
4846
}
4947

5048
/**

src/Symfony/Bridge/Doctrine/Middleware/Debug/Connection.php

Lines changed: 25 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,15 @@
2424
*/
2525
final class Connection extends AbstractConnectionMiddleware
2626
{
27-
private $nestingLevel = 0;
28-
private $debugDataHolder;
29-
private $stopwatch;
30-
private $connectionName;
31-
32-
public function __construct(ConnectionInterface $connection, DebugDataHolder $debugDataHolder, ?Stopwatch $stopwatch, string $connectionName)
33-
{
27+
private int $nestingLevel = 0;
28+
29+
public function __construct(
30+
ConnectionInterface $connection,
31+
private DebugDataHolder $debugDataHolder,
32+
private ?Stopwatch $stopwatch,
33+
private string $connectionName,
34+
) {
3435
parent::__construct($connection);
35-
36-
$this->debugDataHolder = $debugDataHolder;
37-
$this->stopwatch = $stopwatch;
38-
$this->connectionName = $connectionName;
3936
}
4037

4138
public function prepare(string $sql): DriverStatement
@@ -44,28 +41,22 @@ public function prepare(string $sql): DriverStatement
4441
parent::prepare($sql),
4542
$this->debugDataHolder,
4643
$this->connectionName,
47-
$sql
44+
$sql,
4845
);
4946
}
5047

5148
public function query(string $sql): Result
5249
{
5350
$this->debugDataHolder->addQuery($this->connectionName, $query = new Query($sql));
5451

55-
if (null !== $this->stopwatch) {
56-
$this->stopwatch->start('doctrine', 'doctrine');
57-
}
58-
52+
$this->stopwatch?->start('doctrine', 'doctrine');
5953
$query->start();
6054

6155
try {
6256
$result = parent::query($sql);
6357
} finally {
6458
$query->stop();
65-
66-
if (null !== $this->stopwatch) {
67-
$this->stopwatch->stop('doctrine');
68-
}
59+
$this->stopwatch?->stop('doctrine');
6960
}
7061

7162
return $result;
@@ -75,20 +66,14 @@ public function exec(string $sql): int
7566
{
7667
$this->debugDataHolder->addQuery($this->connectionName, $query = new Query($sql));
7768

78-
if (null !== $this->stopwatch) {
79-
$this->stopwatch->start('doctrine', 'doctrine');
80-
}
81-
69+
$this->stopwatch?->start('doctrine', 'doctrine');
8270
$query->start();
8371

8472
try {
8573
$affectedRows = parent::exec($sql);
8674
} finally {
8775
$query->stop();
88-
89-
if (null !== $this->stopwatch) {
90-
$this->stopwatch->stop('doctrine');
91-
}
76+
$this->stopwatch?->stop('doctrine');
9277
}
9378

9479
return $affectedRows;
@@ -101,24 +86,14 @@ public function beginTransaction(): bool
10186
$this->debugDataHolder->addQuery($this->connectionName, $query = new Query('"START TRANSACTION"'));
10287
}
10388

104-
if (null !== $this->stopwatch) {
105-
$this->stopwatch->start('doctrine', 'doctrine');
106-
}
107-
108-
if (null !== $query) {
109-
$query->start();
110-
}
89+
$this->stopwatch?->start('doctrine', 'doctrine');
90+
$query?->start();
11191

11292
try {
11393
$ret = parent::beginTransaction();
11494
} finally {
115-
if (null !== $query) {
116-
$query->stop();
117-
}
118-
119-
if (null !== $this->stopwatch) {
120-
$this->stopwatch->stop('doctrine');
121-
}
95+
$query?->stop();
96+
$this->stopwatch?->stop('doctrine');
12297
}
12398

12499
return $ret;
@@ -131,24 +106,14 @@ public function commit(): bool
131106
$this->debugDataHolder->addQuery($this->connectionName, $query = new Query('"COMMIT"'));
132107
}
133108

134-
if (null !== $this->stopwatch) {
135-
$this->stopwatch->start('doctrine', 'doctrine');
136-
}
137-
138-
if (null !== $query) {
139-
$query->start();
140-
}
109+
$this->stopwatch?->start('doctrine', 'doctrine');
110+
$query?->start();
141111

142112
try {
143113
$ret = parent::commit();
144114
} finally {
145-
if (null !== $query) {
146-
$query->stop();
147-
}
148-
149-
if (null !== $this->stopwatch) {
150-
$this->stopwatch->stop('doctrine');
151-
}
115+
$query?->stop();
116+
$this->stopwatch?->stop('doctrine');
152117
}
153118

154119
return $ret;
@@ -161,24 +126,14 @@ public function rollBack(): bool
161126
$this->debugDataHolder->addQuery($this->connectionName, $query = new Query('"ROLLBACK"'));
162127
}
163128

164-
if (null !== $this->stopwatch) {
165-
$this->stopwatch->start('doctrine', 'doctrine');
166-
}
167-
168-
if (null !== $query) {
169-
$query->start();
170-
}
129+
$this->stopwatch?->start('doctrine', 'doctrine');
130+
$query?->start();
171131

172132
try {
173133
$ret = parent::rollBack();
174134
} finally {
175-
if (null !== $query) {
176-
$query->stop();
177-
}
178-
179-
if (null !== $this->stopwatch) {
180-
$this->stopwatch->stop('doctrine');
181-
}
135+
$query?->stop();
136+
$this->stopwatch?->stop('doctrine');
182137
}
183138

184139
return $ret;

src/Symfony/Bridge/Doctrine/Middleware/Debug/DebugDataHolder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717
class DebugDataHolder
1818
{
19-
private $data = [];
19+
private array $data = [];
2020

2121
public function addQuery(string $connectionName, Query $query): void
2222
{

src/Symfony/Bridge/Doctrine/Middleware/Debug/Driver.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,13 @@
2222
*/
2323
final class Driver extends AbstractDriverMiddleware
2424
{
25-
private $debugDataHolder;
26-
private $stopwatch;
27-
private $connectionName;
28-
29-
public function __construct(DriverInterface $driver, DebugDataHolder $debugDataHolder, ?Stopwatch $stopwatch, string $connectionName)
30-
{
25+
public function __construct(
26+
DriverInterface $driver,
27+
private DebugDataHolder $debugDataHolder,
28+
private ?Stopwatch $stopwatch,
29+
private string $connectionName,
30+
) {
3131
parent::__construct($driver);
32-
33-
$this->debugDataHolder = $debugDataHolder;
34-
$this->stopwatch = $stopwatch;
35-
$this->connectionName = $connectionName;
3632
}
3733

3834
public function connect(array $params): Connection
@@ -41,7 +37,7 @@ public function connect(array $params): Connection
4137
parent::connect($params),
4238
$this->debugDataHolder,
4339
$this->stopwatch,
44-
$this->connectionName
40+
$this->connectionName,
4541
);
4642
}
4743
}

src/Symfony/Bridge/Doctrine/Middleware/Debug/Middleware.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,11 @@
2222
*/
2323
final class Middleware implements MiddlewareInterface
2424
{
25-
private $debugDataHolder;
26-
private $stopwatch;
27-
private $connectionName;
28-
29-
public function __construct(DebugDataHolder $debugDataHolder, ?Stopwatch $stopwatch, string $connectionName = 'default')
30-
{
31-
$this->debugDataHolder = $debugDataHolder;
32-
$this->stopwatch = $stopwatch;
33-
$this->connectionName = $connectionName;
25+
public function __construct(
26+
private DebugDataHolder $debugDataHolder,
27+
private ?Stopwatch $stopwatch,
28+
private string $connectionName = 'default',
29+
) {
3430
}
3531

3632
public function wrap(DriverInterface $driver): DriverInterface

src/Symfony/Bridge/Doctrine/Middleware/Debug/Query.php

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,15 @@
2020
*/
2121
class Query
2222
{
23-
private $params = [];
24-
private $types = [];
23+
private array $params = [];
24+
private array $types = [];
2525

26-
private $start;
27-
private $duration;
26+
private ?float $start = null;
27+
private ?float $duration = null;
2828

29-
private $sql;
30-
31-
public function __construct(string $sql)
32-
{
33-
$this->sql = $sql;
29+
public function __construct(
30+
private string $sql,
31+
) {
3432
}
3533

3634
public function start(): void
@@ -45,11 +43,7 @@ public function stop(): void
4543
}
4644
}
4745

48-
/**
49-
* @param string|int $param
50-
* @param string|int|float|bool|null $variable
51-
*/
52-
public function setParam($param, &$variable, int $type): void
46+
public function setParam(string|int $param, null|string|int|float|bool &$variable, int $type): void
5347
{
5448
// Numeric indexes start at 0 in profiler
5549
$idx = \is_int($param) ? $param - 1 : $param;
@@ -58,11 +52,7 @@ public function setParam($param, &$variable, int $type): void
5852
$this->types[$idx] = $type;
5953
}
6054

61-
/**
62-
* @param string|int $param
63-
* @param string|int|float|bool|null $value
64-
*/
65-
public function setValue($param, $value, int $type): void
55+
public function setValue(string|int $param, null|string|int|float|bool $value, int $type): void
6656
{
6757
// Numeric indexes start at 0 in profiler
6858
$idx = \is_int($param) ? $param - 1 : $param;

src/Symfony/Bridge/Doctrine/Middleware/Debug/Statement.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@
2323
*/
2424
final class Statement extends AbstractStatementMiddleware
2525
{
26-
private $debugDataHolder;
27-
private $connectionName;
28-
private $query;
26+
private Query $query;
2927

30-
public function __construct(StatementInterface $statement, DebugDataHolder $debugDataHolder, string $connectionName, string $sql)
31-
{
28+
public function __construct(
29+
StatementInterface $statement,
30+
private DebugDataHolder $debugDataHolder,
31+
private string $connectionName,
32+
string $sql,
33+
) {
3234
parent::__construct($statement);
3335

34-
$this->debugDataHolder = $debugDataHolder;
35-
$this->connectionName = $connectionName;
3636
$this->query = new Query($sql);
3737
}
3838

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy