Skip to content

[DoctrineBridge] Restore PHP8 features for Doctrine debug middlewares #46090

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,20 @@
*/
class DoctrineDataCollector extends DataCollector
{
private ManagerRegistry $registry;
private array $connections;
private array $managers;
private ?DebugDataHolder $debugDataHolder;

/**
* @var array<string, DebugStack>
*/
private array $loggers = [];

public function __construct(ManagerRegistry $registry, DebugDataHolder $debugDataHolder = null)
{
$this->registry = $registry;
public function __construct(
private ManagerRegistry $registry,
private ?DebugDataHolder $debugDataHolder = null,
) {
$this->connections = $registry->getConnectionNames();
$this->managers = $registry->getManagerNames();
$this->debugDataHolder = $debugDataHolder;
}

/**
Expand Down
95 changes: 25 additions & 70 deletions src/Symfony/Bridge/Doctrine/Middleware/Debug/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,15 @@
*/
final class Connection extends AbstractConnectionMiddleware
{
private $nestingLevel = 0;
private $debugDataHolder;
private $stopwatch;
private $connectionName;

public function __construct(ConnectionInterface $connection, DebugDataHolder $debugDataHolder, ?Stopwatch $stopwatch, string $connectionName)
{
private int $nestingLevel = 0;

public function __construct(
ConnectionInterface $connection,
private DebugDataHolder $debugDataHolder,
private ?Stopwatch $stopwatch,
private string $connectionName,
) {
parent::__construct($connection);

$this->debugDataHolder = $debugDataHolder;
$this->stopwatch = $stopwatch;
$this->connectionName = $connectionName;
}

public function prepare(string $sql): DriverStatement
Expand All @@ -44,28 +41,22 @@ public function prepare(string $sql): DriverStatement
parent::prepare($sql),
$this->debugDataHolder,
$this->connectionName,
$sql
$sql,
);
}

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

if (null !== $this->stopwatch) {
$this->stopwatch->start('doctrine', 'doctrine');
}

$this->stopwatch?->start('doctrine', 'doctrine');
$query->start();

try {
$result = parent::query($sql);
} finally {
$query->stop();

if (null !== $this->stopwatch) {
$this->stopwatch->stop('doctrine');
}
$this->stopwatch?->stop('doctrine');
}

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

if (null !== $this->stopwatch) {
$this->stopwatch->start('doctrine', 'doctrine');
}

$this->stopwatch?->start('doctrine', 'doctrine');
$query->start();

try {
$affectedRows = parent::exec($sql);
} finally {
$query->stop();

if (null !== $this->stopwatch) {
$this->stopwatch->stop('doctrine');
}
$this->stopwatch?->stop('doctrine');
}

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

if (null !== $this->stopwatch) {
$this->stopwatch->start('doctrine', 'doctrine');
}

if (null !== $query) {
$query->start();
}
$this->stopwatch?->start('doctrine', 'doctrine');
$query?->start();

try {
$ret = parent::beginTransaction();
} finally {
if (null !== $query) {
$query->stop();
}

if (null !== $this->stopwatch) {
$this->stopwatch->stop('doctrine');
}
$query?->stop();
$this->stopwatch?->stop('doctrine');
}

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

if (null !== $this->stopwatch) {
$this->stopwatch->start('doctrine', 'doctrine');
}

if (null !== $query) {
$query->start();
}
$this->stopwatch?->start('doctrine', 'doctrine');
$query?->start();

try {
$ret = parent::commit();
} finally {
if (null !== $query) {
$query->stop();
}

if (null !== $this->stopwatch) {
$this->stopwatch->stop('doctrine');
}
$query?->stop();
$this->stopwatch?->stop('doctrine');
}

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

if (null !== $this->stopwatch) {
$this->stopwatch->start('doctrine', 'doctrine');
}

if (null !== $query) {
$query->start();
}
$this->stopwatch?->start('doctrine', 'doctrine');
$query?->start();

try {
$ret = parent::rollBack();
} finally {
if (null !== $query) {
$query->stop();
}

if (null !== $this->stopwatch) {
$this->stopwatch->stop('doctrine');
}
$query?->stop();
$this->stopwatch?->stop('doctrine');
}

return $ret;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
class DebugDataHolder
{
private $data = [];
private array $data = [];

public function addQuery(string $connectionName, Query $query): void
{
Expand Down
18 changes: 7 additions & 11 deletions src/Symfony/Bridge/Doctrine/Middleware/Debug/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,13 @@
*/
final class Driver extends AbstractDriverMiddleware
{
private $debugDataHolder;
private $stopwatch;
private $connectionName;

public function __construct(DriverInterface $driver, DebugDataHolder $debugDataHolder, ?Stopwatch $stopwatch, string $connectionName)
{
public function __construct(
DriverInterface $driver,
private DebugDataHolder $debugDataHolder,
private ?Stopwatch $stopwatch,
private string $connectionName,
) {
parent::__construct($driver);

$this->debugDataHolder = $debugDataHolder;
$this->stopwatch = $stopwatch;
$this->connectionName = $connectionName;
}

public function connect(array $params): Connection
Expand All @@ -41,7 +37,7 @@ public function connect(array $params): Connection
parent::connect($params),
$this->debugDataHolder,
$this->stopwatch,
$this->connectionName
$this->connectionName,
);
}
}
14 changes: 5 additions & 9 deletions src/Symfony/Bridge/Doctrine/Middleware/Debug/Middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,11 @@
*/
final class Middleware implements MiddlewareInterface
{
private $debugDataHolder;
private $stopwatch;
private $connectionName;

public function __construct(DebugDataHolder $debugDataHolder, ?Stopwatch $stopwatch, string $connectionName = 'default')
{
$this->debugDataHolder = $debugDataHolder;
$this->stopwatch = $stopwatch;
$this->connectionName = $connectionName;
public function __construct(
private DebugDataHolder $debugDataHolder,
private ?Stopwatch $stopwatch,
private string $connectionName = 'default',
) {
}

public function wrap(DriverInterface $driver): DriverInterface
Expand Down
28 changes: 9 additions & 19 deletions src/Symfony/Bridge/Doctrine/Middleware/Debug/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,15 @@
*/
class Query
{
private $params = [];
private $types = [];
private array $params = [];
private array $types = [];

private $start;
private $duration;
private ?float $start = null;
private ?float $duration = null;

private $sql;

public function __construct(string $sql)
{
$this->sql = $sql;
public function __construct(
private string $sql,
) {
}

public function start(): void
Expand All @@ -45,11 +43,7 @@ public function stop(): void
}
}

/**
* @param string|int $param
* @param string|int|float|bool|null $variable
*/
public function setParam($param, &$variable, int $type): void
public function setParam(string|int $param, null|string|int|float|bool &$variable, int $type): void
{
// Numeric indexes start at 0 in profiler
$idx = \is_int($param) ? $param - 1 : $param;
Expand All @@ -58,11 +52,7 @@ public function setParam($param, &$variable, int $type): void
$this->types[$idx] = $type;
}

/**
* @param string|int $param
* @param string|int|float|bool|null $value
*/
public function setValue($param, $value, int $type): void
public function setValue(string|int $param, null|string|int|float|bool $value, int $type): void
{
// Numeric indexes start at 0 in profiler
$idx = \is_int($param) ? $param - 1 : $param;
Expand Down
14 changes: 7 additions & 7 deletions src/Symfony/Bridge/Doctrine/Middleware/Debug/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@
*/
final class Statement extends AbstractStatementMiddleware
{
private $debugDataHolder;
private $connectionName;
private $query;
private Query $query;

public function __construct(StatementInterface $statement, DebugDataHolder $debugDataHolder, string $connectionName, string $sql)
{
public function __construct(
StatementInterface $statement,
private DebugDataHolder $debugDataHolder,
private string $connectionName,
string $sql,
) {
parent::__construct($statement);

$this->debugDataHolder = $debugDataHolder;
$this->connectionName = $connectionName;
$this->query = new Query($sql);
}

Expand Down
Loading