Skip to content
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
14 changes: 8 additions & 6 deletions src/Model/Behavior/TrashBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
/**
* Trash Behavior.
*/
class TrashBehavior extends Behavior

Check failure on line 24 in src/Model/Behavior/TrashBehavior.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

ClassMustBeFinal

src/Model/Behavior/TrashBehavior.php:24:7: ClassMustBeFinal: Class Muffin\Trash\Model\Behavior\TrashBehavior is never extended and is not part of the public API, and thus must be made final. (see https://psalm.dev/361)
{
public const AFTER_DELETE_EVENT_OPTION = 'trash';

Expand Down Expand Up @@ -50,7 +50,7 @@
* @param array $config The config for this behavior.
* @return void
*/
public function initialize(array $config): void

Check failure on line 53 in src/Model/Behavior/TrashBehavior.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

MissingOverrideAttribute

src/Model/Behavior/TrashBehavior.php:53:5: MissingOverrideAttribute: Method Muffin\Trash\Model\Behavior\TrashBehavior::initialize should have the "Override" attribute (see https://psalm.dev/358)
{
if (isset($config['events']) && $config['events'] !== []) {
$this->setConfig('events', $config['events'], false);
Expand All @@ -63,7 +63,7 @@
* @return array<string, mixed>
* @throws \InvalidArgumentException When events are configured in an invalid format.
*/
public function implementedEvents(): array

Check failure on line 66 in src/Model/Behavior/TrashBehavior.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

MissingOverrideAttribute

src/Model/Behavior/TrashBehavior.php:66:5: MissingOverrideAttribute: Method Muffin\Trash\Model\Behavior\TrashBehavior::implementedevents should have the "Override" attribute (see https://psalm.dev/358)
{
$events = [];
$config = $this->getConfig('events');
Expand Down Expand Up @@ -160,7 +160,7 @@
}
}

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

return (bool)$this->_table->save($entity, $options);
}
Expand Down Expand Up @@ -287,7 +287,7 @@
if ($entity->isDirty()) {
throw new CakeException('Can not restore from a dirty entity.');
}
$entity->set($data, ['guard' => false]);
$entity->patch($data, ['guard' => false]);

return $this->_table->save($entity, $options);
}
Expand All @@ -313,7 +313,9 @@
if ($this->_isRecursable($association, $this->_table)) {
if ($entity === null) {
if ($result > 1) {
$result += $association->getTarget()->cascadingRestoreTrash(null, $options);
/** @var \Muffin\Trash\Model\Behavior\TrashBehavior $behavior */
$behavior = $association->getTarget()->getBehavior('Trash');
$result += $behavior->cascadingRestoreTrash(null, $options);

Check failure on line 318 in src/Model/Behavior/TrashBehavior.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

PossiblyInvalidOperand

src/Model/Behavior/TrashBehavior.php:318:36: PossiblyInvalidOperand: Cannot perform a numeric operation with a non-numeric type bool (see https://psalm.dev/163)

Check failure on line 318 in src/Model/Behavior/TrashBehavior.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

PossiblyInvalidOperand

src/Model/Behavior/TrashBehavior.php:318:25: PossiblyInvalidOperand: Cannot perform a numeric operation with a non-numeric type Cake\Datasource\EntityInterface (see https://psalm.dev/163)
}
} else {
/** @var list<string> $foreignKey */
Expand All @@ -323,10 +325,10 @@
$conditions = array_combine($foreignKey, $entity->extract($bindingKey));

foreach ($association->find('withTrashed')->where($conditions) as $related) {
/** @var \Muffin\Trash\Model\Behavior\TrashBehavior $behavior */
$behavior = $association->getTarget()->getBehavior('Trash');
if (
!$association
->getTarget()
->cascadingRestoreTrash($related, ['_primary' => false] + $options)
!$behavior->cascadingRestoreTrash($related, ['_primary' => false] + $options)

Check failure on line 331 in src/Model/Behavior/TrashBehavior.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

PossiblyInvalidArgument

src/Model/Behavior/TrashBehavior.php:331:63: PossiblyInvalidArgument: Argument 1 of Muffin\Trash\Model\Behavior\TrashBehavior::cascadingRestoreTrash expects Cake\Datasource\EntityInterface|null, but possibly different type Cake\Datasource\EntityInterface|array<array-key, mixed> provided (see https://psalm.dev/092)

Check failure on line 331 in src/Model/Behavior/TrashBehavior.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

RiskyTruthyFalsyComparison

src/Model/Behavior/TrashBehavior.php:331:29: RiskyTruthyFalsyComparison: Operand of type Cake\Datasource\EntityInterface|bool|int contains type int, which can be falsy and truthy. This can cause possibly unexpected behavior. Use strict comparison instead. (see https://psalm.dev/356)

Check failure on line 331 in src/Model/Behavior/TrashBehavior.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

Parameter #1 $entity of method Muffin\Trash\Model\Behavior\TrashBehavior::cascadingRestoreTrash() expects Cake\Datasource\EntityInterface|null, array|Cake\Datasource\EntityInterface given.
) {
$result = false;
}
Expand Down
57 changes: 29 additions & 28 deletions tests/TestCase/Model/Behavior/TrashBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ function (
public function testTrashComposite()
{
$item = $this->CompositeArticlesUsers->get([3, 1]);
$result = $this->CompositeArticlesUsers->trash($item);
$result = $this->CompositeArticlesUsers->getBehavior('Trash')->trash($item);

$this->assertTrue($result);
$this->assertCount(1, $this->CompositeArticlesUsers->find('onlyTrashed'));
Expand All @@ -318,7 +318,7 @@ public function testTrashComposite()
public function testTrash()
{
$article = $this->Articles->get(1);
$result = $this->Articles->trash($article);
$result = $this->Articles->getBehavior('Trash')->trash($article);

$this->assertTrue($result);
$this->assertCount(3, $this->Articles->find('withTrashed'));
Expand All @@ -343,7 +343,7 @@ public function testTrashNoPrimaryKey()
$article = $this->Articles->get(1);
$article->unset('id');
$this->expectException(CakeException::class);
$this->Articles->trash($article);
$this->Articles->getBehavior('Trash')->trash($article);
}

/**
Expand All @@ -355,7 +355,7 @@ public function testTrashNonAccessibleProperty()
{
$article = $this->Articles->get(1);
$article->setAccess('trashed', false);
$result = $this->Articles->trash($article);
$result = $this->Articles->getBehavior('Trash')->trash($article);

$this->assertTrue($result);
$this->assertCount(3, $this->Articles->find('withTrashed'));
Expand Down Expand Up @@ -399,7 +399,7 @@ public function testFindWithTrashed()
*/
public function testEmptyTrash()
{
$this->Articles->emptyTrash();
$this->Articles->getBehavior('Trash')->emptyTrash();

$this->assertCount(1, $this->Articles->find());
}
Expand All @@ -411,7 +411,7 @@ public function testEmptyTrash()
*/
public function testRestoreTrash()
{
$this->Articles->restoreTrash();
$this->Articles->getBehavior('Trash')->restoreTrash();

$this->assertCount(3, $this->Articles->find());
}
Expand All @@ -427,7 +427,7 @@ public function testRestoreDirtyEntity()
$entity->setDirty('title');

$this->expectException(CakeException::class);
$this->Articles->restoreTrash($entity);
$this->Articles->getBehavior('Trash')->restoreTrash($entity);
}

/**
Expand All @@ -439,7 +439,7 @@ public function testTrashAll()
{
$this->assertCount(1, $this->Articles->find());

$this->Articles->trashAll('1 = 1');
$this->Articles->getBehavior('Trash')->trashAll('1 = 1');
$this->assertCount(0, $this->Articles->find());
}

Expand All @@ -450,7 +450,7 @@ public function testTrashAll()
*/
public function testRestoreTrashEntity()
{
$this->Articles->restoreTrash(new Entity([
$this->Articles->getBehavior('Trash')->restoreTrash(new Entity([
'id' => 2,
], ['markNew' => false, 'markClean' => true]));

Expand Down Expand Up @@ -502,7 +502,7 @@ public function testInteroperabilityWithCounterCache()
public function testInteroperabilityWithCounterCacheAndTrashMethod()
{
$comment = $this->Comments->get(1);
$this->Comments->trash($comment);
$this->Comments->getBehavior('Trash')->trash($comment);
$result = $this->Articles->get(1);

$this->assertEquals(0, $result->comment_count);
Expand All @@ -521,7 +521,7 @@ public function testCascadingTrash()
$association->setCascadeCallbacks(true);

$article = $this->Articles->get(1);
$this->Articles->trash($article);
$this->Articles->getBehavior('Trash')->trash($article);

$article = $this->Articles->find('withTrashed')
->where(['Articles.id' => 1])
Expand Down Expand Up @@ -553,7 +553,7 @@ public function testDisabledCascadingForTrash()
$this->Articles->behaviors()->get('Trash')->setConfig('cascadeOnTrash', false);

$article = $this->Articles->get(1);
$this->Articles->trash($article);
$this->Articles->getBehavior('Trash')->trash($article);

$article = $this->Articles->find('withTrashed')
->where(['Articles.id' => 1])
Expand All @@ -575,10 +575,10 @@ public function testCascadingUntrashOptionsArePassedToSave()
$association->setDependent(true);
$association->setCascadeCallbacks(true);

$this->Articles->Comments->getTarget()->trashAll([]);
$this->Articles->Comments->getTarget()->getBehavior('Trash')->trashAll([]);
$this->assertEquals(0, $this->Articles->Comments->getTarget()->find()->count());

$this->Articles->trashAll([]);
$this->Articles->getBehavior('Trash')->trashAll([]);
$this->assertEquals(0, $this->Articles->find()->count());

$article = $this->Articles
Expand Down Expand Up @@ -615,7 +615,7 @@ function (
}
);

$result = $this->Articles->cascadingRestoreTrash($article, [
$result = $this->Articles->getBehavior('Trash')->cascadingRestoreTrash($article, [
'restoreOptions' => true,
]);

Expand All @@ -641,13 +641,13 @@ public function testCascadingUntrashEntity()
$association->setDependent(true);
$association->setCascadeCallbacks(true);

$this->Articles->Comments->getTarget()->trashAll([]);
$this->Articles->Comments->getTarget()->getBehavior('Trash')->trashAll([]);
$this->assertEquals(0, $this->Articles->Comments->getTarget()->find()->count());

$this->Articles->CompositeArticlesUsers->getTarget()->trashAll([]);
$this->Articles->CompositeArticlesUsers->getTarget()->getBehavior('Trash')->trashAll([]);
$this->assertEquals(0, $this->Articles->CompositeArticlesUsers->getTarget()->find()->count());

$this->Articles->trashAll([]);
$this->Articles->getBehavior('Trash')->trashAll([]);
$this->assertEquals(0, $this->Articles->find()->count());

$article = $this->Articles
Expand Down Expand Up @@ -690,7 +690,7 @@ public function testCascadingUntrashEntity()

$this->assertInstanceOf(
EntityInterface::class,
$this->Articles->cascadingRestoreTrash($article)
$this->Articles->getBehavior('Trash')->cascadingRestoreTrash($article)
);

$article = $this->Articles
Expand Down Expand Up @@ -735,13 +735,13 @@ public function testCascadingUntrashAll()
$association->setDependent(true);
$association->setCascadeCallbacks(true);

$this->Articles->Comments->getTarget()->trashAll([]);
$this->Articles->Comments->getTarget()->getBehavior('Trash')->trashAll([]);
$this->assertEquals(0, $this->Articles->Comments->getTarget()->find()->count());

$this->Articles->CompositeArticlesUsers->getTarget()->trashAll([]);
$this->Articles->CompositeArticlesUsers->getTarget()->getBehavior('Trash')->trashAll([]);
$this->assertEquals(0, $this->Articles->CompositeArticlesUsers->getTarget()->find()->count());

$this->Articles->trashAll([]);
$this->Articles->getBehavior('Trash')->trashAll([]);
$this->assertEquals(0, $this->Articles->find()->count());

$article = $this->Articles
Expand All @@ -766,7 +766,7 @@ public function testCascadingUntrashAll()
$this->assertNotEmpty($article->composite_articles_users[0]->trashed);
$this->assertInstanceOf(DateTime::class, $article->composite_articles_users[0]->trashed);

$this->assertEquals(8, $this->Articles->cascadingRestoreTrash());
$this->assertEquals(8, $this->Articles->getBehavior('Trash')->cascadingRestoreTrash());

$article = $this->Articles
->find()
Expand All @@ -793,22 +793,23 @@ public function testCascadingUntrashFailure()
$association = $this->Articles->Comments;
$association->setDependent(true);
$association->setCascadeCallbacks(true);
$association->getEventManager()->on('Model.beforeSave', function () {
return false;
$association->getEventManager()->on('Model.beforeSave', function ($event) {
$event->setResult(false);
$event->stopPropagation();
});

$association->getTarget()->trashAll([]);
$association->getTarget()->getBehavior('Trash')->trashAll([]);
$this->assertEquals(0, $association->getTarget()->find()->count());

$this->Articles->trashAll([]);
$this->Articles->getBehavior('Trash')->trashAll([]);
$this->assertEquals(0, $this->Articles->find()->count());

$article = $this->Articles
->find('withTrashed')
->where(['Articles.id' => 1])
->first();

$this->assertFalse($this->Articles->cascadingRestoreTrash($article));
$this->assertFalse($this->Articles->getBehavior('Trash')->cascadingRestoreTrash($article));
}

/**
Expand Down
Loading