10
10
use PHPStan \Type \Constant \ConstantStringType ;
11
11
use PHPStan \Type \Type ;
12
12
use PHPStan \Type \TypeCombinator ;
13
+ use function array_key_exists ;
13
14
use function strrpos ;
14
15
use function substr ;
15
16
@@ -71,23 +72,48 @@ public function resolve(Expr $expr): Type
71
72
72
73
public function getPatternModifiers (string $ pattern ): ?string
73
74
{
74
- $ delimiter = $ this ->getDelimiterFromString (new ConstantStringType ($ pattern ));
75
- if ($ delimiter === null ) {
75
+ $ endDelimiterPos = $ this ->getEndDelimiterPos ($ pattern );
76
+
77
+ if ($ endDelimiterPos === false ) {
76
78
return null ;
77
79
}
78
80
79
- if ( $ delimiter === ' { ' ) {
80
- $ endDelimiterPos = strrpos ( $ pattern , ' } ' );
81
- } else {
82
- // same start and end delimiter
83
- $ endDelimiterPos = strrpos ( $ pattern , $ delimiter );
84
- }
81
+ return substr ( $ pattern , $ endDelimiterPos + 1 );
82
+ }
83
+
84
+ public function removeDelimitersAndModifiers ( string $ pattern ): string
85
+ {
86
+ $ endDelimiterPos = $ this -> getEndDelimiterPos ( $ pattern );
85
87
86
88
if ($ endDelimiterPos === false ) {
87
- return null ;
89
+ return $ pattern ;
88
90
}
89
91
90
- return substr ($ pattern , $ endDelimiterPos + 1 );
92
+ return substr ($ pattern , 1 , $ endDelimiterPos - 1 );
93
+ }
94
+
95
+ private function getEndDelimiterPos (string $ pattern ): false |int
96
+ {
97
+ $ startDelimiter = $ this ->getPatternDelimiter ($ pattern );
98
+ if ($ startDelimiter === null ) {
99
+ return false ;
100
+ }
101
+
102
+ // delimiter variants, see https://www.php.net/manual/en/regexp.reference.delimiters.php
103
+ $ bracketStyleDelimiters = [
104
+ '{ ' => '} ' ,
105
+ '( ' => ') ' ,
106
+ '[ ' => '] ' ,
107
+ '< ' => '> ' ,
108
+ ];
109
+ if (array_key_exists ($ startDelimiter , $ bracketStyleDelimiters )) {
110
+ $ endDelimiterPos = strrpos ($ pattern , $ bracketStyleDelimiters [$ startDelimiter ]);
111
+ } else {
112
+ // same start and end delimiter
113
+ $ endDelimiterPos = strrpos ($ pattern , $ startDelimiter );
114
+ }
115
+
116
+ return $ endDelimiterPos ;
91
117
}
92
118
93
119
/**
@@ -105,7 +131,7 @@ public function getPatternDelimiters(Concat $concat, Scope $scope): array
105
131
106
132
$ delimiters = [];
107
133
foreach ($ left ->getConstantStrings () as $ leftString ) {
108
- $ delimiter = $ this ->getDelimiterFromString ($ leftString );
134
+ $ delimiter = $ this ->getPatternDelimiter ($ leftString-> getValue () );
109
135
if ($ delimiter === null ) {
110
136
continue ;
111
137
}
@@ -115,13 +141,13 @@ public function getPatternDelimiters(Concat $concat, Scope $scope): array
115
141
return $ delimiters ;
116
142
}
117
143
118
- private function getDelimiterFromString ( ConstantStringType $ string ): ?string
144
+ private function getPatternDelimiter ( string $ regex ): ?string
119
145
{
120
- if ($ string -> getValue () === '' ) {
146
+ if ($ regex === '' ) {
121
147
return null ;
122
148
}
123
149
124
- return substr ($ string -> getValue () , 0 , 1 );
150
+ return substr ($ regex , 0 , 1 );
125
151
}
126
152
127
153
}
0 commit comments