• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

bitExpert / phpstan-magento / 4638260582

pending completion
4638260582

push

github

GitHub
Merge pull request #300 from shochdoerfer/feature/rule_disallow_setTemplate

18 of 18 new or added lines in 1 file covered. (100.0%)

617 of 699 relevant lines covered (88.27%)

1.82 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.44
/src/bitExpert/PHPStan/Magento/Rules/SetTemplateDisallowedForBlockRule.php
1
<?php
2

3
/*
4
 * This file is part of the phpstan-magento package.
5
 *
6
 * (c) bitExpert AG
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
declare(strict_types=1);
12

13
namespace bitExpert\PHPStan\Magento\Rules;
14

15
use PhpParser\Node;
16
use PhpParser\Node\Expr\MethodCall;
17
use PHPStan\Analyser\Scope;
18
use PHPStan\Rules\Rule;
19
use PHPStan\ShouldNotHappenException;
20
use PHPStan\Type\ObjectType;
21
use PHPStan\Type\VerbosityLevel;
22

23
/**
24
 * Do not use setTemplate in Block classes, see
25
 * https://github.com/extdn/extdn-phpcs/blob/master/Extdn/Sniffs/Blocks/SetTemplateInBlockSniff.md
26
 *
27
 * @implements Rule<MethodCall>
28
 */
29
class SetTemplateDisallowedForBlockRule implements Rule
30
{
31
    /**
32
     * @phpstan-return class-string<MethodCall>
33
     * @return string
34
     */
35
    public function getNodeType(): string
36
    {
37
        return MethodCall::class;
2✔
38
    }
39

40
    /**
41
     * @param Node $node
42
     * @param Scope $scope
43
     * @return (string|\PHPStan\Rules\RuleError)[] errors
44
     * @throws ShouldNotHappenException
45
     */
46
    public function processNode(Node $node, Scope $scope): array
47
    {
48
        if (!$node instanceof MethodCall) {
4✔
49
            throw new ShouldNotHappenException();
1✔
50
        }
51

52
        if (!$node->name instanceof Node\Identifier) {
3✔
53
            return [];
1✔
54
        }
55

56
        if ($node->name->name !== 'setTemplate') {
2✔
57
            return [];
1✔
58
        }
59

60
        $type = $scope->getType($node->var);
1✔
61
        $isAbstractModelType = (new ObjectType('Magento\Framework\View\Element\Template'))->isSuperTypeOf($type);
1✔
62
        if (!$isAbstractModelType->yes()) {
1✔
63
            return [];
×
64
        }
65

66
        return [
1✔
67
            sprintf(
1✔
68
                'Setter methods like %s::%s() are deprecated in Block classes, use constructor arguments instead',
1✔
69
                $type->describe(VerbosityLevel::typeOnly()),
1✔
70
                $node->name->name
1✔
71
            )
1✔
72
        ];
1✔
73
    }
74
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc