Skip to content

Commit 506e545

Browse files
committed
New extension TableMethodThrowTypeExtension
1 parent 120b5ba commit 506e545

3 files changed

Lines changed: 59 additions & 5 deletions

File tree

src/ThrowType/TableMethodThrowTypeExtension.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,19 @@ class TableMethodThrowTypeExtension implements DynamicMethodThrowTypeExtension
2020
*/
2121
protected ReflectionProvider $reflectionProvider;
2222

23+
/**
24+
* @var array<int, string>
25+
*/
26+
protected array $methods = [
27+
'get',
28+
'deleteManyOrFail',
29+
'findOrCreate',
30+
'save',
31+
'saveOrFail',
32+
'saveMany',
33+
'saveManyOrFail',
34+
];
35+
2336
/**
2437
* @param \PHPStan\Reflection\ReflectionProvider $reflectionProvider
2538
*/
@@ -34,7 +47,11 @@ public function __construct(ReflectionProvider $reflectionProvider)
3447
*/
3548
public function isMethodSupported(MethodReflection $methodReflection): bool
3649
{
37-
return $methodReflection->getName() === 'get';
50+
if (!in_array($methodReflection->getName(), $this->methods, true)) {
51+
return false;
52+
}
53+
54+
return $methodReflection->getDeclaringClass()->is(Table::class);
3855
}
3956

4057
/**
@@ -51,8 +68,7 @@ public function getThrowTypeFromMethodCall(
5168
$methodName = $methodReflection->getName();
5269
$type = $scope->getType($methodCall->var);
5370
$classReflection = $type->getObjectClassReflections()[0];
54-
$isAssociation = $classReflection->is(Association::class);
55-
if ($isAssociation) {
71+
if ($classReflection->is(Association::class)) {
5672
return $this->getThrowType($methodName, $scope);
5773
}
5874

tests/test_app/Controller/NotesController.php

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
use Cake\Datasource\Exception\InvalidPrimaryKeyException;
1818
use Cake\Datasource\Exception\RecordNotFoundException;
1919
use Cake\Log\Log;
20+
use Cake\ORM\Exception\PersistenceFailedException;
21+
use Cake\ORM\Exception\RolledbackTransactionException;
2022
use Cake\ORM\TableRegistry;
2123

2224
/**
@@ -197,9 +199,38 @@ public function viewWithTryCatch()
197199
} catch (RecordNotFoundException) {
198200
}
199201

202+
$user = $this->Notes->NewMyUsers->get(1);
200203
try {
201-
$note = $this->Notes->get(1);
202-
$note->note = 'This is a test';
204+
$this->Notes->NewMyUsers->save($user);
205+
} catch (RolledbackTransactionException) {
206+
}
207+
try {
208+
$this->Notes->NewMyUsers->findOrCreate(['name' => 'This is a test']);
209+
} catch (PersistenceFailedException) {
210+
}
211+
try {
212+
$this->Notes->NewMyUsers->saveOrFail($user);
213+
} catch (PersistenceFailedException) {
214+
}
215+
216+
try {
217+
$this->Notes->NewMyUsers->saveMany([$user]);
218+
} catch (PersistenceFailedException) {
219+
}
220+
221+
try {
222+
$this->Notes->NewMyUsers->saveManyOrFail([$user]);
223+
} catch (PersistenceFailedException) {
224+
}
225+
226+
try {
227+
$this->Notes->NewMyUsers->deleteManyOrFail([$user]);
228+
} catch (PersistenceFailedException) {
229+
}
230+
231+
try {
232+
$user = $this->Notes->get(1);
233+
$user->note = 'This is a test';
203234
} catch (InvalidPrimaryKeyException) {
204235
}
205236

tests/test_app/Model/Table/MyUsersTable.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@
77

88
/**
99
* @method \App\Model\Entity\User get($primaryKey, $options = [])
10+
* @method \App\Model\Entity\User findOrCreate($search, ?callable $callback = null, $options = [])
11+
* @method \App\Model\Entity\User|false save(\Cake\Datasource\EntityInterface $entity, $options = [])
12+
* @method \App\Model\Entity\User saveOrFail(\Cake\Datasource\EntityInterface $entity, $options = [])
13+
* @method iterable<\App\Model\Entity\User>|false saveMany(iterable<\App\Model\Entity\User> $entities, $options = [])
14+
* @method iterable<\App\Model\Entity\User> saveManyOrFail(iterable<\App\Model\Entity\User> $entities, $options = [])
15+
* @method iterable<\App\Model\Entity\User>|false deleteMany(iterable<\App\Model\Entity\User> $entities, $options = [])
16+
* @method iterable<\App\Model\Entity\User> deleteManyOrFail(iterable<\App\Model\Entity\User> $entities, $options = [])
1017
*/
1118
class MyUsersTable extends Table
1219
{

0 commit comments

Comments
 (0)