Skip to content

Fix for CakePHP 5.2 #76

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
.idea
/.phpunit.result.cache
/tools
/nbproject/*
17 changes: 6 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
# Trash

[![Build Status](https://img.shields.io/github/actions/workflow/status/UseMuffin/Trash/ci.yml?style=flat-square
)](https://github.com/UseMuffin/Trash/actions?query=workflow%3ACI+branch%3Amaster)
[![Coverage](https://img.shields.io/codecov/c/github/UseMuffin/Trash/master.svg?style=flat-square)](https://codecov.io/github/UseMuffin/Trash)
[![Total Downloads](https://img.shields.io/packagist/dt/muffin/trash.svg?style=flat-square)](https://packagist.org/packages/muffin/trash)
[![License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](LICENSE)

Adds "soft"-delete support to CakePHP tables.
Expand All @@ -13,7 +9,7 @@ Adds "soft"-delete support to CakePHP tables.
Using [Composer][composer]:

```
composer require muffin/trash
composer require vandalorumrex/trash
```

You then need to load the plugin. You can use the shell command:
Expand Down Expand Up @@ -115,13 +111,12 @@ To ensure your PRs are considered for upstream, you MUST follow the CakePHP codi

## Bugs & Feedback

http://github.com/usemuffin/trash/issues
http://github.com/vandalorumrex/trash/issues

## License

Copyright (c) 2015-present, [Use Muffin][muffin] and licensed under [The MIT License][mit].
This library is license under the MIT License (MIT). Please see License File for more information.

[cakephp]:http://cakephp.org
[composer]:http://getcomposer.org
[mit]:http://www.opensource.org/licenses/mit-license.php
[muffin]:http://usemuffin.com
## Credits

This library was forked from [UseMuffin Trash's](https://github.com/UseMuffin) on Github https://github.com/UseMuffin/Trash.
15 changes: 10 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "muffin/trash",
"name": "vandalorumrex/trash",
"description": "Adds soft delete support to CakePHP ORM tables.",
"type": "cakephp-plugin",
"keywords": [
"cakephp",
"muffin",
"vandalorumrex",
"trash",
"orm"
],
"homepage": "https://github.com/usemuffin/trash",
"homepage": "https://github.com/vandalorumrex/trash",
"license": "MIT",
"authors": [
{
Expand All @@ -21,14 +21,19 @@
"homepage": "https://github.com/ADmad",
"role": "Author"
},
{
"name": "VandalorumRex",
"homepage": "https://github.com/VandalorumRex",
"role": "Fixer"
},
{
"name": "Others",
"homepage": "https://github.com/usemuffin/trash/graphs/contributors"
}
],
"support": {
"issues": "https://github.com/usemuffin/trash/issues",
"source": "https://github.com/usemuffin/trash"
"issues": "https://github.com/vandalorumrex/trash/issues",
"source": "https://github.com/vandalorumrex/trash"
},
"require": {
"php": ">=8.1",
Expand Down
42 changes: 23 additions & 19 deletions src/Model/Behavior/TrashBehavior.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

declare(strict_types=1);

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

View workflow job for this annotation

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

Expected 0 lines before declare statement, found 1.

namespace Muffin\Trash\Model\Behavior;

Expand All @@ -16,12 +17,13 @@
use Cake\ORM\Behavior;
use Cake\ORM\Table;
use InvalidArgumentException;

use function Cake\Core\pluginSplit;

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

View workflow job for this annotation

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

Expected 0 lines between different types of use statement, found 1.

/**
* Trash Behavior.
*/
class TrashBehavior extends Behavior

Check failure on line 26 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:26: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 +52,7 @@
* @param array $config The config for this behavior.
* @return void
*/
public function initialize(array $config): void

Check failure on line 55 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:55: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 +65,7 @@
* @return array<string, mixed>
* @throws \InvalidArgumentException When events are configured in an invalid format.
*/
public function implementedEvents(): array

Check failure on line 68 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:68: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 All @@ -71,7 +73,7 @@
return $events;
}

foreach ((array)$config as $eventKey => $event) {
foreach ((array) $config as $eventKey => $event) {

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

View workflow job for this annotation

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

A cast statement must not be followed by a space
if (is_numeric($eventKey)) {
$eventKey = $event;
$event = null;
Expand Down Expand Up @@ -143,7 +145,7 @@
*/
public function trash(EntityInterface $entity, array $options = []): bool
{
$primaryKey = (array)$this->_table->getPrimaryKey();
$primaryKey = (array) $this->_table->getPrimaryKey();

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

View workflow job for this annotation

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

A cast statement must not be followed by a space

foreach ($primaryKey as $field) {
if (!$entity->has($field)) {
Expand All @@ -160,9 +162,13 @@
}
}

$entity->set($this->getTrashField(false), new DateTime());
if (method_exists($entity, 'patch')) {

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

View workflow job for this annotation

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

Call to function method_exists() with Cake\Datasource\EntityInterface and 'patch' will always evaluate to true.
$entity = $entity->patch([$this->getTrashField(false) => new DateTime()]);
} else {
$entity->set($this->getTrashField(false), new DateTime());
}

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

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

View workflow job for this annotation

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

A cast statement must not be followed by a space
}

/**
Expand Down Expand Up @@ -202,17 +208,15 @@
}

if (
$expression instanceof IdentifierExpression
&& in_array($expression->getIdentifier(), $fieldIdentifiers, true)
$expression instanceof IdentifierExpression && in_array($expression->getIdentifier(), $fieldIdentifiers, true)

Check warning on line 211 in src/Model/Behavior/TrashBehavior.php

View workflow job for this annotation

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

Line exceeds 120 characters; contains 126 characters
) {
$addCondition = false;

return;
}

if (
$expression instanceof FieldInterface
&& in_array($expression->getField(), $fieldIdentifiers, true)
$expression instanceof FieldInterface && in_array($expression->getField(), $fieldIdentifiers, true)
) {
$addCondition = false;
}
Expand All @@ -231,8 +235,8 @@
public function findOnlyTrashed(SelectQuery $query, array $options): SelectQuery
{
return $query
->applyOptions(['skipAddTrashCondition' => true])
->andWhere([$this->getTrashField() . ' IS NOT' => null]);
->applyOptions(['skipAddTrashCondition' => true])
->andWhere([$this->getTrashField() . ' IS NOT' => null]);
}

/**
Expand All @@ -258,7 +262,7 @@
{
return $this->_table->updateAll(
[$this->getTrashField(false) => new DateTime()],
$conditions

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

View workflow job for this annotation

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

Multi-line function calls must have a trailing comma after the last parameter.
);
}

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

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

View workflow job for this annotation

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

Call to function method_exists() with Cake\Datasource\EntityInterface and 'patch' will always evaluate to true.
$entity = $entity->patch($data, ['guard' => false]);
} else {
$entity->set($data, ['guard' => false]);
}

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

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

View workflow job for this annotation

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

Multi-line function declaration must have a trailing comma after the last parameter.
): bool|int|EntityInterface {
$result = $this->restoreTrash($entity, $options);

Expand All @@ -313,20 +321,20 @@
if ($this->_isRecursable($association, $this->_table)) {
if ($entity === null) {
if ($result > 1) {
$result += $association->getTarget()->cascadingRestoreTrash(null, $options);

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

View workflow job for this annotation

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

UndefinedMagicMethod

src/Model/Behavior/TrashBehavior.php:324:63: UndefinedMagicMethod: Magic method Cake\ORM\Table::cascadingrestoretrash does not exist (see https://psalm.dev/219)
}
} else {
/** @var list<string> $foreignKey */
$foreignKey = (array)$association->getForeignKey();
$foreignKey = (array) $association->getForeignKey();

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

View workflow job for this annotation

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

A cast statement must not be followed by a space
/** @var list<string> $bindingKey */
$bindingKey = (array)$association->getBindingKey();
$bindingKey = (array) $association->getBindingKey();

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

View workflow job for this annotation

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

A cast statement must not be followed by a space
$conditions = array_combine($foreignKey, $entity->extract($bindingKey));

foreach ($association->find('withTrashed')->where($conditions) as $related) {
if (
!$association
->getTarget()
->cascadingRestoreTrash($related, ['_primary' => false] + $options)

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

View workflow job for this annotation

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

UndefinedMagicMethod

src/Model/Behavior/TrashBehavior.php:337:35: UndefinedMagicMethod: Magic method Cake\ORM\Table::cascadingrestoretrash does not exist (see https://psalm.dev/219)
) {
$result = false;
}
Expand Down Expand Up @@ -383,11 +391,7 @@
protected function _isRecursable(Association $association, Table $table): bool
{
return (
$association->getTarget()->hasBehavior('Trash')
|| $association->getTarget()->hasBehavior(static::class)
)
&& $association->isOwningSide($table)
&& $association->getDependent()
&& $association->getCascadeCallbacks();
$association->getTarget()->hasBehavior('Trash') || $association->getTarget()->hasBehavior(static::class)
) && $association->isOwningSide($table) && $association->getDependent() && $association->getCascadeCallbacks();

Check warning on line 395 in src/Model/Behavior/TrashBehavior.php

View workflow job for this annotation

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

Line exceeds 120 characters; contains 123 characters
}
}
Loading