1
- <?php declare (strict_types = 1 );
1
+ <?php declare (strict_types= 1 );
2
2
3
3
namespace Composer \Pcre \PHPStan ;
4
4
5
5
use Composer \Pcre \Preg ;
6
- use PhpParser \Node \Expr \FuncCall ;
7
- use PhpParser \Node \Expr \MethodCall ;
8
6
use PhpParser \Node \Expr \StaticCall ;
9
7
use PHPStan \Analyser \Scope ;
10
8
use PHPStan \Analyser \SpecifiedTypes ;
11
9
use PHPStan \Analyser \TypeSpecifier ;
12
10
use PHPStan \Analyser \TypeSpecifierAwareExtension ;
13
11
use PHPStan \Analyser \TypeSpecifierContext ;
14
- use PHPStan \Reflection \FunctionReflection ;
15
12
use PHPStan \Reflection \MethodReflection ;
16
13
use PHPStan \TrinaryLogic ;
17
- use PHPStan \Type \FunctionTypeSpecifyingExtension ;
18
- use PHPStan \Type \MethodTypeSpecifyingExtension ;
19
14
use PHPStan \Type \Php \RegexArrayShapeMatcher ;
20
15
use PHPStan \Type \StaticMethodTypeSpecifyingExtension ;
21
- use function in_array ;
22
- use function strtolower ;
23
16
24
17
final class PregMatchTypeSpecifyingExtension implements StaticMethodTypeSpecifyingExtension, TypeSpecifierAwareExtension
25
18
{
26
19
27
- private TypeSpecifier $ typeSpecifier ;
20
+ private TypeSpecifier $ typeSpecifier ;
28
21
29
22
private RegexArrayShapeMatcher $ regexShapeMatcher ;
30
23
@@ -36,58 +29,59 @@ public function __construct(
36
29
}
37
30
38
31
39
- public function setTypeSpecifier (TypeSpecifier $ typeSpecifier ): void
40
- {
41
- $ this ->typeSpecifier = $ typeSpecifier ;
42
- }
32
+ public function setTypeSpecifier (TypeSpecifier $ typeSpecifier ): void
33
+ {
34
+ $ this ->typeSpecifier = $ typeSpecifier ;
35
+ }
43
36
44
- public function getClass (): string {
37
+ public function getClass (): string
38
+ {
45
39
return Preg::class;
46
40
}
47
41
48
- public function isStaticMethodSupported (MethodReflection $ methodReflection , StaticCall $ node , TypeSpecifierContext $ context ) : bool
42
+ public function isStaticMethodSupported (MethodReflection $ methodReflection , StaticCall $ node , TypeSpecifierContext $ context ): bool
49
43
{
50
44
return $ methodReflection ->getName () === 'match ' && !$ context ->null ();
51
45
}
52
46
53
- public function specifyTypes (MethodReflection $ methodReflection , StaticCall $ node , Scope $ scope , TypeSpecifierContext $ context ) : SpecifiedTypes
47
+ public function specifyTypes (MethodReflection $ methodReflection , StaticCall $ node , Scope $ scope , TypeSpecifierContext $ context ): SpecifiedTypes
54
48
{
55
- $ args = $ node ->getArgs ();
56
- $ patternArg = $ args [0 ] ?? null ;
57
- $ matchesArg = $ args [2 ] ?? null ;
58
- $ flagsArg = $ args [3 ] ?? null ;
59
-
60
- if (
61
- $ patternArg === null || $ matchesArg === null
62
- ) {
63
- return new SpecifiedTypes ();
64
- }
65
-
66
- $ patternType = $ scope -> getType ($ patternArg -> value );
67
- $ flagsType = null ;
68
- if ( $ flagsArg !== null ) {
69
- $ flagsType = $ scope -> getType ( $ flagsArg -> value );
70
- }
71
-
72
- $ matchedType = $ this ->regexShapeMatcher ->matchType ($ patternType , $ flagsType , TrinaryLogic::createFromBoolean ($ context ->true ()));
73
- if ($ matchedType === null ) {
74
- return new SpecifiedTypes ();
75
- }
76
-
77
- $ overwrite = false ;
78
- if ($ context ->false ()) {
79
- $ overwrite = true ;
80
- $ context = $ context ->negate ();
81
- }
82
-
83
- return $ this ->typeSpecifier ->create (
84
- $ matchesArg ->value ,
85
- $ matchedType ,
86
- $ context ,
87
- $ overwrite ,
88
- $ scope ,
89
- $ node ,
90
- );
91
- }
49
+ $ args = $ node ->getArgs ();
50
+ $ patternArg = $ args [0 ] ?? null ;
51
+ $ matchesArg = $ args [2 ] ?? null ;
52
+ $ flagsArg = $ args [3 ] ?? null ;
53
+
54
+ if (
55
+ $ patternArg === null || $ matchesArg === null
56
+ ) {
57
+ return new SpecifiedTypes ();
58
+ }
59
+
60
+ $ flagsType = PregMatchFlags:: getType ($ flagsArg , $ scope );
61
+ if ( $ flagsType === null ) {
62
+ return new SpecifiedTypes ();
63
+ }
64
+ $ patternType = $ scope -> getType ( $ patternArg -> value );
65
+
66
+ $ matchedType = $ this ->regexShapeMatcher ->matchType ($ patternType , $ flagsType , TrinaryLogic::createFromBoolean ($ context ->true ()));
67
+ if ($ matchedType === null ) {
68
+ return new SpecifiedTypes ();
69
+ }
70
+
71
+ $ overwrite = false ;
72
+ if ($ context ->false ()) {
73
+ $ overwrite = true ;
74
+ $ context = $ context ->negate ();
75
+ }
76
+
77
+ return $ this ->typeSpecifier ->create (
78
+ $ matchesArg ->value ,
79
+ $ matchedType ,
80
+ $ context ,
81
+ $ overwrite ,
82
+ $ scope ,
83
+ $ node ,
84
+ );
85
+ }
92
86
93
87
}
0 commit comments