Skip to content
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

Set properties to readonly if possible #5547

Merged
merged 1 commit into from
Jul 28, 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
10 changes: 3 additions & 7 deletions src/Cache/ArrayResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,15 @@
*/
final class ArrayResult implements Result
{
private int $columnCount = 0;
private int $num = 0;
private readonly int $columnCount;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

readonly properties must not have a default. The old default was moved to the constructor logic.

private int $num = 0;

/**
* @param list<array<string, mixed>> $data
*/
public function __construct(private array $data)
{
if (count($data) === 0) {
return;
}

$this->columnCount = count($data[0]);
$this->columnCount = $data === [] ? 0 : count($data[0]);
}

public function fetchNumeric(): array|false
Expand Down
2 changes: 1 addition & 1 deletion src/Driver/IBMDB2/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ final class Connection implements ConnectionInterface
*
* @param resource $connection
*/
public function __construct(private $connection)
public function __construct(private readonly mixed $connection)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only typed properties may be flagged readonly and there is no native resource type. These are known limitations of the PHP engine. This is why I'm adding a mixed type here. Since the doc block is more precise, this change should not make a difference for static analysis.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really like using the mixed native type where the argument is of a more specific type but given that it's the limitation of the type system, it should be okay.

{
}

Expand Down
2 changes: 1 addition & 1 deletion src/Driver/IBMDB2/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final class Result implements ResultInterface
*
* @param resource $statement
*/
public function __construct(private $statement)
public function __construct(private readonly mixed $statement)
{
}

Expand Down
2 changes: 1 addition & 1 deletion src/Driver/IBMDB2/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ final class Statement implements StatementInterface
*
* @param resource $stmt
*/
public function __construct(private $stmt)
public function __construct(private readonly mixed $stmt)
{
}

Expand Down
12 changes: 5 additions & 7 deletions src/Driver/Mysqli/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ final class Result implements ResultInterface
* Whether the statement result has columns. The property should be used only after the result metadata
* has been fetched ({@see $metadataFetched}). Otherwise, the property value is undetermined.
*/
private bool $hasColumns = false;
private readonly bool $hasColumns;

/**
* Mapping of statement result column indexes to their names. The property should be used only
* if the statement result has columns ({@see $hasColumns}). Otherwise, the property value is undetermined.
*
* @var array<int,string>
*/
private array $columnNames = [];
private readonly array $columnNames;

/** @var mixed[] */
private array $boundValues = [];
Expand All @@ -42,16 +42,14 @@ final class Result implements ResultInterface
*/
public function __construct(private readonly mysqli_stmt $statement)
{
$meta = $statement->result_metadata();
$meta = $statement->result_metadata();
$this->hasColumns = $meta !== false;
$this->columnNames = $meta !== false ? array_column($meta->fetch_fields(), 'name') : [];

if ($meta === false) {
return;
}

$this->hasColumns = true;

$this->columnNames = array_column($meta->fetch_fields(), 'name');

$meta->free();

// Store result of every execution which has it. Otherwise it will be impossible
Expand Down
2 changes: 1 addition & 1 deletion src/Driver/OCI8/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ final class Connection implements ConnectionInterface
*
* @param resource $connection
*/
public function __construct(private $connection)
public function __construct(private readonly mixed $connection)
{
$this->parser = new Parser(false);
$this->executionMode = new ExecutionMode();
Expand Down
2 changes: 1 addition & 1 deletion src/Driver/OCI8/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ final class Result implements ResultInterface
*
* @param resource $statement
*/
public function __construct(private $statement)
public function __construct(private readonly mixed $statement)
{
}

Expand Down
4 changes: 2 additions & 2 deletions src/Driver/OCI8/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ final class Statement implements StatementInterface
* @param array<int,string> $parameterMap
*/
public function __construct(
private $connection,
private $statement,
private readonly mixed $connection,
private readonly mixed $statement,
private readonly array $parameterMap,
private readonly ExecutionMode $executionMode
) {
Expand Down
6 changes: 1 addition & 5 deletions src/Driver/PDO/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,12 @@

final class Connection implements ConnectionInterface
{
private readonly PDO $connection;

/**
* @internal The connection can be only instantiated by its driver.
*/
public function __construct(PDO $connection)
public function __construct(private readonly PDO $connection)
{
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$this->connection = $connection;
}

public function exec(string $sql): int|string
Expand Down
2 changes: 1 addition & 1 deletion src/Driver/SQLSrv/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class Connection implements ConnectionInterface
*
* @param resource $connection
*/
public function __construct(private $connection)
public function __construct(private readonly mixed $connection)
{
}

Expand Down
2 changes: 1 addition & 1 deletion src/Driver/SQLSrv/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final class Result implements ResultInterface
*
* @param resource $statement
*/
public function __construct(private $statement)
public function __construct(private readonly mixed $statement)
{
}

Expand Down
2 changes: 1 addition & 1 deletion src/Driver/SQLSrv/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ final class Statement implements StatementInterface
* @param resource $conn
*/
public function __construct(
private $conn,
private readonly mixed $conn,
private string $sql
) {
if (stripos($sql, 'INSERT INTO ') !== 0) {
Expand Down
13 changes: 4 additions & 9 deletions src/Exception/DriverException.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,23 @@
*/
class DriverException extends Exception implements Driver\Exception
{
/**
* The query that triggered the exception, if any.
*/
private readonly ?Query $query;

/**
* @internal
*
* @param Driver\Exception $driverException The DBAL driver exception to chain.
* @param Query|null $query The SQL query that triggered the exception, if any.
*/
public function __construct(Driver\Exception $driverException, ?Query $query)
{
public function __construct(
Driver\Exception $driverException,
private readonly ?Query $query,
) {
if ($query !== null) {
$message = 'An exception occurred while executing a query: ' . $driverException->getMessage();
} else {
$message = 'An exception occurred in the driver: ' . $driverException->getMessage();
}

parent::__construct($message, $driverException->getCode(), $driverException);

$this->query = $query;
}

public function getSQLState(): ?string
Expand Down
6 changes: 4 additions & 2 deletions src/Query/From.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
*/
final class From
{
public function __construct(public string $table, public ?string $alias = null)
{
public function __construct(
public readonly string $table,
public readonly ?string $alias = null,
) {
}
}
8 changes: 4 additions & 4 deletions src/Query/Join.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
final class Join
{
private function __construct(
public string $type,
public string $table,
public string $alias,
public ?string $condition
public readonly string $type,
public readonly string $table,
public readonly string $alias,
public readonly ?string $condition,
) {
}

Expand Down