Skip to content

Commit 168125e

Browse files
Add dynamic return typing to Mage_Core_Model_Layout class helpers
1 parent 8997d93 commit 168125e

File tree

2 files changed

+64
-4
lines changed

2 files changed

+64
-4
lines changed

extension.neon

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@ parameters:
88
- %currentWorkingDirectory%/public/app/code/local/*/*/sql/*
99

1010
services:
11-
-
12-
class: PHPStanMagento1\Type\Mage\HelperMethodsReturnTypeExtension
13-
tags:
14-
- phpstan.broker.dynamicStaticMethodReturnTypeExtension
1511
-
1612
class: PHPStanMagento1\Reflection\Varien\Object\MagicMethodsReflectionExtension
1713
tags:
1814
- phpstan.broker.methodsClassReflectionExtension
15+
-
16+
class: PHPStanMagento1\Type\Mage\Core\Model\Layout\HelperMethodsReturnTypeExtension
17+
tags:
18+
- phpstan.broker.dynamicMethodReturnTypeExtension
19+
-
20+
class: PHPStanMagento1\Type\Mage\HelperMethodsReturnTypeExtension
21+
tags:
22+
- phpstan.broker.dynamicStaticMethodReturnTypeExtension
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace PHPStanMagento1\Type\Mage;
4+
5+
use PhpParser\Node\Expr\StaticCall;
6+
use PhpParser\Node\Scalar\String_;
7+
use PHPStan\Analyser\Scope;
8+
use PHPStan\Reflection\MethodReflection;
9+
use PHPStan\ShouldNotHappenException;
10+
use PHPStan\Type\DynamicMethodReturnTypeExtension;
11+
use PHPStan\Type\ObjectType;
12+
use PHPStan\Type\Type;
13+
14+
use Mage_Core_Model_Layout;
15+
16+
class HelperMethodsReturnTypeExtension implements DynamicMethodReturnTypeExtension
17+
{
18+
public function getClass(): string
19+
{
20+
return Mage_Core_Model_Layout;
21+
}
22+
23+
public function isStaticMethodSupported(MethodReflection $methodReflection): bool
24+
{
25+
return in_array(
26+
$methodReflection->getName(),
27+
[
28+
'getBlockSingleton',
29+
'helper',
30+
]
31+
);
32+
}
33+
34+
public function getTypeFromStaticMethodCall(MethodReflection $methodReflection, StaticCall $methodCall, Scope $scope): Type
35+
{
36+
if (!isset($methodCall->args[0]) || !$methodCall->args[0]->value instanceof String_) {
37+
throw new ShouldNotHappenException();
38+
}
39+
40+
$name = $methodCall->args[0]->value->value;
41+
$class = $this->getClassFromHelperMethod($methodReflection->getName(), $name);
42+
return new ObjectType($class);
43+
}
44+
45+
private function getClassFromHelperMethod($method, $name)
46+
{
47+
$config = Mage::getConfig();
48+
switch ($method) {
49+
case 'getBlockSingleton':
50+
return $config->getBlockClassName($name);
51+
case 'helper':
52+
return $config->getHelperClassName($name);
53+
}
54+
throw new ShouldNotHappenException();
55+
}
56+
}

0 commit comments

Comments
 (0)