Skip to content

Commit

Permalink
Add strong type to some properties (#1183)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek authored Mar 12, 2024
1 parent dd97646 commit 777b83e
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 45 deletions.
4 changes: 2 additions & 2 deletions src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ class Model implements \IteratorAggregate
];

/** @var array<mixed> The seed used by addField() method. */
protected $_defaultSeedAddField = [Field::class];
protected array $_defaultSeedAddField = [Field::class];

/** @var array<mixed> The seed used by addExpression() method. */
protected $_defaultSeedAddExpression = [CallbackField::class];
protected array $_defaultSeedAddExpression = [CallbackField::class];

/** @var array<string, Field> */
protected array $fields = [];
Expand Down
2 changes: 1 addition & 1 deletion src/Model/JoinsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
trait JoinsTrait
{
/** @var array<mixed> The class used by join() method. */
protected $_defaultSeedJoin = [Join::class];
protected array $_defaultSeedJoin = [Join::class];

/**
* Creates an objects that describes relationship between multiple tables (or collections).
Expand Down
10 changes: 5 additions & 5 deletions src/Model/ReferencesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@
trait ReferencesTrait
{
/** @var array<mixed> The seed used by addReference() method. */
protected $_defaultSeedAddReference = [Reference::class];
protected array $_defaultSeedAddReference = [Reference::class];

/** @var array<mixed> The seed used by hasOne() method. */
protected $_defaultSeedHasOne = [Reference\HasOne::class];
protected array $_defaultSeedHasOne = [Reference\HasOne::class];

/** @var array<mixed> The seed used by hasMany() method. */
protected $_defaultSeedHasMany = [Reference\HasMany::class];
protected array $_defaultSeedHasMany = [Reference\HasMany::class];

/** @var array<mixed> The seed used by containsOne() method. */
protected $_defaultSeedContainsOne = [Reference\ContainsOne::class];
protected array $_defaultSeedContainsOne = [Reference\ContainsOne::class];

/** @var array<mixed> The seed used by containsMany() method. */
protected $_defaultSeedContainsMany = [Reference\ContainsMany::class];
protected array $_defaultSeedContainsMany = [Reference\ContainsMany::class];

/**
* @param array<mixed> $seed
Expand Down
2 changes: 1 addition & 1 deletion src/Model/UserActionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
trait UserActionsTrait
{
/** @var array<mixed> The seed used by addUserAction() method. */
protected $_defaultSeedUserAction = [UserAction::class];
protected array $_defaultSeedUserAction = [UserAction::class];

/** @var array<string, UserAction> Collection of user actions - using key as action system name */
protected $userActions = [];
Expand Down
15 changes: 7 additions & 8 deletions src/Persistence/Sql.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,22 @@ class Sql extends Persistence
public const HOOK_BEFORE_DELETE_QUERY = self::class . '@beforeDeleteQuery';
public const HOOK_AFTER_DELETE_QUERY = self::class . '@afterDeleteQuery';

/** @var Connection */
private $_connection;
private ?Connection $_connection;

/** @var array<mixed> Default class when adding new field. */
protected $_defaultSeedAddField; // no custom seed needed
protected ?array $_defaultSeedAddField = null; // no custom seed needed

/** @var array<mixed> Default class when adding Expression field. */
protected $_defaultSeedAddExpression = [SqlExpressionField::class];
protected ?array $_defaultSeedAddExpression = [SqlExpressionField::class];

/** @var array<mixed> Default class when adding hasOne field. */
protected $_defaultSeedHasOne = [HasOneSql::class];
protected ?array $_defaultSeedHasOne = [HasOneSql::class];

/** @var array<mixed> Default class when adding hasMany field. */
protected $_defaultSeedHasMany; // no custom seed needed
protected ?array $_defaultSeedHasMany = null; // no custom seed needed

/** @var array<mixed> Default class when adding join. */
protected $_defaultSeedJoin = [Sql\Join::class];
protected ?array $_defaultSeedJoin = [Sql\Join::class];

/**
* @param Connection|string|array<string, string>|DbalConnection|DbalDriverConnection $connection
Expand Down Expand Up @@ -85,7 +84,7 @@ public function disconnect(): void
{
parent::disconnect();

$this->_connection = null; // @phpstan-ignore-line
$this->_connection = null;
}

#[\Override]
Expand Down
13 changes: 2 additions & 11 deletions src/Persistence/Sql/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ abstract class Connection
/** @var class-string<Query> */
protected string $queryClass;

/** @var DbalConnection */
private $_connection;
private DbalConnection $_connection;

/** @var array<string, class-string<self>> */
protected static $connectionClassRegistry = [
Expand All @@ -53,7 +52,7 @@ protected function __construct(array $defaults = [])
public function __destruct()
{
// needed for DBAL connection to be released immeditelly
if ($this->_connection !== null) {
if (($this->_connection ?? null) !== null) {
$this->getConnection()->close();
}
}
Expand Down Expand Up @@ -309,10 +308,6 @@ public function dsql($defaults = []): Query
*/
public function executeQuery(Expression $expr): DbalResult
{
if ($this->_connection === null) {
throw new Exception('DBAL connection is not set');
}

return $expr->executeQuery($this->getConnection());
}

Expand All @@ -323,10 +318,6 @@ public function executeQuery(Expression $expr): DbalResult
*/
public function executeStatement(Expression $expr): int
{
if ($this->_connection === null) {
throw new Exception('DBAL connection is not set');
}

return $expr->executeStatement($this->getConnection());
}

Expand Down
8 changes: 3 additions & 5 deletions src/Schema/Migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,12 @@ class Migrator
public const REF_TYPE_LINK = 1;
public const REF_TYPE_PRIMARY = 2;

/** @var Connection */
private $_connection;
private Connection $_connection;

/** @var Table */
public $table;
public Table $table;

/** @var list<string> */
private $createdTableNames = [];
private array $createdTableNames = [];

/**
* @param Connection|Persistence\Sql|Model $source
Expand Down
2 changes: 1 addition & 1 deletion src/Schema/TestSqlPersistence.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct() {} // @phpstan-ignore-line
public function getConnection(): Persistence\Sql\Connection
{
\Closure::bind(function () {
if ($this->_connection === null) {
if (($this->_connection ?? null) === null) {
$this->_connection = Persistence::connect($_ENV['DB_DSN'], $_ENV['DB_USER'], $_ENV['DB_PASSWORD'])->_connection; // @phpstan-ignore-line

if ($this->getDatabasePlatform() instanceof MySQLPlatform) {
Expand Down
11 changes: 0 additions & 11 deletions tests/Persistence/Sql/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,4 @@ public function testException2(): void
$this->expectException(Persistence\Sql\Exception::class);
Connection::connect('');
}

public function testException3(): void
{
$connection = \Closure::bind(static fn () => new Persistence\Sql\Sqlite\Connection(), null, Connection::class)();
$q = $connection->expr('select (2 + 2)');
self::assertSame('select (2 + 2)', $q->render()[0]);

$this->expectException(Persistence\Sql\Exception::class);
$this->expectExceptionMessage('DBAL connection is not set');
$q->executeQuery();
}
}

0 comments on commit 777b83e

Please sign in to comment.