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

Code cleanup #74

Merged
merged 1 commit into from
Feb 3, 2025
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 .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ permissions:

jobs:
testsuite:
uses: cakephp/.github/.github/workflows/[email protected]
secrets: inherit
uses: ADmad/.github/.github/workflows/testsuite-with-db.yml@master
secrets:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

cs-stan:
uses: cakephp/.github/.github/workflows/[email protected]
secrets: inherit
uses: ADmad/.github/.github/workflows/cs-stan.yml@master
5 changes: 0 additions & 5 deletions .phive/phars.xml

This file was deleted.

6 changes: 3 additions & 3 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
parameters:
level: 6
checkMissingIterableValueType: false
checkGenericClassInNonGenericObjectType: false
level: 8
paths:
- src/
ignoreErrors:
- '#Call to an undefined method Cake\\ORM\\Table::cascadingRestoreTrash\(\)#'
- identifier: missingType.iterableValue
- identifier: missingType.generics
61 changes: 19 additions & 42 deletions src/Model/Behavior/TrashBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,13 @@
use Cake\Database\Expression\BetweenExpression;
use Cake\Database\Expression\ComparisonExpression;
use Cake\Database\Expression\IdentifierExpression;
use Cake\Database\Expression\QueryExpression;
use Cake\Database\Expression\UnaryExpression;
use Cake\Database\Query\SelectQuery;
use Cake\Datasource\EntityInterface;
use Cake\Event\EventInterface;
use Cake\I18n\DateTime;
use Cake\ORM\Association;
use Cake\ORM\Behavior;
use Cake\ORM\Table;
use Closure;
use InvalidArgumentException;
use function Cake\Core\pluginSplit;

Expand Down Expand Up @@ -56,7 +53,7 @@ class TrashBehavior extends Behavior
*/
public function initialize(array $config): void
{
if (!empty($config['events'])) {
if (isset($config['events']) && $config['events'] !== []) {
$this->setConfig('events', $config['events'], false);
}
}
Expand All @@ -70,10 +67,12 @@ public function initialize(array $config): void
public function implementedEvents(): array
{
$events = [];
if ($this->getConfig('events') === false) {
$config = $this->getConfig('events');
if ($config === false) {
return $events;
}
foreach ((array)$this->getConfig('events') as $eventKey => $event) {

foreach ((array)$config as $eventKey => $event) {
if (is_numeric($eventKey)) {
$eventKey = $event;
$event = null;
Expand All @@ -84,13 +83,16 @@ public function implementedEvents(): array
if (!is_array($event)) {
throw new InvalidArgumentException('Event should be string or array');
}
$priority = $this->getConfig('priority');

if (!array_key_exists('callable', $event) || $event['callable'] === null) {
[, $event['callable']] = pluginSplit($eventKey);
}

$priority = $this->getConfig('priority');
if ($priority && !array_key_exists('priority', $event)) {
$event['priority'] = $priority;
}

$events[$eventKey] = $event;
}

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

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

$data = [$this->getTrashField(false) => new DateTime()];
$entity->set($data, ['guard' => false]);

if ($this->_table->save($entity, $options)) {
return true;
}
$entity->set($this->getTrashField(false), new DateTime());

return false;
return (bool)$this->_table->save($entity, $options);
}

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

/**
Expand All @@ -250,9 +247,7 @@ public function findOnlyTrashed(SelectQuery $query, array $options): SelectQuery
*/
public function findWithTrashed(SelectQuery $query, array $options = []): SelectQuery
{
return $query->applyOptions([
'skipAddTrashCondition' => true,
]);
return $query->applyOptions(['skipAddTrashCondition' => true]);
}

/**
Expand All @@ -277,7 +272,7 @@ public function trashAll(mixed $conditions): int
*/
public function emptyTrash(): int
{
return $this->_table->deleteAll($this->_getUnaryExpression());
return $this->_table->deleteAll([$this->getTrashField(false) . ' IS NOT' => null]);
}

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

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

/**
Expand Down Expand Up @@ -346,21 +341,6 @@ public function cascadingRestoreTrash(
return $result;
}

/**
* Returns a unary expression for bulk record manipulation.
*
* @return \Closure
*/
protected function _getUnaryExpression(): Closure
{
return fn (QueryExpression $queryExpression): QueryExpression => $queryExpression
->add(new UnaryExpression(
'IS NOT NULL',
$this->getTrashField(false),
UnaryExpression::POSTFIX
));
}

/**
* Returns the table's field used to mark a `trashed` row.
*
Expand All @@ -371,7 +351,7 @@ public function getTrashField(bool $aliased = true): string
{
$field = $this->getConfig('field');

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

/** @psalm-suppress RedundantCondition */
if (empty($field)) {
$field = Configure::read('Muffin/Trash.field');
}
$field ??= Configure::read('Muffin/Trash.field');

if (empty($field)) {
if ($field === null) {
throw new CakeException('TrashBehavior: "field" config needs to be provided.');
}

Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Model/Behavior/TrashBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,7 @@ public function testGetTrashFieldUsesConfiguredValue()
*/
public function testGetTrashFieldFallbackToDefault()
{
$trash = new TrashBehavior($this->Articles, ['field' => '']);
$trash = new TrashBehavior($this->Articles);

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