Skip to content

Commit 9ef3df0

Browse files
authored
Merge pull request #78 from UseMuffin/cleanup
Cleanup
2 parents 988e403 + 208d092 commit 9ef3df0

7 files changed

Lines changed: 55 additions & 36 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
},
3737
"require-dev": {
3838
"cakephp/cakephp": "^5.0.0",
39-
"phpunit/phpunit": "^10.1.0",
39+
"phpunit/phpunit": "^10.5.58 || ^11.5.3 || ^12.4",
4040
"cakephp/cakephp-codesniffer": "^5.0"
4141
},
4242
"scripts": {

phpcs.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?xml version="1.0"?>
22
<ruleset name="Trash">
3-
<config name="installed_paths" value="../../cakephp/cakephp-codesniffer" />
3+
<file>src/</file>
4+
<file>tests/</file>
45

56
<rule ref="CakePHP" />
67
</ruleset>

phpstan.neon

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,5 @@ parameters:
33
paths:
44
- src/
55
ignoreErrors:
6-
- '#Call to an undefined method Cake\\ORM\\Table::cascadingRestoreTrash\(\)#'
76
- identifier: missingType.iterableValue
87
- identifier: missingType.generics

phpunit.xml.dist

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
<coverage/>
44
<php>
55
<ini name="memory_limit" value="-1"/>
6-
<ini name="apc.enable_cli" value="1"/>
76
<env name="FIXTURE_SCHEMA_METADATA" value="./tests/schema.php"/>
87
</php>
98
<!-- Add any additional test suites you want to run here -->

psalm.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,8 @@
2323

2424
<DocblockTypeContradiction errorLevel="info" />
2525
<RedundantConditionGivenDocblockType errorLevel="info" />
26+
27+
<ClassMustBeFinal errorLevel="info" />
28+
<MissingOverrideAttribute errorLevel="info" />
2629
</issueHandlers>
2730
</psalm>

src/Model/Behavior/TrashBehavior.php

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,12 @@ public function trash(EntityInterface $entity, array $options = []): bool
160160
}
161161
}
162162

163-
$entity->patch([$this->getTrashField(false) => new DateTime()]);
163+
/** @phpstan-ignore function.alreadyNarrowedType */
164+
if (method_exists($entity, 'patch')) {
165+
$entity->patch([$this->getTrashField(false) => new DateTime()]);
166+
} else {
167+
$entity->set($this->getTrashField(false), new DateTime());
168+
}
164169

165170
return (bool)$this->_table->save($entity, $options);
166171
}
@@ -258,7 +263,7 @@ public function trashAll(mixed $conditions): int
258263
{
259264
return $this->_table->updateAll(
260265
[$this->getTrashField(false) => new DateTime()],
261-
$conditions
266+
$conditions,
262267
);
263268
}
264269

@@ -287,7 +292,13 @@ public function restoreTrash(?EntityInterface $entity = null, array $options = [
287292
if ($entity->isDirty()) {
288293
throw new CakeException('Can not restore from a dirty entity.');
289294
}
290-
$entity->patch($data, ['guard' => false]);
295+
296+
/** @phpstan-ignore function.alreadyNarrowedType */
297+
if (method_exists($entity, 'patch')) {
298+
$entity->patch($data, ['guard' => false]);
299+
} else {
300+
$entity->set($data, ['guard' => false]);
301+
}
291302

292303
return $this->_table->save($entity, $options);
293304
}
@@ -304,18 +315,21 @@ public function restoreTrash(?EntityInterface $entity = null, array $options = [
304315
*/
305316
public function cascadingRestoreTrash(
306317
?EntityInterface $entity = null,
307-
array $options = []
318+
array $options = [],
308319
): bool|int|EntityInterface {
309320
$result = $this->restoreTrash($entity, $options);
321+
$return = $result;
310322

311323
$associations = $this->_table->associations()->getByType(['HasOne', 'HasMany']);
312324
foreach ($associations as $association) {
313325
if ($this->_isRecursable($association, $this->_table)) {
314326
if ($entity === null) {
315-
if ($result > 1) {
327+
if ($result > 0) {
316328
/** @var \Muffin\Trash\Model\Behavior\TrashBehavior $behavior */
317329
$behavior = $association->getTarget()->getBehavior('Trash');
318-
$result += $behavior->cascadingRestoreTrash(null, $options);
330+
if ($behavior->cascadingRestoreTrash(null, $options) === false) {
331+
$return = false;
332+
}
319333
}
320334
} else {
321335
/** @var list<string> $foreignKey */
@@ -324,20 +338,21 @@ public function cascadingRestoreTrash(
324338
$bindingKey = (array)$association->getBindingKey();
325339
$conditions = array_combine($foreignKey, $entity->extract($bindingKey));
326340

327-
foreach ($association->find('withTrashed')->where($conditions) as $related) {
341+
/** @var \Cake\Datasource\EntityInterface $related */
342+
foreach ($association->find('withTrashed')->where($conditions)->all() as $related) {
328343
/** @var \Muffin\Trash\Model\Behavior\TrashBehavior $behavior */
329344
$behavior = $association->getTarget()->getBehavior('Trash');
330345
if (
331-
!$behavior->cascadingRestoreTrash($related, ['_primary' => false] + $options)
346+
$behavior->cascadingRestoreTrash($related, ['_primary' => false] + $options) === false
332347
) {
333-
$result = false;
348+
$return = false;
334349
}
335350
}
336351
}
337352
}
338353
}
339354

340-
return $result;
355+
return $return;
341356
}
342357

343358
/**

tests/TestCase/Model/Behavior/TrashBehaviorTest.php

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Cake\TestSuite\TestCase;
1717
use InvalidArgumentException;
1818
use Muffin\Trash\Model\Behavior\TrashBehavior;
19+
use PHPUnit\Framework\Attributes\DataProvider;
1920

2021
class TrashBehaviorTest extends TestCase
2122
{
@@ -58,7 +59,7 @@ public function setUp(): void
5859

5960
$this->CompositeArticlesUsers = $this->getTableLocator()->get(
6061
'Muffin/Trash.CompositeArticlesUsers',
61-
['table' => 'trash_composite_articles_users']
62+
['table' => 'trash_composite_articles_users'],
6263
);
6364
$this->CompositeArticlesUsers->addBehavior('Muffin/Trash.Trash');
6465

@@ -129,7 +130,7 @@ public function testBeforeFindWithTrashFieldInComparison()
129130
$query = $this->Articles->find('all');
130131

131132
$result = $query->where(
132-
[$this->Articles->aliasField('trashed') . ' >= ' => new DateTime('-1 day')]
133+
[$this->Articles->aliasField('trashed') . ' >= ' => new DateTime('-1 day')],
133134
)->toArray();
134135
$this->assertCount(2, $result);
135136
}
@@ -179,7 +180,7 @@ function (Event $event, EntityInterface $entity, ArrayObject $options) {
179180
$entity->setError('id', 'Save aborted');
180181
$event->setResult(false);
181182
$event->stopPropagation();
182-
}
183+
},
183184
);
184185

185186
$result = $this->Articles->delete($article);
@@ -223,15 +224,15 @@ function (Event $event, EntityInterface $entity, ArrayObject $options) use (&$ha
223224
if (isset($options['deleteOptions'])) {
224225
$hasDeleteOptionsBefore = true;
225226
}
226-
}
227+
},
227228
);
228229
$this->Comments->getEventManager()->on(
229230
'Model.afterDelete',
230231
function (Event $event, EntityInterface $entity, ArrayObject $options) use (&$hasDeleteOptionsAfter) {
231232
if (isset($options['deleteOptions'])) {
232233
$hasDeleteOptionsAfter = true;
233234
}
234-
}
235+
},
235236
);
236237

237238
$article = $this->Articles->get(1);
@@ -265,24 +266,24 @@ function (Event $event, EntityInterface $entity, ArrayObject $options) use (&$ma
265266
if (isset($options['deleteOptions'])) {
266267
$mainHasDeleteOptions = true;
267268
}
268-
}
269+
},
269270
);
270271
$this->Comments->getEventManager()->on(
271272
'Model.beforeSave',
272273
function (
273274
Event $event,
274275
EntityInterface $entity,
275-
ArrayObject $options
276+
ArrayObject $options,
276277
) use (
277278
&$dependentHasDeleteOptions,
278-
&$dependentIsNotPrimary
279+
&$dependentIsNotPrimary,
279280
) {
280281
if (isset($options['deleteOptions'])) {
281282
$dependentHasDeleteOptions = true;
282283
}
283284

284285
$dependentIsNotPrimary = $options['_primary'] === false;
285-
}
286+
},
286287
);
287288

288289
$article = $this->Articles->get(1);
@@ -329,7 +330,7 @@ public function testTrash()
329330
$this->getTableLocator()
330331
->get('ArticlesUsers', ['table' => 'trash_articles_users'])
331332
->find()
332-
->count()
333+
->count(),
333334
);
334335
}
335336

@@ -595,24 +596,24 @@ function (Event $event, EntityInterface $entity, ArrayObject $options) use (&$ma
595596
if (isset($options['restoreOptions'])) {
596597
$mainHasRestoreOptions = true;
597598
}
598-
}
599+
},
599600
);
600601
$this->Comments->getEventManager()->on(
601602
'Model.beforeSave',
602603
function (
603604
Event $event,
604605
EntityInterface $entity,
605-
ArrayObject $options
606+
ArrayObject $options,
606607
) use (
607608
&$dependentHasRestoreOptions,
608-
&$dependentIsNotPrimary
609+
&$dependentIsNotPrimary,
609610
) {
610611
if (isset($options['restoreOptions'])) {
611612
$dependentHasRestoreOptions = true;
612613
}
613614

614615
$dependentIsNotPrimary = $options['_primary'] === false;
615-
}
616+
},
616617
);
617618

618619
$result = $this->Articles->getBehavior('Trash')->cascadingRestoreTrash($article, [
@@ -690,7 +691,7 @@ public function testCascadingUntrashEntity()
690691

691692
$this->assertInstanceOf(
692693
EntityInterface::class,
693-
$this->Articles->getBehavior('Trash')->cascadingRestoreTrash($article)
694+
$this->Articles->getBehavior('Trash')->cascadingRestoreTrash($article),
694695
);
695696

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

769-
$this->assertEquals(8, $this->Articles->getBehavior('Trash')->cascadingRestoreTrash());
770+
$this->assertEquals(3, $this->Articles->getBehavior('Trash')->cascadingRestoreTrash());
770771

771772
$article = $this->Articles
772773
->find()
@@ -903,7 +904,7 @@ public function testGetTrashFieldSchemaIntrospection()
903904
{
904905
$this->assertEquals(
905906
'Articles.trashed',
906-
$this->Articles->behaviors()->get('Trash')->getTrashField()
907+
$this->Articles->behaviors()->get('Trash')->getTrashField(),
907908
);
908909
}
909910

@@ -923,11 +924,11 @@ public function testImpEInvalidArgumentException()
923924
/**
924925
* Test the implementedEvents method.
925926
*
926-
* @dataProvider provideConfigsForImplementedEventsTest
927927
* @param array $config Initial behavior config.
928928
* @param array $implementedEvents Expected implementedEvents.
929929
* @return void
930930
*/
931+
#[DataProvider('provideConfigsForImplementedEventsTest')]
931932
public function testImplementedEvents(array $config, array $implementedEvents)
932933
{
933934
$trash = new TrashBehavior($this->Users, $config);
@@ -942,6 +943,9 @@ public function testImplementedEvents(array $config, array $implementedEvents)
942943
*/
943944
public static function provideConfigsForImplementedEventsTest()
944945
{
946+
$callable = function () {
947+
};
948+
945949
return [
946950
'No event config inherits default events' => [
947951
[],
@@ -1001,8 +1005,7 @@ public static function provideConfigsForImplementedEventsTest()
10011005
[
10021006
'events' => [
10031007
'Model.beforeDelete' => [
1004-
'callable' => function () {
1005-
},
1008+
'callable' => $callable,
10061009
],
10071010
'Model.beforeFind' => [
10081011
'callable' => ['', 'beforeDelete'],
@@ -1012,8 +1015,7 @@ public static function provideConfigsForImplementedEventsTest()
10121015
],
10131016
[
10141017
'Model.beforeDelete' => [
1015-
'callable' => function () {
1016-
},
1018+
'callable' => $callable,
10171019
],
10181020
'Model.beforeFind' => [
10191021
'callable' => ['', 'beforeDelete'],

0 commit comments

Comments
 (0)