Skip to content

Commit a861488

Browse files
authored
Cache ObjectType instead of re-creating it over and over (#6006)
1 parent 22c9256 commit a861488

3 files changed

Lines changed: 29 additions & 19 deletions

File tree

‎src/Type/Php/BcMathNumberOperatorTypeSpecifyingExtension.php‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@
1717
final class BcMathNumberOperatorTypeSpecifyingExtension implements OperatorTypeSpecifyingExtension
1818
{
1919

20+
private ObjectType $mathNumberType;
21+
2022
public function __construct(private PhpVersion $phpVersion)
2123
{
24+
$this->mathNumberType = new ObjectType('BcMath\Number');
2225
}
2326

2427
public function isOperatorSupported(string $operatorSigil, Type $leftSide, Type $rightSide): bool
@@ -27,19 +30,16 @@ public function isOperatorSupported(string $operatorSigil, Type $leftSide, Type
2730
return false;
2831
}
2932

30-
$bcMathNumberType = new ObjectType('BcMath\Number');
31-
3233
return in_array($operatorSigil, ['-', '+', '*', '/', '**', '%', '<', '<=', '>', '>=', '==', '!=', '<=>'], true)
3334
&& (
34-
$bcMathNumberType->isSuperTypeOf($leftSide)->yes()
35-
|| $bcMathNumberType->isSuperTypeOf($rightSide)->yes()
35+
$this->mathNumberType->isSuperTypeOf($leftSide)->yes()
36+
|| $this->mathNumberType->isSuperTypeOf($rightSide)->yes()
3637
);
3738
}
3839

3940
public function specifyType(string $operatorSigil, Type $leftSide, Type $rightSide): Type
4041
{
41-
$bcMathNumberType = new ObjectType('BcMath\Number');
42-
$otherSide = $bcMathNumberType->isSuperTypeOf($leftSide)->yes()
42+
$otherSide = $this->mathNumberType->isSuperTypeOf($leftSide)->yes()
4343
? $rightSide
4444
: $leftSide;
4545

@@ -58,9 +58,9 @@ public function specifyType(string $operatorSigil, Type $leftSide, Type $rightSi
5858
if (
5959
$otherSide->isInteger()->yes()
6060
|| $otherSide->isNumericString()->yes()
61-
|| $bcMathNumberType->isSuperTypeOf($otherSide)->yes()
61+
|| $this->mathNumberType->isSuperTypeOf($otherSide)->yes()
6262
) {
63-
return $bcMathNumberType;
63+
return $this->mathNumberType;
6464
}
6565

6666
return new ErrorType();

‎src/Type/Php/GmpOperatorTypeSpecifyingExtension.php‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,29 @@
1616
final class GmpOperatorTypeSpecifyingExtension implements OperatorTypeSpecifyingExtension
1717
{
1818

19+
private ObjectType $gmpType;
20+
21+
public function __construct()
22+
{
23+
$this->gmpType = new ObjectType('GMP');
24+
}
25+
1926
public function isOperatorSupported(string $operatorSigil, Type $leftSide, Type $rightSide): bool
2027
{
2128
if ($leftSide instanceof NeverType || $rightSide instanceof NeverType) {
2229
return false;
2330
}
2431

25-
$gmpType = new ObjectType('GMP');
26-
2732
return in_array($operatorSigil, ['+', '-', '*', '/', '**', '%', '&', '|', '^', '<<', '>>', '<', '<=', '>', '>=', '==', '!=', '<=>'], true)
2833
&& (
29-
$gmpType->isSuperTypeOf($leftSide)->yes()
30-
|| $gmpType->isSuperTypeOf($rightSide)->yes()
34+
$this->gmpType->isSuperTypeOf($leftSide)->yes()
35+
|| $this->gmpType->isSuperTypeOf($rightSide)->yes()
3136
);
3237
}
3338

3439
public function specifyType(string $operatorSigil, Type $leftSide, Type $rightSide): Type
3540
{
36-
$gmpType = new ObjectType('GMP');
37-
$otherSide = $gmpType->isSuperTypeOf($leftSide)->yes()
41+
$otherSide = $this->gmpType->isSuperTypeOf($leftSide)->yes()
3842
? $rightSide
3943
: $leftSide;
4044

@@ -51,9 +55,9 @@ public function specifyType(string $operatorSigil, Type $leftSide, Type $rightSi
5155
if (
5256
$otherSide->isInteger()->yes()
5357
|| $otherSide->isNumericString()->yes()
54-
|| $gmpType->isSuperTypeOf($otherSide)->yes()
58+
|| $this->gmpType->isSuperTypeOf($otherSide)->yes()
5559
) {
56-
return $gmpType;
60+
return $this->gmpType;
5761
}
5862

5963
return new ErrorType();

‎src/Type/Php/GmpUnaryOperatorTypeSpecifyingExtension.php‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@
1313
final class GmpUnaryOperatorTypeSpecifyingExtension implements UnaryOperatorTypeSpecifyingExtension
1414
{
1515

16+
private ObjectType $gmpType;
17+
18+
public function __construct()
19+
{
20+
$this->gmpType = new ObjectType('GMP');
21+
}
22+
1623
public function isOperatorSupported(string $operatorSigil, Type $operand): bool
1724
{
1825
if ($operand instanceof NeverType) {
@@ -23,13 +30,12 @@ public function isOperatorSupported(string $operatorSigil, Type $operand): bool
2330
return false;
2431
}
2532

26-
$gmpType = new ObjectType('GMP');
27-
return $gmpType->isSuperTypeOf($operand)->yes();
33+
return $this->gmpType->isSuperTypeOf($operand)->yes();
2834
}
2935

3036
public function specifyType(string $operatorSigil, Type $operand): Type
3137
{
32-
return new ObjectType('GMP');
38+
return $this->gmpType;
3339
}
3440

3541
}

0 commit comments

Comments
 (0)