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