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

Fix phpstan v1.8.9 #1071

Merged
merged 7 commits into from
Oct 15, 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
8 changes: 4 additions & 4 deletions src/Field/EmailField.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
* Stores valid email as per configuration.
*
* Usage:
* $user->addField('email', [EmailField::class]);
* $user->addField('email_mx_check', [EmailField::class, 'dnsCheck' => true]);
* $user->addField('email_with_name', [EmailField::class, 'allowName' => true]);
* $user->addField('email', [EmailField::class]);
* $user->addField('email_mx_check', [EmailField::class, 'dnsCheck' => true]);
* $user->addField('email_with_name', [EmailField::class, 'allowName' => true]);
*/
class EmailField extends Field
{
Expand All @@ -32,7 +32,7 @@ public function normalize($value)

$email = trim($value);
if ($this->allowName) {
$email = preg_replace('/^[^<]*<([^>]*)>/', '\1', $email);
$email = preg_replace('~^[^<]*<([^>]*)>~', '\1', $email);
}

if (!str_contains($email, '@')) {
Expand Down
23 changes: 5 additions & 18 deletions src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,46 +56,32 @@ class Model implements \IteratorAggregate
use Model\UserActionsTrait;
use ReadableCaptionTrait;

/** @const string */
public const HOOK_BEFORE_LOAD = self::class . '@beforeLoad';
/** @const string */
public const HOOK_AFTER_LOAD = self::class . '@afterLoad';
/** @const string */
public const HOOK_BEFORE_UNLOAD = self::class . '@beforeUnload';
/** @const string */
public const HOOK_AFTER_UNLOAD = self::class . '@afterUnload';

/** @const string */
public const HOOK_BEFORE_INSERT = self::class . '@beforeInsert';
/** @const string */
public const HOOK_AFTER_INSERT = self::class . '@afterInsert';
/** @const string */
public const HOOK_BEFORE_UPDATE = self::class . '@beforeUpdate';
/** @const string */
public const HOOK_AFTER_UPDATE = self::class . '@afterUpdate';
/** @const string */
public const HOOK_BEFORE_DELETE = self::class . '@beforeDelete';
/** @const string */
public const HOOK_AFTER_DELETE = self::class . '@afterDelete';

/** @const string */
public const HOOK_BEFORE_SAVE = self::class . '@beforeSave';
/** @const string */
public const HOOK_AFTER_SAVE = self::class . '@afterSave';

/** @const string Executed when execution of self::atomic() failed. */
/** Executed when execution of self::atomic() failed. */
public const HOOK_ROLLBACK = self::class . '@rollback';

/** @const string Executed for every field set using self::set() method. */
/** Executed for every field set using self::set() method. */
public const HOOK_NORMALIZE = self::class . '@normalize';
/** @const string Executed when self::validate() method is called. */
/** Executed when self::validate() method is called. */
public const HOOK_VALIDATE = self::class . '@validate';
/** @const string Executed when self::setOnlyFields() method is called. */
/** Executed when self::setOnlyFields() method is called. */
public const HOOK_ONLY_FIELDS = self::class . '@onlyFields';

/** @const string */
protected const ID_LOAD_ONE = self::class . '@idLoadOne-h7axmDNBB3qVXjVv';
/** @const string */
protected const ID_LOAD_ANY = self::class . '@idLoadAny-h7axmDNBB3qVXjVv';

// {{{ Properties of the class
Expand Down Expand Up @@ -1686,6 +1672,7 @@ public function export(array $fields = null, string $keyField = null, bool $type

// no key field - then just do export
if ($keyField === null) {
// TODO this optimization should be removed in favor of one Persistence::export call and php calculated fields should be exported as well
return $this->getPersistence()->export($this, $fields, $typecast);
}

Expand Down
4 changes: 1 addition & 3 deletions src/Model/AggregateModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
* 'salary' => ['expr' => 'sum([])', 'type' => 'atk4_money'],
* ];
*
* your resulting model will have 3 fields:
* first, last, salary
* your resulting model will have 3 fields: first, last, salary
*
* but when querying it will use the original model to calculate the query, then add grouping and aggregates.
*
Expand All @@ -39,7 +38,6 @@
*/
class AggregateModel extends Model
{
/** @const string */
public const HOOK_INIT_AGGREGATE_SELECT_QUERY = self::class . '@initAggregateSelectQuery';

/** @var array<int, string|Expression> */
Expand Down
3 changes: 0 additions & 3 deletions src/Persistence.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,9 @@ abstract class Persistence
use HookTrait;
use NameTrait;

/** @const string */
public const HOOK_AFTER_ADD = self::class . '@afterAdd';

/** @const string */
public const ID_LOAD_ONE = self::class . '@idLoadOne-qZ5TJwMVJ4LzVhuN';
/** @const string */
public const ID_LOAD_ANY = self::class . '@idLoadAny-qZ5TJwMVJ4LzVhuN';

/** @internal prevent recursion */
Expand Down
4 changes: 2 additions & 2 deletions src/Persistence/Array_/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,9 @@ protected function evaluateIf($v1, string $operator, $v2): bool

break;
case 'LIKE':
$pattern = str_ireplace('%', '(.*?)', preg_quote($v2));
$pattern = str_ireplace('%', '(.*?)', preg_quote($v2, '~'));

$result = (bool) preg_match('/^' . $pattern . '$/', (string) $v1);
$result = (bool) preg_match('~^' . $pattern . '$~', (string) $v1);

break;
case 'NOT LIKE':
Expand Down
23 changes: 8 additions & 15 deletions src/Persistence/Sql.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,12 @@ class Sql extends Persistence
{
use Sql\BinaryTypeCompatibilityTypecastTrait;

/** @const string */
public const HOOK_INIT_SELECT_QUERY = self::class . '@initSelectQuery';
/** @const string */
public const HOOK_BEFORE_INSERT_QUERY = self::class . '@beforeInsertQuery';
/** @const string */
public const HOOK_AFTER_INSERT_QUERY = self::class . '@afterInsertQuery';
/** @const string */
public const HOOK_BEFORE_UPDATE_QUERY = self::class . '@beforeUpdateQuery';
/** @const string */
public const HOOK_AFTER_UPDATE_QUERY = self::class . '@afterUpdateQuery';
/** @const string */
public const HOOK_BEFORE_DELETE_QUERY = self::class . '@beforeDeleteQuery';
/** @const string */
public const HOOK_AFTER_DELETE_QUERY = self::class . '@afterDeleteQuery';

/** @var Connection */
Expand Down Expand Up @@ -97,14 +90,6 @@ public function disconnect(): void
$this->_connection = null; // @phpstan-ignore-line
}

/**
* Returns Query instance.
*/
public function dsql(): Query
{
return $this->getConnection()->dsql();
}

/**
* Atomic executes operations within one begin/end transaction, so if
* the code inside callback will fail, then all of the transaction
Expand Down Expand Up @@ -196,6 +181,14 @@ public function exprNow(int $precision = null): Expression
return $this->getConnection()->dsql()->exprNow($precision);
}

/**
* Creates new Query object.
*/
public function dsql(): Query
{
return $this->getConnection()->dsql();
}

/**
* Initializes base query for model $m.
*/
Expand Down
6 changes: 3 additions & 3 deletions src/Persistence/Sql/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public static function normalizeDsn($dsn, $user = null, $password = null)
* Adds connection class to the registry for resolving in Connection::resolve method.
*
* Can be used as:
* Connection::registerConnection(MySQL\Connection::class, 'pdo_mysql')
* Connection::registerConnection(MySQL\Connection::class, 'pdo_mysql')
*
* @param class-string<self> $connectionClass
*/
Expand Down Expand Up @@ -281,7 +281,7 @@ protected static function connectFromDbalDriverConnection(DbalDriverConnection $
}

/**
* Returns new Expression with connection already set.
* Create new Expression with connection already set.
*
* @param string|array<string, mixed> $template
* @param array<mixed> $arguments
Expand All @@ -296,7 +296,7 @@ public function expr($template = [], array $arguments = []): Expression
}

/**
* Returns new Query with connection already set.
* Create new Query with connection already set.
*
* @param string|array<string, mixed> $defaults
*/
Expand Down
8 changes: 4 additions & 4 deletions src/Persistence/Sql/Expression.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ abstract class Expression implements Expressionable, \ArrayAccess
{
use DiContainerTrait;

/** @const string "[]" in template, escape as parameter */
/** "[]" in template, escape as parameter */
protected const ESCAPE_PARAM = 'param';
/** @const string "{}" in template, escape as identifier */
/** "{}" in template, escape as identifier */
protected const ESCAPE_IDENTIFIER = 'identifier';
/** @const string "{{}}" in template, escape as identifier, but keep input with special characters like "." or "(" unescaped */
/** "{{}}" in template, escape as identifier, but keep input with special characters like "." or "(" unescaped */
protected const ESCAPE_IDENTIFIER_SOFT = 'identifier-soft';
/** @const string keep input as is */
/** Keep input as is */
protected const ESCAPE_NONE = 'none';

protected ?string $template = null;
Expand Down
4 changes: 2 additions & 2 deletions src/Persistence/Sql/Oracle/PlatformTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function getCreateAutoincrementSql($name, $table, $start = 1)
$pkSeq = \Closure::bind(fn () => $this->normalizeIdentifier($aiSequenceName), $this, OraclePlatform::class)()->getName();
$sqls[count($sqls) - 1] = $conn->expr(
// else branch should be maybe (because of concurrency) put into after update trigger
str_replace('[pk_seq]', '\'' . str_replace('\'', '\'\'', $pkSeq) . '\'', <<<'EOT'
str_replace('[pk_seq]', '\'' . str_replace('\'', '\'\'', $pkSeq) . '\'', <<<'EOF'
CREATE TRIGGER {{trigger}}
BEFORE INSERT OR UPDATE
ON {{table}}
Expand All @@ -89,7 +89,7 @@ public function getCreateAutoincrementSql($name, $table, $start = 1)
END LOOP;
END IF;
END;
EOT),
EOF),
[
'trigger' => \Closure::bind(fn () => $this->normalizeIdentifier($aiTriggerName), $this, OraclePlatform::class)()->getName(),
'table' => $tableIdentifier->getName(),
Expand Down
1 change: 0 additions & 1 deletion src/Util/DeepCopy.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class DeepCopy
{
use DebugTrait;

/** @const string */
public const HOOK_AFTER_COPY = self::class . '@afterCopy';

/** Model from which we want to copy records */
Expand Down
2 changes: 1 addition & 1 deletion tests/Model/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class Client extends User
{
public $table = 'client'; // @phpstan-ignore-line https://github.com/phpstan/phpstan/issues/7839
public $table = 'client';

protected function init(): void
{
Expand Down
89 changes: 43 additions & 46 deletions tests/Schema/TestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,52 +41,49 @@ public function testLogQuery(): void
return;
}

$this->assertSameSql(
<<<'EOF'

"START TRANSACTION";


insert into `t` (`name`, `int`, `float`, `null`)
values
('Ewa', 1, '1.0', NULL);


select
`id`,
`name`,
`int`,
`float`,
`null`
from
`t`
where
`int` > -1
and `id` = 1
limit
0,
2;


"COMMIT";


select
`id`,
`name`,
`int`,
`float`,
`null`
from
`t`
where
`int` > -1
limit
0,
1;
EOF . "\n\n",
$output
);
$this->assertSameSql(<<<'EOF'

"START TRANSACTION";


insert into `t` (`name`, `int`, `float`, `null`)
values
('Ewa', 1, '1.0', NULL);


select
`id`,
`name`,
`int`,
`float`,
`null`
from
`t`
where
`int` > -1
and `id` = 1
limit
0,
2;


"COMMIT";


select
`id`,
`name`,
`int`,
`float`,
`null`
from
`t`
where
`int` > -1
limit
0,
1;
EOF . "\n\n", $output);
}

public function testGetSetDropDb(): void
Expand Down
4 changes: 2 additions & 2 deletions tests/UserActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Atk4\Data\Persistence;
use Atk4\Data\Schema\TestCase;

trait UaReminder
trait UaReminderTrait
{
public function sendReminder(): string
{
Expand All @@ -26,7 +26,7 @@ public function backupClients(): string

class UaClient extends Model
{
use UaReminder;
use UaReminderTrait;

public $caption = 'UaClient';

Expand Down