Skip to content

Commit 09096ce

Browse files
committed
Code cleanup
1 parent 9f4d333 commit 09096ce

File tree

5 files changed

+27
-55
lines changed

5 files changed

+27
-55
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ permissions:
1313

1414
jobs:
1515
testsuite:
16-
uses: cakephp/.github/.github/workflows/testsuite-with-db.yml@5.x
17-
secrets: inherit
16+
uses: ADmad/.github/.github/workflows/testsuite-with-db.yml@master
17+
secrets:
18+
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
1819

1920
cs-stan:
20-
uses: cakephp/.github/.github/workflows/cs-stan.yml@5.x
21-
secrets: inherit
21+
uses: ADmad/.github/.github/workflows/cs-stan.yml@master

.phive/phars.xml

Lines changed: 0 additions & 5 deletions
This file was deleted.

phpstan.neon

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
parameters:
2-
level: 6
3-
checkMissingIterableValueType: false
4-
checkGenericClassInNonGenericObjectType: false
2+
level: 8
53
paths:
64
- src/
75
ignoreErrors:
86
- '#Call to an undefined method Cake\\ORM\\Table::cascadingRestoreTrash\(\)#'
7+
- identifier: missingType.iterableValue
8+
- identifier: missingType.generics

src/Model/Behavior/TrashBehavior.php

Lines changed: 19 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,13 @@
99
use Cake\Database\Expression\BetweenExpression;
1010
use Cake\Database\Expression\ComparisonExpression;
1111
use Cake\Database\Expression\IdentifierExpression;
12-
use Cake\Database\Expression\QueryExpression;
13-
use Cake\Database\Expression\UnaryExpression;
1412
use Cake\Database\Query\SelectQuery;
1513
use Cake\Datasource\EntityInterface;
1614
use Cake\Event\EventInterface;
1715
use Cake\I18n\DateTime;
1816
use Cake\ORM\Association;
1917
use Cake\ORM\Behavior;
2018
use Cake\ORM\Table;
21-
use Closure;
2219
use InvalidArgumentException;
2320
use function Cake\Core\pluginSplit;
2421

@@ -56,7 +53,7 @@ class TrashBehavior extends Behavior
5653
*/
5754
public function initialize(array $config): void
5855
{
59-
if (!empty($config['events'])) {
56+
if (isset($config['events']) && $config['events'] !== []) {
6057
$this->setConfig('events', $config['events'], false);
6158
}
6259
}
@@ -70,10 +67,12 @@ public function initialize(array $config): void
7067
public function implementedEvents(): array
7168
{
7269
$events = [];
73-
if ($this->getConfig('events') === false) {
70+
$config = $this->getConfig('events');
71+
if ($config === false) {
7472
return $events;
7573
}
76-
foreach ((array)$this->getConfig('events') as $eventKey => $event) {
74+
75+
foreach ((array)$config as $eventKey => $event) {
7776
if (is_numeric($eventKey)) {
7877
$eventKey = $event;
7978
$event = null;
@@ -84,13 +83,16 @@ public function implementedEvents(): array
8483
if (!is_array($event)) {
8584
throw new InvalidArgumentException('Event should be string or array');
8685
}
87-
$priority = $this->getConfig('priority');
86+
8887
if (!array_key_exists('callable', $event) || $event['callable'] === null) {
8988
[, $event['callable']] = pluginSplit($eventKey);
9089
}
90+
91+
$priority = $this->getConfig('priority');
9192
if ($priority && !array_key_exists('priority', $event)) {
9293
$event['priority'] = $priority;
9394
}
95+
9496
$events[$eventKey] = $event;
9597
}
9698

@@ -108,7 +110,7 @@ public function implementedEvents(): array
108110
*/
109111
public function beforeDelete(EventInterface $event, EntityInterface $entity, ArrayObject $options): void
110112
{
111-
if ($options->offsetExists('purge') && $options['purge'] === true) {
113+
if (isset($options['purge']) && $options['purge'] === true) {
112114
return;
113115
}
114116

@@ -159,14 +161,9 @@ public function trash(EntityInterface $entity, array $options = []): bool
159161
}
160162
}
161163

162-
$data = [$this->getTrashField(false) => new DateTime()];
163-
$entity->set($data, ['guard' => false]);
164-
165-
if ($this->_table->save($entity, $options)) {
166-
return true;
167-
}
164+
$entity->set($this->getTrashField(false), new DateTime());
168165

169-
return false;
166+
return (bool)$this->_table->save($entity, $options);
170167
}
171168

172169
/**
@@ -238,7 +235,7 @@ public function findOnlyTrashed(SelectQuery $query, array $options): SelectQuery
238235
{
239236
return $query
240237
->applyOptions(['skipAddTrashCondition' => true])
241-
->andWhere($query->newExpr()->isNotNull($this->getTrashField()));
238+
->andWhere([$this->getTrashField() . ' IS NOT' => null]);
242239
}
243240

244241
/**
@@ -250,9 +247,7 @@ public function findOnlyTrashed(SelectQuery $query, array $options): SelectQuery
250247
*/
251248
public function findWithTrashed(SelectQuery $query, array $options = []): SelectQuery
252249
{
253-
return $query->applyOptions([
254-
'skipAddTrashCondition' => true,
255-
]);
250+
return $query->applyOptions(['skipAddTrashCondition' => true]);
256251
}
257252

258253
/**
@@ -277,7 +272,7 @@ public function trashAll(mixed $conditions): int
277272
*/
278273
public function emptyTrash(): int
279274
{
280-
return $this->_table->deleteAll($this->_getUnaryExpression());
275+
return $this->_table->deleteAll([$this->getTrashField(false) . ' IS NOT' => null]);
281276
}
282277

283278
/**
@@ -300,7 +295,7 @@ public function restoreTrash(?EntityInterface $entity = null, array $options = [
300295
return $this->_table->save($entity, $options);
301296
}
302297

303-
return $this->_table->updateAll($data, $this->_getUnaryExpression());
298+
return $this->_table->updateAll($data, [$this->getTrashField(false) . ' IS NOT' => null]);
304299
}
305300

306301
/**
@@ -346,21 +341,6 @@ public function cascadingRestoreTrash(
346341
return $result;
347342
}
348343

349-
/**
350-
* Returns a unary expression for bulk record manipulation.
351-
*
352-
* @return \Closure
353-
*/
354-
protected function _getUnaryExpression(): Closure
355-
{
356-
return fn (QueryExpression $queryExpression): QueryExpression => $queryExpression
357-
->add(new UnaryExpression(
358-
'IS NOT NULL',
359-
$this->getTrashField(false),
360-
UnaryExpression::POSTFIX
361-
));
362-
}
363-
364344
/**
365345
* Returns the table's field used to mark a `trashed` row.
366346
*
@@ -371,7 +351,7 @@ public function getTrashField(bool $aliased = true): string
371351
{
372352
$field = $this->getConfig('field');
373353

374-
if (empty($field)) {
354+
if ($field === null) {
375355
$columns = $this->_table->getSchema()->columns();
376356
foreach (['deleted', 'trashed'] as $name) {
377357
if (in_array($name, $columns, true)) {
@@ -380,12 +360,9 @@ public function getTrashField(bool $aliased = true): string
380360
}
381361
}
382362

383-
/** @psalm-suppress RedundantCondition */
384-
if (empty($field)) {
385-
$field = Configure::read('Muffin/Trash.field');
386-
}
363+
$field ??= Configure::read('Muffin/Trash.field');
387364

388-
if (empty($field)) {
365+
if ($field === null) {
389366
throw new CakeException('TrashBehavior: "field" config needs to be provided.');
390367
}
391368

tests/TestCase/Model/Behavior/TrashBehaviorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -874,7 +874,7 @@ public function testGetTrashFieldUsesConfiguredValue()
874874
*/
875875
public function testGetTrashFieldFallbackToDefault()
876876
{
877-
$trash = new TrashBehavior($this->Articles, ['field' => '']);
877+
$trash = new TrashBehavior($this->Articles);
878878

879879
$this->assertEmpty($trash->getConfig('field'));
880880
$this->assertEquals('Articles.trashed', $trash->getTrashField());

0 commit comments

Comments
 (0)