Skip to content
Draft
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
4 changes: 4 additions & 0 deletions extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,7 @@ services:
class: CakeDC\PHPStan\Type\TypeFactoryBuildDynamicReturnTypeExtension
tags:
- phpstan.broker.dynamicStaticMethodReturnTypeExtension
-
class: CakeDC\PHPStan\ThrowType\TableMethodThrowTypeExtension
tags:
- phpstan.dynamicMethodThrowTypeExtension
102 changes: 102 additions & 0 deletions src/ThrowType/TableMethodThrowTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php
declare(strict_types=1);

namespace CakeDC\PHPStan\ThrowType;

use Cake\ORM\Association;
use Cake\ORM\Table;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\MissingMethodFromReflectionException;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\DynamicMethodThrowTypeExtension;
use PHPStan\Type\Type;

class TableMethodThrowTypeExtension implements DynamicMethodThrowTypeExtension
{
/**
* @var \PHPStan\Reflection\ReflectionProvider
*/
protected ReflectionProvider $reflectionProvider;

/**
* @var array<int, string>
*/
protected array $methods = [
'get',
'deleteManyOrFail',
'findOrCreate',
'save',
'saveOrFail',
'saveMany',
'saveManyOrFail',
];

/**
* @param \PHPStan\Reflection\ReflectionProvider $reflectionProvider
*/
public function __construct(ReflectionProvider $reflectionProvider)
{
$this->reflectionProvider = $reflectionProvider;
}

/**
* @param \PHPStan\Reflection\MethodReflection $methodReflection
* @return bool
*/
public function isMethodSupported(MethodReflection $methodReflection): bool
{
if (!in_array($methodReflection->getName(), $this->methods, true)) {
return false;
}

return $methodReflection->getDeclaringClass()->is(Table::class);
}

/**
* @param \PHPStan\Reflection\MethodReflection $methodReflection
* @param \PhpParser\Node\Expr\MethodCall $methodCall
* @param \PHPStan\Analyser\Scope $scope
* @return \PHPStan\Type\Type|null
*/
public function getThrowTypeFromMethodCall(
MethodReflection $methodReflection,
MethodCall $methodCall,
Scope $scope,
): ?Type {
$methodName = $methodReflection->getName();
$type = $scope->getType($methodCall->var);
$classReflection = $type->getObjectClassReflections()[0];
if ($classReflection->is(Association::class)) {
return $this->getThrowType($methodName, $scope);
}

if (!$classReflection->is(Table::class)) {
return null;
}

$tag = $classReflection->getResolvedPhpDoc()?->getMethodTags()['get'] ?? null;
if ($tag === null) {
return null;
}

return $this->getThrowType($methodName, $scope);
}

/**
* @param string $methodName
* @param \PHPStan\Analyser\Scope $scope
* @return \PHPStan\Type\Type|null
*/
protected function getThrowType(string $methodName, Scope $scope): ?Type
{
$reflection = $this->reflectionProvider->getClass(Table::class);

try {
return $reflection->getMethod($methodName, $scope)->getThrowType();
} catch (MissingMethodFromReflectionException $e) {
return null;
}
}
}
70 changes: 70 additions & 0 deletions tests/test_app/Controller/NotesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
namespace App\Controller;

use Cake\Controller\Controller;
use Cake\Datasource\Exception\InvalidPrimaryKeyException;
use Cake\Datasource\Exception\RecordNotFoundException;
use Cake\Log\Log;
use Cake\ORM\Exception\PersistenceFailedException;
use Cake\ORM\Exception\RolledbackTransactionException;
use Cake\ORM\TableRegistry;

/**
Expand Down Expand Up @@ -183,4 +187,70 @@ public function listUsers()
//BelongsTo should match the correct Users table methods.
$this->Notes->Users->blockOld();
}

/**
* @return void
*/
public function viewWithTryCatch()
{
try {
$note = $this->Notes->get(1);
$note->note = 'This is a test';
} catch (RecordNotFoundException) {
}

$user = $this->Notes->NewMyUsers->get(1);
try {
$this->Notes->NewMyUsers->save($user);
} catch (RolledbackTransactionException) {
}
try {
$this->Notes->NewMyUsers->findOrCreate(['name' => 'This is a test']);
} catch (PersistenceFailedException) {
}
try {
$this->Notes->NewMyUsers->saveOrFail($user);
} catch (PersistenceFailedException) {
}

try {
$this->Notes->NewMyUsers->saveMany([$user]);
} catch (PersistenceFailedException) {
}

try {
$this->Notes->NewMyUsers->saveManyOrFail([$user]);
} catch (PersistenceFailedException) {
}

try {
$this->Notes->NewMyUsers->deleteManyOrFail([$user]);
} catch (PersistenceFailedException) {
}

try {
$user = $this->Notes->get(1);
$user->note = 'This is a test';
} catch (InvalidPrimaryKeyException) {
}

try {
$user = $this->Notes->Users->get(1);
$user->name = 'user1';
} catch (RecordNotFoundException) {
//TableMethodThrowTypeExtension avoids dead catch
}
try {
$user = $this->Notes->MyUsers->get(2);
$user->name = 'user2';
} catch (RecordNotFoundException) {
//TableMethodThrowTypeExtension avoids dead catch
}
try {
$user = $this->Notes->NewMyUsers->get(3);
$user->name = 'user3';
} catch (RecordNotFoundException) {
//TableMethodThrowTypeExtension avoids dead catch
}
}
}
7 changes: 7 additions & 0 deletions tests/test_app/Model/Table/MyUsersTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@

/**
* @method \App\Model\Entity\User get($primaryKey, $options = [])
* @method \App\Model\Entity\User findOrCreate($search, ?callable $callback = null, $options = [])
* @method \App\Model\Entity\User|false save(\Cake\Datasource\EntityInterface $entity, $options = [])
* @method \App\Model\Entity\User saveOrFail(\Cake\Datasource\EntityInterface $entity, $options = [])
* @method iterable<\App\Model\Entity\User>|false saveMany(iterable<\App\Model\Entity\User> $entities, $options = [])
* @method iterable<\App\Model\Entity\User> saveManyOrFail(iterable<\App\Model\Entity\User> $entities, $options = [])
* @method iterable<\App\Model\Entity\User>|false deleteMany(iterable<\App\Model\Entity\User> $entities, $options = [])
* @method iterable<\App\Model\Entity\User> deleteManyOrFail(iterable<\App\Model\Entity\User> $entities, $options = [])
*/
class MyUsersTable extends Table
{
Expand Down
1 change: 1 addition & 0 deletions tests/test_app/Model/Table/NotesTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* @property \App\Model\Table\VeryCustomize00009ArticlesTable&\Cake\ORM\Association\HasMany $VeryCustomize00009Articles
* @property \Cake\ORM\Association\BelongsTo<\App\Model\Table\UsersTable> $Users
* @property \Cake\ORM\Association\BelongsTo&\App\Model\Table\UsersTable $MyUsers//Don't use generic here, we need this way for testing
* @property \Cake\ORM\Association\BelongsTo<\App\Model\Table\MyUsersTable> $NewMyUsers
*/
class NotesTable extends Table
{
Expand Down