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

CS and type improvements #995

Merged
merged 8 commits into from
May 27, 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
4 changes: 2 additions & 2 deletions src/Field/SqlExpressionField.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class SqlExpressionField extends Field
init as private _init;
}

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

/** @var bool Expressions are always read_only. */
Expand Down Expand Up @@ -83,7 +83,7 @@ public function getDsqlExpression(Expression $expression): Expression

// Otherwise call it from expression itself
return $expression->expr('([])', [$expression->expr($expr)]);
} elseif ($expr instanceof Expressionable && !$expr instanceof Expression) {
} elseif ($expr instanceof Expressionable && !$expr instanceof Expression) { // @phpstan-ignore-line
return $expression->expr('[]', [$expr]);
}

Expand Down
50 changes: 21 additions & 29 deletions src/Model/UserAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class UserAction
public const APPLIES_TO_MULTIPLE_RECORDS = 'multiple'; // e.g. delete
public const APPLIES_TO_ALL_RECORDS = 'all'; // e.g. truncate

/** @var string by default - action is for a single-record */
/** @var string by default action is for a single record */
public $appliesTo = self::APPLIES_TO_SINGLE_RECORD;

/** Defining action modifier */
Expand All @@ -41,28 +41,25 @@ class UserAction
public const MODIFIER_DELETE = 'delete'; // delete record(s)
public const MODIFIER_READ = 'read'; // just read, does not modify record(s)

/** @var string How this action interact with record. default = 'read' */
public $modifier = self::MODIFIER_READ;
/** @var string How this action interact with record */
public $modifier;

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

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

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

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

/** @var bool Specifies that the action is dangerous. Should be displayed in red. */
public $dangerous = false;

/** @var bool|string|\Closure Set this to "true", string or return the value from the callback. Will ask user to confirm. */
/** @var bool|string|\Closure(static): string Will ask user to confirm. */
public $confirmation = false;

/** @var bool|\Closure setting this to false will disable action. Callback will be executed with ($m) and must return bool */
/** @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 Expand Up @@ -130,28 +127,23 @@ public function getActionForEntity(Model $entity): self
*/
public function execute(...$args)
{
if ($this->callback === null) {
$fx = \Closure::fromCallable([$this->getEntity(), $this->shortName]);
} elseif (is_string($this->callback)) {
$fx = \Closure::fromCallable([$this->getEntity(), $this->callback]);
} else {
array_unshift($args, $this->getEntity());
$fx = $this->callback;
}

// todo - ACL tests must allow

try {
$this->validateBeforeExecute();

$run = function () use ($args) {
if ($this->callback === null) {
$fx = [$this->getEntity(), $this->shortName];
} elseif (is_string($this->callback)) {
$fx = [$this->getEntity(), $this->callback];
} else {
array_unshift($args, $this->getEntity());
$fx = $this->callback;
}

return $fx(...$args);
};

if ($this->atomic) {
return $this->getModel()->atomic($run);
}

return $run();
return $this->atomic === false
? $fx(...$args)
: $this->getModel()->atomic(static fn () => $fx(...$args));
} catch (CoreException $e) {
$e->addMoreInfo('action', $this);

Expand Down
4 changes: 2 additions & 2 deletions src/Persistence/Sql/Mssql/ExpressionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ public function execute(object $connection = null): DbalResult
$eDriver = $e->getPrevious();
if ($eDriver !== null && $eDriver instanceof DriverException && $eDriver->getCode() === 544) {
try {
return $executeFx('set IDENTITY_INSERT [table_noalias] on;'
. "\n" . 'insert[option] into [table_noalias] ([set_fields]) values ([set_values]);');
return $executeFx('set IDENTITY_INSERT [table_noalias] on;' . "\n"
. 'insert[option] into [table_noalias] ([set_fields]) values ([set_values]);');
} finally {
$executeFx('set IDENTITY_INSERT [table_noalias] off;');
}
Expand Down
4 changes: 2 additions & 2 deletions 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|array
* @var Model|\Closure(Model, static, array): Model|array
*/
public $model;

Expand Down Expand Up @@ -117,7 +117,7 @@ final protected function getOurFieldValue(Model $ourEntity)

public function getTheirFieldName(): string
{
return $this->their_field ?? $this->model->id_field;
return $this->their_field ?? Model::assertInstanceOf($this->model)->id_field;
}

protected function onHookToOurModel(Model $model, string $spot, \Closure $fx, array $args = [], int $priority = 5): int
Expand Down
3 changes: 2 additions & 1 deletion tests/ConditionSqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,8 @@ public function testExpressionJoin(): void
1 => ['id' => 1, 'name' => 'John', 'surname' => 'Smith', 'gender' => 'M', 'contact_id' => 1],
2 => ['id' => 2, 'name' => 'Sue', 'surname' => 'Sue', 'gender' => 'F', 'contact_id' => 2],
3 => ['id' => 3, 'name' => 'Peter', 'surname' => 'Smith', 'gender' => 'M', 'contact_id' => 1],
], 'contact' => [
],
'contact' => [
1 => ['id' => 1, 'contact_phone' => '+123 smiths'],
2 => ['id' => 2, 'contact_phone' => '+321 sues'],
],
Expand Down
36 changes: 24 additions & 12 deletions tests/JoinArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ public function testJoinSaving1(): void
'user' => [
1 => ['name' => 'John', 'contact_id' => 1],
2 => ['name' => 'Peter', 'contact_id' => 1],
], 'contact' => [
],
'contact' => [
1 => ['contact_phone' => '+123'],
],
], $this->getInternalPersistenceData($db));
Expand All @@ -108,7 +109,8 @@ public function testJoinSaving1(): void
1 => ['name' => 'John', 'contact_id' => 1],
2 => ['name' => 'Peter', 'contact_id' => 1],
3 => ['name' => 'Joe', 'contact_id' => 2],
], 'contact' => [
],
'contact' => [
1 => ['contact_phone' => '+123'],
2 => ['contact_phone' => '+321'],
],
Expand Down Expand Up @@ -142,7 +144,8 @@ public function testJoinSaving2(): void
'user' => [
1 => ['name' => 'John'],
2 => ['name' => 'Peter'],
], 'contact' => [
],
'contact' => [
1 => ['test_id' => 1, 'contact_phone' => '+123'],
2 => ['test_id' => 2, 'contact_phone' => null],
],
Expand All @@ -162,7 +165,8 @@ public function testJoinSaving2(): void
1 => ['name' => 'John'],
2 => ['name' => 'Peter'],
3 => ['name' => 'Sue'],
], 'contact' => [
],
'contact' => [
1 => ['test_id' => 1, 'contact_phone' => '+123'],
3 => ['test_id' => 3, 'contact_phone' => '+444'],
],
Expand Down Expand Up @@ -219,7 +223,8 @@ public function testJoinLoading(): void
1 => ['name' => 'John', 'contact_id' => 1],
2 => ['name' => 'Peter', 'contact_id' => 1],
3 => ['name' => 'Joe', 'contact_id' => 2],
], 'contact' => [
],
'contact' => [
1 => ['contact_phone' => '+123'],
2 => ['contact_phone' => '+321'],
],
Expand Down Expand Up @@ -253,7 +258,8 @@ public function testJoinUpdate(): void
1 => ['name' => 'John', 'contact_id' => 1],
2 => ['name' => 'Peter', 'contact_id' => 1],
3 => ['name' => 'Joe', 'contact_id' => 2],
], 'contact' => [
],
'contact' => [
1 => ['contact_phone' => '+123'],
2 => ['contact_phone' => '+321'],
],
Expand All @@ -274,7 +280,8 @@ public function testJoinUpdate(): void
1 => ['name' => 'John 2', 'contact_id' => 1],
2 => ['name' => 'Peter', 'contact_id' => 1],
3 => ['name' => 'Joe', 'contact_id' => 2],
], 'contact' => [
],
'contact' => [
1 => ['contact_phone' => '+555'],
2 => ['contact_phone' => '+321'],
],
Expand All @@ -290,7 +297,8 @@ public function testJoinUpdate(): void
1 => ['name' => 'John 2', 'contact_id' => 1],
2 => ['name' => 'Peter', 'contact_id' => 1],
3 => ['name' => 'XX', 'contact_id' => 2],
], 'contact' => [
],
'contact' => [
1 => ['contact_phone' => '+555'],
2 => ['contact_phone' => '+999'],
],
Expand All @@ -307,7 +315,8 @@ public function testJoinUpdate(): void
2 => ['name' => 'Peter', 'contact_id' => 1],
3 => ['name' => 'XX', 'contact_id' => 2],
4 => ['name' => 'YYY', 'contact_id' => 3],
], 'contact' => [
],
'contact' => [
1 => ['contact_phone' => '+555'],
2 => ['contact_phone' => '+999'],
3 => ['contact_phone' => '+777'],
Expand All @@ -323,7 +332,8 @@ public function testJoinDelete(): void
2 => ['name' => 'Peter', 'contact_id' => 1],
3 => ['name' => 'XX', 'contact_id' => 2],
4 => ['name' => 'YYY', 'contact_id' => 3],
], 'contact' => [
],
'contact' => [
1 => ['contact_phone' => '+555'],
2 => ['contact_phone' => '+999'],
3 => ['contact_phone' => '+777'],
Expand All @@ -343,7 +353,8 @@ public function testJoinDelete(): void
2 => ['name' => 'Peter', 'contact_id' => 1],
3 => ['name' => 'XX', 'contact_id' => 2],
4 => ['name' => 'YYY', 'contact_id' => 3],
], 'contact' => [
],
'contact' => [
2 => ['contact_phone' => '+999'],
3 => ['contact_phone' => '+777'],
],
Expand All @@ -357,7 +368,8 @@ public function testLoadMissing(): void
2 => ['name' => 'Peter', 'contact_id' => 1],
3 => ['name' => 'XX', 'contact_id' => 2],
4 => ['name' => 'YYY', 'contact_id' => 3],
], 'contact' => [
],
'contact' => [
2 => ['contact_phone' => '+999'],
3 => ['contact_phone' => '+777'],
],
Expand Down
Loading