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 #300 from shochdoerfer/feature/rule_disallow_setTe…
…mplate Add rule "Disallow setTemplate() in Block classes"
- Loading branch information
Showing
7 changed files
with
221 additions
and
0 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
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
74 changes: 74 additions & 0 deletions
74
src/bitExpert/PHPStan/Magento/Rules/SetTemplateDisallowedForBlockRule.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,74 @@ | ||
<?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; | ||
|
||
/** | ||
* Do not use setTemplate in Block classes, see | ||
* https://github.com/extdn/extdn-phpcs/blob/master/Extdn/Sniffs/Blocks/SetTemplateInBlockSniff.md | ||
* | ||
* @implements Rule<MethodCall> | ||
*/ | ||
class SetTemplateDisallowedForBlockRule 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 ($node->name->name !== 'setTemplate') { | ||
return []; | ||
} | ||
|
||
$type = $scope->getType($node->var); | ||
$isAbstractModelType = (new ObjectType('Magento\Framework\View\Element\Template'))->isSuperTypeOf($type); | ||
if (!$isAbstractModelType->yes()) { | ||
return []; | ||
} | ||
|
||
return [ | ||
sprintf( | ||
'Setter methods like %s::%s() are deprecated in Block classes, use constructor arguments instead', | ||
$type->describe(VerbosityLevel::typeOnly()), | ||
$node->name->name | ||
) | ||
]; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
tests/bitExpert/PHPStan/Magento/Rules/Helper/SampleBlock.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,24 @@ | ||
<?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\Helper; | ||
|
||
use \Magento\Framework\View\Element\Template; | ||
|
||
class SampleBlock extends Template | ||
{ | ||
public function __construct() | ||
{ | ||
// We do not call the parent constructor here on purpose as we do not want do to deal with creating | ||
// not needed dependencies just for the testcase! | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
tests/bitExpert/PHPStan/Magento/Rules/Helper/block_settemplate.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 | ||
|
||
$block = new \bitExpert\PHPStan\Magento\Rules\Helper\SampleBlock(); | ||
$block->setTemplate('template.phtml'); |
99 changes: 99 additions & 0 deletions
99
tests/bitExpert/PHPStan/Magento/Rules/SetTemplateDisallowedForBlockRuleUnitTest.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,99 @@ | ||
<?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\SampleBlock; | ||
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<SetTemplateDisallowedForBlockRule> | ||
*/ | ||
class SetTemplateDisallowedForBlockRuleUnitTest extends RuleTestCase | ||
{ | ||
protected function getRule(): Rule | ||
{ | ||
return new SetTemplateDisallowedForBlockRule(); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function checkCaughtExceptions(): void | ||
{ | ||
$this->analyse([__DIR__ . '/Helper/block_settemplate.php'], [ | ||
[ | ||
'Setter methods like '.SampleBlock::class.'::setTemplate() are deprecated in Block classes, '. | ||
'use constructor arguments instead', | ||
4, | ||
], | ||
]); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function getNodeTypeMethodReturnsMethodCall(): void | ||
{ | ||
$rule = new SetTemplateDisallowedForBlockRule(); | ||
|
||
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 SetTemplateDisallowedForBlockRule(); | ||
$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 SetTemplateDisallowedForBlockRule(); | ||
$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 SetTemplateDisallowedForBlockRule(); | ||
$return = $rule->processNode($node, $scope); | ||
|
||
self::assertCount(0, $return); | ||
} | ||
} |