Skip to content

Commit

Permalink
Fix phpstan v1.8.3 (#1061)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Sep 1, 2022
1 parent a78e203 commit ca0251d
Show file tree
Hide file tree
Showing 27 changed files with 236 additions and 314 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test-unit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ jobs:
ACCEPT_EULA: Y
SA_PASSWORD: atk4_pass
oracle:
image: gvenzl/oracle-xe:18
image: gvenzl/oracle-xe:18-slim-faststart
env:
ORACLE_PASSWORD: atk4_pass
steps:
Expand Down
3 changes: 0 additions & 3 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@
// fn => without curly brackets is less readable,
// also prevent bounding of unwanted variables for GC
'use_arrow_functions' => false,

// @TODO fix later
'php_unit_strict' => false,
])
->setFinder($finder)
->setCacheFile(sys_get_temp_dir() . '/php-cs-fixer.' . md5(__DIR__) . '.cache');
100 changes: 0 additions & 100 deletions CONTRIBUTING.md

This file was deleted.

2 changes: 0 additions & 2 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
ignore:
- docs
comment: false
coverage:
status:
Expand Down
74 changes: 32 additions & 42 deletions docs/advanced.rst
Original file line number Diff line number Diff line change
Expand Up @@ -240,46 +240,36 @@ Start by creating a class::
}
}

public function softDelete(Model $m)
public function softDelete(Model $entity)
{
$m->assertIsLoaded();
$entity->assertIsLoaded();

$id = $m->getId();
if ($m->hook('beforeSoftDelete') === false) {
return $m;
$id = $entity->getId();
if ($entity->hook('beforeSoftDelete') === false) {
return $entity;
}

$reloadAfterSaveBackup = $m->getModel()->reloadAfterSave;
try {
$m->getModel()->reloadAfterSave = false;
$m->save(['is_deleted' => true])->unload();
} finally {
$m->getModel()->reloadAfterSave = $reloadAfterSaveBackup;
}
$entity->saveAndUnload(['is_deleted' => true]);

$entity->hook('afterSoftDelete', [$id]);

$m->hook('afterSoftDelete', [$id]);
return $m;
return $entity;
}

public function restore(Model $m)
public function restore(Model $entity)
{
$m->assertIsLoaded();
$entity->assertIsLoaded();

$id = $m->getId();
if ($m->hook('beforeRestore') === false) {
return $m;
$id = $entity->getId();
if ($entity->hook('beforeRestore') === false) {
return $entity;
}

$reloadAfterSaveBackup = $m->getModel()->reloadAfterSave;
try {
$m->getModel()->reloadAfterSave = false;
$m->save(['is_deleted' => false])->unload();
} finally {
$m->getModel()->reloadAfterSave = $reloadAfterSaveBackup;
}
$entity->saveAndUnload(['is_deleted' => false]);

$entity->hook('afterRestore', [$id]);

$m->hook('afterRestore', [$id]);
return $m;
return $entity;
}
}

Expand Down Expand Up @@ -337,13 +327,13 @@ before and just slightly modifying it::
$this->getOwner()->onHook(Model::HOOK_BEFORE_DELETE, \Closure::fromCallable([$this, 'softDelete']), null, 100);
}

public function softDelete(Model $m)
public function softDelete(Model $entity)
{
parent::softDelete();

$m->hook(Model::HOOK_AFTER_DELETE);
$entity->hook(Model::HOOK_AFTER_DELETE);

$m->breakHook(false); // this will cancel original delete()
$entity->breakHook(false); // this will cancel original delete()
}
}

Expand Down Expand Up @@ -393,18 +383,18 @@ inside your model are unique::
$this->getOwner()->onHook(Model::HOOK_BEFORE_SAVE, \Closure::fromCallable([$this, 'beforeSave']));
}

protected function beforeSave(Model $m)
protected function beforeSave(Model $entity)
{
foreach ($this->fields as $field) {
if ($m->getDirtyRef()[$field]) {
$mm = clone $m;
$mm->addCondition($mm->idField != $this->id);
$mm = $mm->tryLoadBy($field, $m->get($field));
if ($entity->getDirtyRef()[$field]) {
$modelCloned = clone $entity->getModel();
$modelCloned->addCondition($entity->idField != $this->id);
$entityCloned = $modelCloned->tryLoadBy($field, $entity->get($field));

if ($mm !== null) {
if ($entityCloned !== null) {
throw (new \Atk4\Data\Exception('Duplicate record exists'))
->addMoreInfo('field', $field)
->addMoreInfo('value', $m->get($field));
->addMoreInfo('value', $entity->get($field));
}
}
}
Expand Down Expand Up @@ -719,10 +709,10 @@ so::
You should never call save() inside afterSave hook, but if you wish to do some
further manipulation, you can reload a clone::

$mm = clone $m;
$mm->reload();
if ($mm->get('amount_due') == 0) {
$mm->save(['status' => 'paid']);
$entityCloned = clone $entity;
$entityCloned->reload();
if ($entityCloned->get('amount_due') == 0) {
$entityCloned->save(['status' => 'paid']);
}

Related Record Conditioning
Expand Down
2 changes: 1 addition & 1 deletion docs/persistence/sql/queries.rst
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ The example above will perform a select query first:

If a single row can be retrieved, then the update will be performed:

- `update user set name="John", surname="Smith", revision=revision + 1 where id = 123`
- `update user set name = "John", surname = "Smith", revision = revision + 1 where id = 123`

Otherwise an insert operation will be performed:

Expand Down
4 changes: 2 additions & 2 deletions docs/persistence/sql/transactions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ It is recommended to always use atomic() in your code.
exception, whole transaction will be automatically rolled back::

$c->atomic(function () use ($c) {
$c->dsql('user')->set('balance=balance + 10')->where('id', 10)->mode('update')->executeStatement();
$c->dsql('user')->set('balance=balance - 10')->where('id', 14)->mode('update')->executeStatement();
$c->dsql('user')->set('balance = balance + 10')->where('id', 10)->mode('update')->executeStatement();
$c->dsql('user')->set('balance = balance - 10')->where('id', 14)->mode('update')->executeStatement();
});

atomic() can be nested.
Expand Down
2 changes: 1 addition & 1 deletion src/Field/SqlExpressionField.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class SqlExpressionField extends Field
public bool $neverSave = true;
public bool $readOnly = true;

/** @var \Closure(object, Expression): (string|Expressionable)|string|Expressionable Used expression. */
/** @var \Closure(Model, Expression): (string|Expressionable)|string|Expressionable Used expression. */
public $expr;

/** @var string Specifies how to aggregate this. */
Expand Down
6 changes: 3 additions & 3 deletions src/Model/UserAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ class UserAction
/** @var string How this action interact with record */
public $modifier;

/** @var \Closure(object, mixed ...$args): mixed|string code to execute. By default will call entity method with same name */
/** @var \Closure|string code to execute. By default will call entity method with same name */
public $callback;

/** @var \Closure(object, mixed ...$args): mixed|string identical to callback, but would generate preview of action without permanent effect */
/** @var \Closure|string identical to callback, but would generate preview of action without permanent effect */
public $preview;

/** @var string|null caption to put on the button */
Expand All @@ -59,7 +59,7 @@ class UserAction
/** @var bool|string|\Closure(static): string Will ask user to confirm. */
public $confirmation = false;

/** @var bool|\Closure(object): bool setting this to false will disable action. */
/** @var bool|\Closure(Model): bool setting this to false will disable action. */
public $enabled = true;

/** @var bool system action will be hidden from UI, but can still be explicitly triggered */
Expand Down
5 changes: 3 additions & 2 deletions src/Model/UserActionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Atk4\Data\Model;

use Atk4\Core\Factory;
use Atk4\Data\Model;

trait UserActionsTrait
{
Expand Down Expand Up @@ -139,8 +140,8 @@ protected function initUserActions(): void
$this->addUserAction('delete', [
'appliesTo' => UserAction::APPLIES_TO_SINGLE_RECORD,
'modifier' => UserAction::MODIFIER_DELETE,
'callback' => function ($model) {
return $model->delete();
'callback' => function (Model $entity) {
return $entity->delete();
},
]);

Expand Down
2 changes: 1 addition & 1 deletion src/Persistence/Array_/Db/Row.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function updateValues(array $data): void
}

$that = $this;
\Closure::bind(function () use ($owner, $that, $newData) { // @phpstan-ignore-line https://github.com/phpstan/phpstan/issues/7862
\Closure::bind(function () use ($owner, $that, $newData) {
$owner->beforeValuesSet($that, $newData);
}, null, $owner)();

Expand Down
2 changes: 1 addition & 1 deletion src/Persistence/Array_/Db/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public function addRow(string $rowClass, array $rowData): Row
$that = $this;
$columnNames = $this->getColumnNames();
/** @var Row $row */
$row = \Closure::bind(function () use ($that, $rowClass, $columnNames) { // @phpstan-ignore-line https://github.com/phpstan/phpstan/issues/7862
$row = \Closure::bind(function () use ($that, $rowClass, $columnNames) {
$row = new $rowClass($that);
foreach ($columnNames as $columnName) {
$row->initValue($columnName);
Expand Down
2 changes: 1 addition & 1 deletion src/Reference.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class Reference
* then used inside getModel() to fully populate and associate with
* persistence.
*
* @var Model|\Closure(object, static, array): Model|array
* @var Model|\Closure(Model, static, array): Model|array
*/
public $model;

Expand Down
6 changes: 6 additions & 0 deletions src/Schema/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,12 @@ public function getDb(array $tableNames = null, bool $noId = false): array
$idColumnName = isset($row['_id']) ? '_id' : 'id';
}

foreach ($row as $k => $v) {
if (preg_match('~(?:^|_)id$~', $k) && $v === (string) (int) $v) {
$row[$k] = (int) $v;
}
}

if ($noId) {
unset($row[$idColumnName]);
$res[] = $row;
Expand Down
Loading

0 comments on commit ca0251d

Please sign in to comment.