-
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 #38 from CakeDC/feature/new-rules-2410
Feature/new rules 2410
- Loading branch information
Showing
20 changed files
with
610 additions
and
45 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: 1 | ||
:patch: 2 | ||
:minor: 2 | ||
: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,71 @@ | ||
<?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\Controller; | ||
|
||
use Cake\Controller\ComponentRegistry; | ||
use CakeDC\PHPStan\Rule\LoadObjectExistsCakeClassRule; | ||
use CakeDC\PHPStan\Utility\CakeNameRegistry; | ||
|
||
class LoadComponentExistsClassRule extends LoadObjectExistsCakeClassRule | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
protected string $identifier = 'cake.loadComponent.existClass'; | ||
|
||
/** | ||
* @var array<string> | ||
*/ | ||
protected array $sourceMethods = [ | ||
'loadComponent', | ||
]; | ||
|
||
/** | ||
* @var array<string> | ||
*/ | ||
protected array $componentRegistryMethods = [ | ||
'load', | ||
]; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
protected function getTargetClassName(string $name): ?string | ||
{ | ||
return CakeNameRegistry::getComponentClassName($name); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
protected function getDetails(string $reference, array $args): ?array | ||
{ | ||
if (str_ends_with($reference, 'Controller')) { | ||
return [ | ||
'alias' => $args[0] ?? null, | ||
'options' => $args[1] ?? null, | ||
'sourceMethods' => $this->sourceMethods, | ||
]; | ||
} | ||
if ($reference === ComponentRegistry::class) { | ||
return [ | ||
'alias' => $args[0] ?? null, | ||
'options' => $args[1] ?? null, | ||
'sourceMethods' => $this->componentRegistryMethods, | ||
]; | ||
} | ||
|
||
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
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,84 @@ | ||
<?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\Mailer; | ||
|
||
use CakeDC\PHPStan\Utility\CakeNameRegistry; | ||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PhpParser\Node\Scalar\String_; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use PHPStan\Type\ThisType; | ||
|
||
class GetMailerExistsClassRule implements Rule | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
protected string $identifier = 'cake.getMailer.existClass'; | ||
|
||
/** | ||
* @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); | ||
if ( | ||
!$node->name instanceof Node\Identifier | ||
|| $node->name->name !== 'getMailer' | ||
) { | ||
return []; | ||
} | ||
|
||
$args = $node->getArgs(); | ||
if (!isset($args[0])) { | ||
return []; | ||
} | ||
$value = $args[0]->value; | ||
if (!$value instanceof String_) { | ||
return []; | ||
} | ||
$callerType = $scope->getType($node->var); | ||
if (!$callerType instanceof ThisType) { | ||
return []; | ||
} | ||
$reflection = $callerType->getClassReflection(); | ||
|
||
if (CakeNameRegistry::getMailerClassName($value->value)) { | ||
return []; | ||
} | ||
|
||
return [ | ||
RuleErrorBuilder::message(sprintf( | ||
'Call to %s::%s could not find the class for "%s"', | ||
$reflection->getName(), | ||
$node->name->name, | ||
$value->value, | ||
)) | ||
->identifier($this->identifier) | ||
->build(), | ||
]; | ||
} | ||
} |
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,48 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace CakeDC\PHPStan\Rule\Model; | ||
|
||
use Cake\Datasource\EntityInterface; | ||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\ArrayDimFetch; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use PHPStan\Type\ObjectType; | ||
|
||
class DisallowEntityArrayAccessRule implements Rule | ||
{ | ||
/** | ||
* @return string | ||
*/ | ||
public function getNodeType(): string | ||
{ | ||
return ArrayDimFetch::class; | ||
} | ||
|
||
/** | ||
* @param \PhpParser\Node $node | ||
* @param \PHPStan\Analyser\Scope $scope | ||
* @return array<\PHPStan\Rules\RuleError> | ||
* @throws \PHPStan\ShouldNotHappenException | ||
* @throws \PHPStan\Reflection\MissingMethodFromReflectionException | ||
*/ | ||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
assert($node instanceof ArrayDimFetch); | ||
$type = $scope->getType($node->var); | ||
if (!$type instanceof ObjectType || !is_a($type->getClassName(), EntityInterface::class, true)) { | ||
return []; | ||
} | ||
|
||
return [ | ||
RuleErrorBuilder::message(sprintf( | ||
'Array access to entity to %s is not allowed, access as object instead', | ||
$type->getClassName(), | ||
)) | ||
->identifier('cake.entity.arrayAccess') | ||
->build(), | ||
]; | ||
} | ||
} |
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,29 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace CakeDC\PHPStan\Traits; | ||
|
||
use PHPStan\Reflection\ClassReflection; | ||
|
||
trait IsFromTargetTrait | ||
{ | ||
/** | ||
* @param \PHPStan\Reflection\ClassReflection $reflection | ||
* @return bool | ||
*/ | ||
protected function isFromTargetTrait(ClassReflection $reflection, string $targetTrait): bool | ||
{ | ||
foreach ($reflection->getTraits() as $trait) { | ||
if ($trait->getName() === $targetTrait) { | ||
return true; | ||
} | ||
} | ||
foreach ($reflection->getParents() as $parent) { | ||
if ($this->isFromTargetTrait($parent, $targetTrait)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
Oops, something went wrong.