6
6
use PHPStan \Type \Constant \ConstantIntegerType ;
7
7
use function is_float ;
8
8
use function is_int ;
9
+ use function is_numeric ;
10
+ use function is_string ;
11
+ use function pow ;
9
12
10
13
final class ExponentiateHelper
11
14
{
@@ -83,10 +86,16 @@ private static function exponentiateConstantScalar(ConstantScalarType $base, Typ
83
86
$ min = null ;
84
87
$ max = null ;
85
88
if ($ exponent ->getMin () !== null ) {
86
- $ min = $ base ->getValue () ** $ exponent ->getMin ();
89
+ $ min = self ::pow ($ base ->getValue (), $ exponent ->getMin ());
90
+ if ($ min === null ) {
91
+ return new ErrorType ();
92
+ }
87
93
}
88
94
if ($ exponent ->getMax () !== null ) {
89
- $ max = $ base ->getValue () ** $ exponent ->getMax ();
95
+ $ max = self ::pow ($ base ->getValue (), $ exponent ->getMax ());
96
+ if ($ max === null ) {
97
+ return new ErrorType ();
98
+ }
90
99
}
91
100
92
101
if (!is_float ($ min ) && !is_float ($ max )) {
@@ -95,7 +104,11 @@ private static function exponentiateConstantScalar(ConstantScalarType $base, Typ
95
104
}
96
105
97
106
if ($ exponent instanceof ConstantScalarType) {
98
- $ result = $ base ->getValue () ** $ exponent ->getValue ();
107
+ $ result = self ::pow ($ base ->getValue (), $ exponent ->getValue ());
108
+ if ($ result === null ) {
109
+ return new ErrorType ();
110
+ }
111
+
99
112
if (is_int ($ result )) {
100
113
return new ConstantIntegerType ($ result );
101
114
}
@@ -105,4 +118,15 @@ private static function exponentiateConstantScalar(ConstantScalarType $base, Typ
105
118
return null ;
106
119
}
107
120
121
+ private static function pow (mixed $ base , mixed $ exp ): float |int |null
122
+ {
123
+ if (is_string ($ base ) && !is_numeric ($ base )) {
124
+ return null ;
125
+ }
126
+ if (is_string ($ exp ) && !is_numeric ($ exp )) {
127
+ return null ;
128
+ }
129
+ return pow ($ base , $ exp );
130
+ }
131
+
108
132
}
0 commit comments