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

Upgrade phpstan to v1.0 #910

Merged
merged 5 commits into from
Nov 3, 2021
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
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,22 @@
"ext-pdo": "*",
"atk4/core": "dev-develop",
"doctrine/dbal": "^2.13.3 || ^3.0",
"mvorisek/atk4-hintable": "~1.6.0"
"mvorisek/atk4-hintable": "~1.7.0"
},
"require-release": {
"php": ">=7.4 <8.2",
"ext-intl": "*",
"ext-pdo": "*",
"atk4/core": "~3.1.0",
"doctrine/dbal": "^2.13.3 || ^3.0",
"mvorisek/atk4-hintable": "~1.6.0"
"mvorisek/atk4-hintable": "~1.7.0"
},
"require-dev": {
"ergebnis/composer-normalize": "^2.13",
"friendsofphp/php-cs-fixer": "^3.0",
"johnkary/phpunit-speedtrap": "^3.3",
"phpstan/extension-installer": "^1.1",
"phpstan/phpstan": "^0.12.82",
"phpstan/phpstan": "^1.0",
"phpunit/phpunit": "^9.5.5"
},
"suggest": {
Expand Down
2 changes: 1 addition & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ parameters:
level: 6
paths:
- ./
excludes_analyse:
excludePaths:
- cache/
- build/
- vendor/
Expand Down
4 changes: 2 additions & 2 deletions src/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ public function getPersistenceName(): string
*/
public function useAlias(): bool
{
return isset($this->actual);
return $this->actual !== null;
}

// }}}
Expand Down Expand Up @@ -475,7 +475,7 @@ public function __debugInfo(): array
foreach ([
'type', 'system', 'never_persist', 'never_save', 'read_only', 'ui', 'joinName',
] as $key) {
if (isset($this->{$key})) {
if ($this->{$key} !== null) {
$arr[$key] = $this->{$key};
}
}
Expand Down
9 changes: 2 additions & 7 deletions src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,19 +131,14 @@ class Model implements \IteratorAggregate
* model normally lives. The interpretation of the table will be decoded
* by persistence driver.
*
* You can define this field as associative array where "key" is used
* as the name of persistence driver. Here is example for mysql and default:
*
* $table = ['user', 'mysql' => 'tbl_user'];
*
* @var string|array<0|string, string>|false
* @var string|false
*/
public $table;

/**
* Use alias for $table.
*
* @var string
* @var string|null
*/
public $table_alias;

Expand Down
4 changes: 2 additions & 2 deletions src/Model/FieldPropertiesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ trait FieldPropertiesTrait
/**
* Field type. Name of type registered in \Doctrine\DBAL\Types\Type.
*
* @var string
* @var string|null
*/
public $type;

Expand Down Expand Up @@ -90,7 +90,7 @@ trait FieldPropertiesTrait
* will always return meaningful label (even if caption is null). Set
* this property to any string.
*
* @var string
* @var string|null
*/
public $caption;

Expand Down
9 changes: 7 additions & 2 deletions src/Model/Join.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,13 @@ protected function init(): void
// split by LAST dot in foreign_table name
[$this->foreign_table, $this->foreign_field] = preg_split('~\.+(?=[^.]+$)~', $this->foreign_table);

if (!isset($this->reverse)) {
if ($this->reverse === null) {
$this->reverse = true;
}
}

if ($this->reverse === true) {
if (isset($this->master_field) && $this->master_field !== $id_field) { // TODO not implemented yet, see https://github.com/atk4/data/issues/803
if ($this->master_field && $this->master_field !== $id_field) { // TODO not implemented yet, see https://github.com/atk4/data/issues/803
throw (new Exception('Joining tables on non-id fields is not implemented yet'))
->addMoreInfo('condition', $this->getOwner()->table . '.' . $this->master_field . ' = ' . $this->foreign_table . '.' . $this->foreign_field);
}
Expand All @@ -211,6 +211,11 @@ protected function init(): void
}

$this->onHookShortToOwner(Model::HOOK_AFTER_UNLOAD, \Closure::fromCallable([$this, 'afterUnload']));

// if kind is not specified, figure out join type
if (!$this->kind) {
$this->kind = $this->weak ? 'left' : 'inner';
}
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Model/UserAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ class UserAction
/** @var \Closure|string code, identical to callback, but would generate preview of action without permanent effect */
public $preview;

/** @var string caption to put on the button */
/** @var string|null caption to put on the button */
public $caption;

/** @var string|\Closure a longer description of this action. Closure must return string. */
/** @var string|\Closure|null a longer description of this action. Closure must return string. */
public $description;

/** @var bool Specifies that the action is dangerous. Should be displayed in red. */
Expand Down
4 changes: 2 additions & 2 deletions src/Persistence.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ protected function _typecastSaveField(Field $field, $value)
throw new Exception('Must be instance of DateTimeInterface');
}

if ($field->type === 'datetime' && isset($field->persist_timezone)) {
if ($field->type === 'datetime') {
$value = new \DateTime($value->format('Y-m-d H:i:s.u'), $value->getTimezone());
$value->setTimezone(new \DateTimeZone($field->persist_timezone));
}
Expand Down Expand Up @@ -339,7 +339,7 @@ protected function _typecastLoadField(Field $field, $value)
}
}

if ($field->type === 'datetime' && isset($field->persist_timezone)) {
if ($field->type === 'datetime') {
$value = \DateTime::createFromFormat($format, $value, new \DateTimeZone($field->persist_timezone));
if ($value !== false) {
$value->setTimezone(new \DateTimeZone(date_default_timezone_get()));
Expand Down
7 changes: 1 addition & 6 deletions src/Persistence/Array_/Join.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,7 @@ protected function init(): void
{
parent::init();

// If kind is not specified, figure out join type
if (!isset($this->kind)) {
$this->kind = $this->weak ? 'left' : 'inner';
}

// Add necessary hooks
// add necessary hooks
if ($this->reverse) {
$this->onHookShortToOwner(Model::HOOK_AFTER_INSERT, \Closure::fromCallable([$this, 'afterInsert']), [], -5);
$this->onHookShortToOwner(Model::HOOK_BEFORE_UPDATE, \Closure::fromCallable([$this, 'beforeUpdate']), [], -5);
Expand Down
2 changes: 1 addition & 1 deletion src/Persistence/Sql.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public function add(Model $model, array $defaults = []): Model

$model = parent::add($model, $defaults);

if (!isset($model->table) || (!is_string($model->table) && $model->table !== false)) {
if ($model->table === null) {
throw (new Exception('Property $table must be specified for a model'))
->addMoreInfo('model', $model);
}
Expand Down
9 changes: 2 additions & 7 deletions src/Persistence/Sql/Join.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,14 @@ protected function init(): void

$this->getOwner()->persistence_data['use_table_prefixes'] = true;

// If kind is not specified, figure out join type
if (!isset($this->kind)) {
$this->kind = $this->weak ? 'left' : 'inner';
}

// Our short name will be unique
// our short name will be unique
if (!$this->foreign_alias) {
$this->foreign_alias = ($this->getOwner()->table_alias ?: '') . $this->short_name;
}

$this->onHookShortToOwner(Persistence\Sql::HOOK_INIT_SELECT_QUERY, \Closure::fromCallable([$this, 'initSelectQuery']));

// Add necessary hooks
// add necessary hooks
if ($this->reverse) {
$this->onHookShortToOwner(Model::HOOK_AFTER_INSERT, \Closure::fromCallable([$this, 'afterInsert']));
$this->onHookShortToOwner(Model::HOOK_BEFORE_UPDATE, \Closure::fromCallable([$this, 'beforeUpdate']));
Expand Down
8 changes: 4 additions & 4 deletions src/Reference.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@ class Reference
* This is an optional property which can be used by your implementation
* to store field-level relationship based on a common field matching.
*
* @var string
* @var string|null
*/
protected $their_field;

/**
* Caption of the referenced model. Can be used in UI components, for example.
* Should be in plain English and ready for proper localization.
*
* @var string
* @var string|null
*/
public $caption;

Expand Down Expand Up @@ -205,7 +205,7 @@ protected function initTableAlias(): void
$aliasFull = $this->link;
$alias = preg_replace('~_(' . preg_quote($ourModel->id_field, '~') . '|id)$~', '', $aliasFull);
$alias = preg_replace('~([0-9a-z]?)[0-9a-z]*[^0-9a-z]*~i', '$1', $alias);
if (isset($ourModel->table_alias)) {
if ($ourModel->table_alias !== null) {
$aliasFull = $ourModel->table_alias . '_' . $aliasFull;
$alias = preg_replace('~^_(.+)_[0-9a-f]{12}$~', '$1', $ourModel->table_alias) . '_' . $alias;
}
Expand Down Expand Up @@ -280,7 +280,7 @@ public function __debugInfo(): array
$arr = [];
foreach ($this->__debug_fields as $k => $v) {
$k = is_int($k) ? $v : $k;
if (isset($this->{$v})) {
if ($this->{$v} !== null) {
$arr[$k] = $this->{$v};
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Schema/Migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ protected function getReferenceField(Field $field): ?Field
return $reference->their_field;
}, null, \Atk4\Data\Reference::class)();

$referenceField = $referenceTheirField ?? $reference->getOwner()->id_field;
$referenceField = $referenceTheirField ?? $reference->getOwner()->id_field; // TODO migrate to $reference->getTheirFieldName()

$modelSeed = is_array($reference->model)
? $reference->model
Expand Down
2 changes: 1 addition & 1 deletion tests/Persistence/Sql/WithDb/SelectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ public function testOtherQueries(): void
// That's why we add usort here.
$data = $this->q('employee')->field('id,name')->getRows();
usort($data, function ($a, $b) {
return $a['id'] - $b['id'];
return $a['id'] - $b['id']; // @phpstan-ignore-line
});
$this->assertSame(
[['id' => '1', 'name' => 'Peter'], ['id' => '2', 'name' => 'Jane']],
Expand Down
9 changes: 0 additions & 9 deletions tests/Persistence/Sql/WithDb/TransactionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,6 @@ private function q($table = null, string $alias = null): Query
return $q;
}

/**
* @param string|array $template
* @param array $args
*/
private function e($template = [], array $args = null): Expression
{
return $this->c->expr($template, $args);
}

public function testCommitException1(): void
{
// try to commit when not in transaction
Expand Down