-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from CakeDC/feature/cakephp-rules-001
Feature/cakephp rules 001
- Loading branch information
Showing
28 changed files
with
2,316 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
:major: 3 | ||
:minor: 0 | ||
:minor: 1 | ||
:patch: 0 | ||
:special: '' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
parameters: | ||
cakeDC: | ||
addAssociationExistsTableClassRule: true | ||
addAssociationMatchOptionsTypesRule: true | ||
addBehaviorExistsClassRule: true | ||
tableGetMatchOptionsTypesRule: true | ||
ormSelectQueryFindMatchOptionsTypesRule: true | ||
|
||
parametersSchema: | ||
cakeDC: structure([ | ||
addAssociationExistsTableClassRule: anyOf(bool(), arrayOf(bool())) | ||
addAssociationMatchOptionsTypesRule: anyOf(bool(), arrayOf(bool())) | ||
addBehaviorExistsClassRule: anyOf(bool(), arrayOf(bool())) | ||
tableGetMatchOptionsTypesRule: anyOf(bool(), arrayOf(bool())) | ||
ormSelectQueryFindMatchOptionsTypesRule: anyOf(bool(), arrayOf(bool())) | ||
]) | ||
|
||
conditionalTags: | ||
CakeDC\PHPStan\Rule\Model\AddAssociationExistsTableClassRule: | ||
phpstan.rules.rule: %cakeDC.addAssociationExistsTableClassRule% | ||
CakeDC\PHPStan\Rule\Model\AddAssociationMatchOptionsTypesRule: | ||
phpstan.rules.rule: %cakeDC.addAssociationMatchOptionsTypesRule% | ||
CakeDC\PHPStan\Rule\Model\AddBehaviorExistsClassRule: | ||
phpstan.rules.rule: %cakeDC.addBehaviorExistsClassRule% | ||
CakeDC\PHPStan\Rule\Model\TableGetMatchOptionsTypesRule: | ||
phpstan.rules.rule: %cakeDC.tableGetMatchOptionsTypesRule% | ||
CakeDC\PHPStan\Rule\Model\OrmSelectQueryFindMatchOptionsTypesRule: | ||
phpstan.rules.rule: %cakeDC.ormSelectQueryFindMatchOptionsTypesRule% | ||
|
||
services: | ||
- | ||
class: CakeDC\PHPStan\Rule\Model\AddAssociationExistsTableClassRule | ||
- | ||
class: CakeDC\PHPStan\Rule\Model\AddAssociationMatchOptionsTypesRule | ||
- | ||
class: CakeDC\PHPStan\Rule\Model\AddBehaviorExistsClassRule | ||
- | ||
class: CakeDC\PHPStan\Rule\Model\TableGetMatchOptionsTypesRule | ||
- | ||
class: CakeDC\PHPStan\Rule\Model\OrmSelectQueryFindMatchOptionsTypesRule |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace CakeDC\PHPStan\PhpDoc; | ||
|
||
use Cake\ORM\Association; | ||
use Cake\ORM\Association\BelongsTo; | ||
use Cake\ORM\Association\BelongsToMany; | ||
use Cake\ORM\Association\HasMany; | ||
use Cake\ORM\Association\HasOne; | ||
use PHPStan\Analyser\NameScope; | ||
use PHPStan\PhpDoc\TypeNodeResolver; | ||
use PHPStan\PhpDoc\TypeNodeResolverAwareExtension; | ||
use PHPStan\PhpDoc\TypeNodeResolverExtension; | ||
use PHPStan\PhpDocParser\Ast\Type\IntersectionTypeNode; | ||
use PHPStan\PhpDocParser\Ast\Type\TypeNode; | ||
use PHPStan\Type\Generic\GenericObjectType; | ||
use PHPStan\Type\ObjectType; | ||
use PHPStan\Type\Type; | ||
|
||
/** | ||
* Fix intersection association phpDoc to correct generic object type, ex: | ||
* | ||
* Change `\Cake\ORM\Association\BelongsTo&\App\Model\Table\UsersTable` to `\Cake\ORM\Association\BelongsTo<\App\Model\Table\UsersTable>` | ||
* | ||
* The type `\Cake\ORM\Association\BelongsTo&\App\Model\Table\UsersTable` is considered invalid (NeverType) by PHPStan | ||
*/ | ||
class TableAssociationTypeNodeResolverExtension implements TypeNodeResolverExtension, TypeNodeResolverAwareExtension | ||
{ | ||
private TypeNodeResolver $typeNodeResolver; | ||
|
||
/** | ||
* @var array<string> | ||
*/ | ||
protected array $associationTypes = [ | ||
BelongsTo::class, | ||
BelongsToMany::class, | ||
HasMany::class, | ||
HasOne::class, | ||
Association::class, | ||
]; | ||
|
||
/** | ||
* @param \PHPStan\PhpDoc\TypeNodeResolver $typeNodeResolver | ||
* @return void | ||
*/ | ||
public function setTypeNodeResolver(TypeNodeResolver $typeNodeResolver): void | ||
{ | ||
$this->typeNodeResolver = $typeNodeResolver; | ||
} | ||
|
||
/** | ||
* @param \PHPStan\PhpDocParser\Ast\Type\TypeNode $typeNode | ||
* @param \PHPStan\Analyser\NameScope $nameScope | ||
* @return \PHPStan\Type\Type|null | ||
*/ | ||
public function resolve(TypeNode $typeNode, NameScope $nameScope): ?Type | ||
{ | ||
if (!$typeNode instanceof IntersectionTypeNode) { | ||
return null; | ||
} | ||
$types = $this->typeNodeResolver->resolveMultiple($typeNode->types, $nameScope); | ||
$config = [ | ||
'association' => null, | ||
'table' => null, | ||
]; | ||
foreach ($types as $type) { | ||
if (!$type instanceof ObjectType) { | ||
continue; | ||
} | ||
$className = $type->getClassName(); | ||
if ($config['association'] === null && in_array($className, $this->associationTypes)) { | ||
$config['association'] = $type; | ||
} elseif ($config['table'] === null && str_ends_with($className, 'Table')) { | ||
$config['table'] = $type; | ||
} | ||
} | ||
if ($config['table'] && $config['association']) { | ||
return new GenericObjectType( | ||
$config['association']->getClassName(), | ||
[$config['table']] | ||
); | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/** | ||
* Copyright 2024, Cake Development Corporation (https://www.cakedc.com) | ||
* | ||
* Licensed under The MIT License | ||
* Redistributions of files must retain the above copyright notice. | ||
* | ||
* @copyright Copyright 2024, Cake Development Corporation (https://www.cakedc.com) | ||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php) | ||
*/ | ||
|
||
namespace CakeDC\PHPStan\Rule; | ||
|
||
use CakeDC\PHPStan\Rule\Traits\ParseClassNameFromArgTrait; | ||
use PhpParser\Node; | ||
use PhpParser\Node\Arg; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PhpParser\Node\Scalar\String_; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
|
||
abstract class LoadObjectExistsCakeClassRule implements Rule | ||
{ | ||
use ParseClassNameFromArgTrait; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected string $identifier; | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getNodeType(): string | ||
{ | ||
return MethodCall::class; | ||
} | ||
|
||
/** | ||
* @param \PhpParser\Node $node | ||
* @param \PHPStan\Analyser\Scope $scope | ||
* @return array<\PHPStan\Rules\RuleError> | ||
*/ | ||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
assert($node instanceof MethodCall); | ||
$args = $node->getArgs(); | ||
if (!$node->name instanceof Node\Identifier) { | ||
return []; | ||
} | ||
$reference = $scope->getType($node->var)->getReferencedClasses()[0] ?? null; | ||
if ($reference === null) { | ||
return []; | ||
} | ||
$details = $this->getDetails($reference, $args); | ||
|
||
if ( | ||
$details === null | ||
|| !in_array($node->name->name, $details['sourceMethods']) | ||
|| !$details['alias'] instanceof Arg | ||
|| !$details['alias']->value instanceof String_ | ||
) { | ||
return []; | ||
} | ||
|
||
$inputClassName = $this->getInputClassName( | ||
$details['alias']->value, | ||
$details['options'] | ||
); | ||
if ($this->getTargetClassName($inputClassName)) { | ||
return []; | ||
} | ||
|
||
return [ | ||
RuleErrorBuilder::message(sprintf( | ||
'Call to %s::%s could not find the class for "%s"', | ||
$reference, | ||
$node->name->name, | ||
$inputClassName, | ||
)) | ||
->identifier($this->identifier) | ||
->build(), | ||
]; | ||
} | ||
|
||
/** | ||
* @param \PhpParser\Node\Scalar\String_ $nameArg | ||
* @param \PhpParser\Node\Arg|null $options | ||
* @return string | ||
*/ | ||
protected function getInputClassName(String_ $nameArg, ?Arg $options): string | ||
{ | ||
$className = $nameArg->value; | ||
|
||
if ( | ||
$options === null | ||
|| !$options->value instanceof Node\Expr\Array_ | ||
) { | ||
return $className; | ||
} | ||
foreach ($options->value->items as $item) { | ||
if ( | ||
!$item instanceof Node\Expr\ArrayItem | ||
|| !$item->key instanceof String_ | ||
|| $item->key->value !== 'className' | ||
) { | ||
continue; | ||
} | ||
$name = $this->parseClassNameFromExprTrait($item->value); | ||
if ($name !== null) { | ||
return $name; | ||
} | ||
} | ||
|
||
return $className; | ||
} | ||
|
||
/** | ||
* @param string $name | ||
* @return string|null | ||
*/ | ||
abstract protected function getTargetClassName(string $name): ?string; | ||
|
||
/** | ||
* @param string $reference | ||
* @param array<\PhpParser\Node\Arg> $args | ||
* @return array{'alias': ?\PhpParser\Node\Arg, 'options': ?\PhpParser\Node\Arg, 'sourceMethods':array<string>}|null | ||
*/ | ||
abstract protected function getDetails(string $reference, array $args): ?array; | ||
} |
Oops, something went wrong.