Skip to content

Commit

Permalink
Fix nullable parameter type for PHP 8.4 (#1186)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek authored Mar 19, 2024
1 parent 60869be commit 3042f52
Show file tree
Hide file tree
Showing 33 changed files with 59 additions and 60 deletions.
3 changes: 0 additions & 3 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,6 @@
'general_phpdoc_annotation_remove' => [
'annotations' => ['author', 'copyright', 'throws'],
],
'nullable_type_declaration_for_default_null_value' => [
'use_nullable_type_declaration' => false,
],

// fn => without curly brackets is less readable,
// also prevent bounding of unwanted variables for GC
Expand Down
2 changes: 1 addition & 1 deletion src/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function setDefaults(array $properties, bool $passively = false): self

/**
* @param \Closure<T of Model>(T, mixed, mixed, mixed, mixed, mixed, mixed, mixed, mixed, mixed, mixed): mixed $fx
* @param array<int, mixed> $args
* @param array<int, mixed> $args
*/
protected function onHookToOwnerEntity(string $spot, \Closure $fx, array $args = [], int $priority = 5): int
{
Expand Down
2 changes: 1 addition & 1 deletion src/Field/PasswordField.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function verifyPassword(Model $entity, string $password): bool
return $this->hashPasswordVerify($v, $password);
}

public function generatePassword(int $length = null): string
public function generatePassword(?int $length = null): string
{
$charsAll = array_diff(array_merge(
range('0', '9'),
Expand Down
22 changes: 11 additions & 11 deletions src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ class Model implements \IteratorAggregate
*
* @param array<string, mixed> $defaults
*/
public function __construct(Persistence $persistence = null, array $defaults = [])
public function __construct(?Persistence $persistence = null, array $defaults = [])
{
$this->scope = \Closure::bind(static function () {
return new RootScope();
Expand All @@ -258,7 +258,7 @@ public function isEntity(): bool
return $this->_model !== null;
}

public function assertIsModel(self $expectedModelInstance = null): void
public function assertIsModel(?self $expectedModelInstance = null): void
{
if ($this->_model !== null) {
throw new \TypeError('Expected model, but instance is an entity');
Expand All @@ -271,7 +271,7 @@ public function assertIsModel(self $expectedModelInstance = null): void
}
}

public function assertIsEntity(self $expectedModelInstance = null): void
public function assertIsEntity(?self $expectedModelInstance = null): void
{
if ($this->_model === null) {
throw new \TypeError('Expected entity, but instance is a model');
Expand Down Expand Up @@ -493,7 +493,7 @@ public function &getDirtyRef(): array
*
* @return array<string, string> [field => err_spec]
*/
public function validate(string $intent = null): array
public function validate(?string $intent = null): array
{
$errors = [];
foreach ($this->hook(self::HOOK_VALIDATE, [$intent]) as $error) {
Expand Down Expand Up @@ -805,7 +805,7 @@ public function setMulti(array $fields)
*
* @return ($field is null ? array<string, mixed> : mixed)
*/
public function get(string $field = null)
public function get(?string $field = null)
{
if ($field === null) {
$this->assertIsEntity();
Expand Down Expand Up @@ -1095,7 +1095,7 @@ public function setOrder($field, string $direction = 'asc')
*
* @return $this
*/
public function setLimit(int $count = null, int $offset = 0)
public function setLimit(?int $count = null, int $offset = 0)
{
$this->assertIsModel();

Expand Down Expand Up @@ -1143,7 +1143,7 @@ public function setPersistence(Persistence $persistence)
return $this;
}

public function assertHasPersistence(string $methodName = null): void
public function assertHasPersistence(?string $methodName = null): void
{
if (!$this->issetPersistence()) {
throw new Exception('Model is not associated with a persistence');
Expand Down Expand Up @@ -1706,13 +1706,13 @@ public function import(array $rows)
/**
* Export DataSet as array of hashes.
*
* @param array<int, string>|null $fields Names of fields to export
* @param string $keyField Optional name of field which value we will use as array key
* @param bool $typecast Should we typecast exported data
* @param array<int, string> $fields Names of fields to export
* @param string $keyField Optional name of field which value we will use as array key
* @param bool $typecast Should we typecast exported data
*
* @return ($keyField is string ? array<mixed, array<string, mixed>> : list<array<string, mixed>>)
*/
public function export(array $fields = null, string $keyField = null, bool $typecast = true): array
public function export(?array $fields = null, ?string $keyField = null, bool $typecast = true): array
{
$this->assertHasPersistence('export');

Expand Down
4 changes: 2 additions & 2 deletions src/Model/Join.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public function setOwner(object $owner)

/**
* @param \Closure<T of Model>(T, mixed, mixed, mixed, mixed, mixed, mixed, mixed, mixed, mixed, mixed): mixed $fx
* @param array<int, mixed> $args
* @param array<int, mixed> $args
*/
protected function onHookToOwnerModel(string $spot, \Closure $fx, array $args = [], int $priority = 5): int
{
Expand All @@ -196,7 +196,7 @@ static function (Model $modelOrEntity) use ($name): self {

/**
* @param \Closure<T of Model>(T, mixed, mixed, mixed, mixed, mixed, mixed, mixed, mixed, mixed, mixed): mixed $fx
* @param array<int, mixed> $args
* @param array<int, mixed> $args
*/
protected function onHookToOwnerEntity(string $spot, \Closure $fx, array $args = [], int $priority = 5): int
{
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Scope.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public function negate(): self
}

#[\Override]
public function toWords(Model $model = null): string
public function toWords(?Model $model = null): string
{
$parts = [];
foreach ($this->elements as $nestedCondition) {
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Scope/AbstractScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ abstract public function isEmpty(): bool;
/**
* Convert the scope to human readable words when applied on $model.
*/
abstract public function toWords(Model $model = null): string;
abstract public function toWords(?Model $model = null): string;

/**
* Simplifies by peeling off nested group conditions with single contained component.
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Scope/Condition.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ public function negate(): self
}

#[\Override]
public function toWords(Model $model = null): string
public function toWords(?Model $model = null): string
{
if ($model !== null) {
$model->assertIsModel();
Expand Down
2 changes: 1 addition & 1 deletion src/Model/UserActionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private function addUserActionFromModel(string $name, UserAction $action): void
*
* @return array<string, UserAction>
*/
public function getUserActions(string $appliesTo = null): array
public function getUserActions(?string $appliesTo = null): array
{
$this->assertIsModel();

Expand Down
2 changes: 1 addition & 1 deletion src/Persistence.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ abstract class Persistence
* leaving user and password arguments = null
* @param array<string, mixed> $defaults
*/
public static function connect($dsn, string $user = null, string $password = null, array $defaults = []): self
public static function connect($dsn, ?string $user = null, ?string $password = null, array $defaults = []): self
{
// parse DSN string
$dsn = Persistence\Sql\Connection::normalizeDsn($dsn, $user, $password);
Expand Down
6 changes: 3 additions & 3 deletions src/Persistence/Array_.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ public function generateNewId(Model $model)
*
* @return int|string
*/
public function lastInsertId(Model $model = null)
public function lastInsertId(?Model $model = null)
{
if ($model !== null) {
return $this->lastInsertIdByTable[$model->table] ?? null;
Expand All @@ -358,7 +358,7 @@ public function prepareIterator(Model $model): \Traversable
*
* @return list<array<string, mixed>>
*/
public function export(Model $model, array $fields = null, bool $typecast = true): array
public function export(Model $model, ?array $fields = null, bool $typecast = true): array
{
$data = $model->action('select', [$fields])->getRows();

Expand All @@ -376,7 +376,7 @@ public function export(Model $model, array $fields = null, bool $typecast = true
*
* @param array<int, string>|null $fields
*/
public function initAction(Model $model, array $fields = null): Action
public function initAction(Model $model, ?array $fields = null): Action
{
if (is_object($model->table)) {
$tableAction = $this->action($model->table, 'select');
Expand Down
2 changes: 1 addition & 1 deletion src/Persistence/Csv.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ protected function insertRaw(Model $model, array $dataRaw)
*
* @return list<array<string, mixed>>
*/
public function export(Model $model, array $fields = null): array
public function export(Model $model, ?array $fields = null): array
{
$data = [];
foreach ($model as $entity) {
Expand Down
8 changes: 4 additions & 4 deletions src/Persistence/Sql.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ static function ($matches) use ($model, &$arguments) {
/**
* Creates new Query object with current time expression.
*/
public function exprNow(int $precision = null): Expression
public function exprNow(?int $precision = null): Expression
{
return $this->getConnection()->dsql()->exprNow($precision);
}
Expand Down Expand Up @@ -225,7 +225,7 @@ public function initField(Query $query, Field $field): void
*
* @param array<int, string>|null $fields
*/
public function initQueryFields(Model $model, Query $query, array $fields = null): void
public function initQueryFields(Model $model, Query $query, ?array $fields = null): void
{
// init fields
if ($fields !== null) {
Expand Down Expand Up @@ -329,7 +329,7 @@ private function fixMssqlOracleMissingFieldsInGroup(Model $model, Query $query):
}
}

private function _initQueryConditions(Query $query, Model\Scope\AbstractScope $condition = null): void
private function _initQueryConditions(Query $query, Model\Scope\AbstractScope $condition): void
{
if (!$condition->isEmpty()) {
// peel off the single nested scopes to convert (((field = value))) to field = value
Expand Down Expand Up @@ -512,7 +512,7 @@ public function tryLoad(Model $model, $id): ?array
*
* @return list<array<string, mixed>>
*/
public function export(Model $model, array $fields = null, bool $typecast = true): array
public function export(Model $model, ?array $fields = null, bool $typecast = true): array
{
$data = $model->action('select', [$fields])->getRows();

Expand Down
2 changes: 1 addition & 1 deletion src/Persistence/Sql/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ public function rollBack(): void
*
* Drivers like PostgreSQL need to receive sequence name to get ID because PDO doesn't support this method.
*/
public function lastInsertId(string $sequence = null): string
public function lastInsertId(?string $sequence = null): string
{
$res = $this->getConnection()->lastInsertId($sequence);

Expand Down
6 changes: 3 additions & 3 deletions src/Persistence/Sql/Expression.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public function expr($template = [], array $arguments = []): self
*
* @return $this
*/
public function reset(string $tag = null)
public function reset(?string $tag = null)
{
// unset all arguments
if ($tag === null) {
Expand Down Expand Up @@ -651,7 +651,7 @@ protected function _execute(?object $connection, bool $fromExecuteStatement)
/**
* @param DbalConnection|Connection $connection
*/
public function executeQuery(object $connection = null): DbalResult
public function executeQuery(?object $connection = null): DbalResult
{
return $this->_execute($connection, false);
}
Expand All @@ -661,7 +661,7 @@ public function executeQuery(object $connection = null): DbalResult
*
* @return int<0, max>
*/
public function executeStatement(object $connection = null): int
public function executeStatement(?object $connection = null): int
{
return $this->_execute($connection, true);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Persistence/Sql/Oracle/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ protected static function createDbalConfiguration(): Configuration
}

#[\Override]
public function lastInsertId(string $sequence = null): string
public function lastInsertId(?string $sequence = null): string
{
if ($sequence) {
return $this->dsql()->field($this->expr('{{}}.CURRVAL', [$sequence]))->getOne();
Expand Down
4 changes: 2 additions & 2 deletions src/Persistence/Sql/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ protected function _renderFrom(): ?string
*
* @return $this
*/
public function with(self $cursor, string $alias, array $fields = null, bool $recursive = false)
public function with(self $cursor, string $alias, ?array $fields = null, bool $recursive = false)
{
$this->_setArgs('with', $alias, [
'cursor' => $cursor,
Expand Down Expand Up @@ -1043,7 +1043,7 @@ public function dsql($defaults = [])
/**
* Returns Expression object for NOW() or CURRENT_TIMESTAMP() method.
*/
public function exprNow(int $precision = null): Expression
public function exprNow(?int $precision = null): Expression
{
return $this->expr(
'current_timestamp(' . ($precision !== null ? '[]' : '') . ')',
Expand Down
4 changes: 2 additions & 2 deletions src/Reference.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,15 @@ final protected function getOurFieldValue(Model $ourEntity)
return $ourEntity->get($this->getOurFieldName());
}

public function getTheirFieldName(Model $theirModel = null): string
public function getTheirFieldName(?Model $theirModel = null): string
{
return $this->theirField
?? ($theirModel ?? Model::assertInstanceOf($this->model))->idField;
}

/**
* @param \Closure<T of Model>(T, mixed, mixed, mixed, mixed, mixed, mixed, mixed, mixed, mixed, mixed): mixed $fx
* @param array<int, mixed> $args
* @param array<int, mixed> $args
*/
protected function onHookToOurModel(string $spot, \Closure $fx, array $args = [], int $priority = 5): int
{
Expand Down
2 changes: 1 addition & 1 deletion src/Reference/HasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ private function getModelTableString(Model $model): string
}

#[\Override]
public function getTheirFieldName(Model $theirModel = null): string
public function getTheirFieldName(?Model $theirModel = null): string
{
if ($this->theirField !== null) {
return $this->theirField;
Expand Down
2 changes: 1 addition & 1 deletion src/Reference/HasOneSql.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ private function _addField(string $fieldName, bool $theirFieldIsTitle, ?string $
*
* @param array<string, mixed> $defaults
*/
public function addField(string $fieldName, string $theirFieldName = null, array $defaults = []): SqlExpressionField
public function addField(string $fieldName, ?string $theirFieldName = null, array $defaults = []): SqlExpressionField
{
if ($theirFieldName === null) {
$theirFieldName = $fieldName;
Expand Down
2 changes: 1 addition & 1 deletion src/Reference/WeakAnalysingMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ final protected function makeHashFromKeyUpdate(\HashContext $hashContext, string
*
* @return ($hashContext is null ? int : null)
*/
protected function makeHashFromKey($value, \HashContext $hashContext = null): ?int
protected function makeHashFromKey($value, ?\HashContext $hashContext = null): ?int
{
if ($hashContext === null) {
$hashContext = hash_init('crc32c');
Expand Down
4 changes: 2 additions & 2 deletions src/Schema/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ protected static function assertSameExportUnordered(array $expected, array $actu
self::assertSame($expected, $actual, $message);
}

public function createMigrator(Model $model = null): Migrator
public function createMigrator(?Model $model = null): Migrator
{
$migrator = new Migrator($model ?? $this->db);
$this->createdMigrators[] = $migrator;
Expand Down Expand Up @@ -382,7 +382,7 @@ public function setDb(array $dbData, bool $importData = true): void
*
* @return array<string, array<int, array<string, mixed>>>
*/
public function getDb(array $tableNames = null, bool $noId = false): array
public function getDb(?array $tableNames = null, bool $noId = false): array
{
if ($tableNames === null) {
$tableNames = [];
Expand Down
2 changes: 1 addition & 1 deletion src/Schema/TestSqlPersistence.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function getConnection(): Persistence\Sql\Connection
// @phpstan-ignore-next-line SQLLogger is deprecated
new class() implements SQLLogger {
#[\Override]
public function startQuery($sql, array $params = null, array $types = null): void
public function startQuery($sql, ?array $params = null, ?array $types = null): void
{
// log transaction savepoint operations only once
// https://github.com/doctrine/dbal/blob/3.6.7/src/Connection.php#L1365
Expand Down
2 changes: 1 addition & 1 deletion src/ValidationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ValidationException extends Exception
*
* @return \Exception
*/
public function __construct(array $errors, Model $model = null)
public function __construct(array $errors, ?Model $model = null)
{
if (count($errors) === 0) {
throw new Exception('At least one error must be given');
Expand Down
2 changes: 1 addition & 1 deletion tests/JoinSqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class JoinSqlTest extends TestCase
/**
* @param Reference|Join $relation
*/
protected function assertMigratorResolveRelation(string $expectedLocalField, string $expectedForeignField, $relation, bool $resolveToPersistence = null): void
protected function assertMigratorResolveRelation(string $expectedLocalField, string $expectedForeignField, $relation, ?bool $resolveToPersistence = null): void
{
if ($resolveToPersistence === null) {
$this->assertMigratorResolveRelation($expectedLocalField, $expectedForeignField, $relation, false);
Expand Down
Loading

0 comments on commit 3042f52

Please sign in to comment.