Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
190 additions
and
1 deletion.
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
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
73 changes: 73 additions & 0 deletions
73
src/bitExpert/PHPStan/Magento/Rules/ResourceModelsShouldBeUsedDirectlyRule.php
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,73 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the phpstan-magento package. | ||
* | ||
* (c) bitExpert AG | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace bitExpert\PHPStan\Magento\Rules; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\ShouldNotHappenException; | ||
use PHPStan\Type\ObjectType; | ||
use PHPStan\Type\VerbosityLevel; | ||
|
||
/** | ||
* Since 100.1.0 resource models should be used directly. | ||
* | ||
* @implements Rule<MethodCall> | ||
*/ | ||
class ResourceModelsShouldBeUsedDirectlyRule implements Rule | ||
{ | ||
/** | ||
* @phpstan-return class-string<MethodCall> | ||
* @return string | ||
*/ | ||
public function getNodeType(): string | ||
{ | ||
return MethodCall::class; | ||
} | ||
|
||
/** | ||
* @param Node $node | ||
* @param Scope $scope | ||
* @return (string|\PHPStan\Rules\RuleError)[] errors | ||
* @throws ShouldNotHappenException | ||
*/ | ||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if (!$node instanceof MethodCall) { | ||
throw new ShouldNotHappenException(); | ||
} | ||
|
||
if (!$node->name instanceof Node\Identifier) { | ||
return []; | ||
} | ||
|
||
if (!in_array($node->name->name, ['getResource', '_getResource'], true)) { | ||
return []; | ||
} | ||
|
||
$type = $scope->getType($node->var); | ||
$isAbstractModelType = (new ObjectType('Magento\Framework\Model\AbstractModel'))->isSuperTypeOf($type); | ||
if (!$isAbstractModelType->yes()) { | ||
return []; | ||
} | ||
|
||
return [ | ||
sprintf( | ||
'%s::%s() is deprecated. Use Resource Models directly', | ||
$type->describe(VerbosityLevel::typeOnly()), | ||
$node->name->name | ||
) | ||
]; | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
tests/bitExpert/PHPStan/Magento/Rules/Helper/resource_model.php
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,4 @@ | ||
<?php | ||
|
||
$model = new \bitExpert\PHPStan\Magento\Rules\Helper\SampleModel(); | ||
$model->getResource(); |
97 changes: 97 additions & 0 deletions
97
tests/bitExpert/PHPStan/Magento/Rules/ResourceModelsShouldBeUsedDirectlyRuleUnitTest.php
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,97 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the phpstan-magento package. | ||
* | ||
* (c) bitExpert AG | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace bitExpert\PHPStan\Magento\Rules; | ||
|
||
use bitExpert\PHPStan\Magento\Rules\Helper\SampleModel; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PhpParser\Node\Expr\Variable; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\ShouldNotHappenException; | ||
use PHPStan\Testing\RuleTestCase; | ||
|
||
/** | ||
* @extends \PHPStan\Testing\RuleTestCase<ResourceModelsShouldBeUsedDirectlyRule> | ||
*/ | ||
class ResourceModelsShouldBeUsedDirectlyRuleUnitTest extends RuleTestCase | ||
{ | ||
protected function getRule(): Rule | ||
{ | ||
return new ResourceModelsShouldBeUsedDirectlyRule(); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function checkCaughtExceptions(): void | ||
{ | ||
$this->analyse([__DIR__ . '/Helper/resource_model.php'], [ | ||
[ | ||
SampleModel::class . '::getResource() is deprecated. Use Resource Models directly', | ||
4, | ||
], | ||
]); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function getNodeTypeMethodReturnsMethodCall(): void | ||
{ | ||
$rule = new ResourceModelsShouldBeUsedDirectlyRule(); | ||
|
||
self::assertSame(MethodCall::class, $rule->getNodeType()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function processNodeThrowsExceptionForNonMethodCallNodes(): void | ||
{ | ||
$this->expectException(ShouldNotHappenException::class); | ||
|
||
$node = new Variable('var'); | ||
$scope = $this->createMock(Scope::class); | ||
|
||
$rule = new ResourceModelsShouldBeUsedDirectlyRule(); | ||
$rule->processNode($node, $scope); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function processNodeReturnsEarlyWhenNodeNameIsWrongType(): void | ||
{ | ||
$node = new MethodCall(new Variable('var'), new Variable('wrong_node')); | ||
$scope = $this->createMock(Scope::class); | ||
|
||
$rule = new ResourceModelsShouldBeUsedDirectlyRule(); | ||
$return = $rule->processNode($node, $scope); | ||
|
||
self::assertCount(0, $return); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function processNodeReturnsEarlyWhenNodeNameIsNotSaveOrLoadOrDelete(): void | ||
{ | ||
$node = new MethodCall(new Variable('var'), 'wrong_node_name'); | ||
$scope = $this->createMock(Scope::class); | ||
|
||
$rule = new ResourceModelsShouldBeUsedDirectlyRule(); | ||
$return = $rule->processNode($node, $scope); | ||
|
||
self::assertCount(0, $return); | ||
} | ||
} |