From 4fe9484b2195702d204b28bab09bdee46dd754f0 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sun, 5 Jul 2026 10:59:04 +0200 Subject: [PATCH] Cache ObjectType instead of re-creating it over and over --- ...hNumberOperatorTypeSpecifyingExtension.php | 16 +++++++-------- .../GmpOperatorTypeSpecifyingExtension.php | 20 +++++++++++-------- ...mpUnaryOperatorTypeSpecifyingExtension.php | 12 ++++++++--- 3 files changed, 29 insertions(+), 19 deletions(-) diff --git a/src/Type/Php/BcMathNumberOperatorTypeSpecifyingExtension.php b/src/Type/Php/BcMathNumberOperatorTypeSpecifyingExtension.php index 8539349a156..42b0a85c7a7 100644 --- a/src/Type/Php/BcMathNumberOperatorTypeSpecifyingExtension.php +++ b/src/Type/Php/BcMathNumberOperatorTypeSpecifyingExtension.php @@ -17,8 +17,11 @@ final class BcMathNumberOperatorTypeSpecifyingExtension implements OperatorTypeSpecifyingExtension { + private ObjectType $mathNumberType; + public function __construct(private PhpVersion $phpVersion) { + $this->mathNumberType = new ObjectType('BcMath\Number'); } public function isOperatorSupported(string $operatorSigil, Type $leftSide, Type $rightSide): bool @@ -27,19 +30,16 @@ public function isOperatorSupported(string $operatorSigil, Type $leftSide, Type return false; } - $bcMathNumberType = new ObjectType('BcMath\Number'); - return in_array($operatorSigil, ['-', '+', '*', '/', '**', '%', '<', '<=', '>', '>=', '==', '!=', '<=>'], true) && ( - $bcMathNumberType->isSuperTypeOf($leftSide)->yes() - || $bcMathNumberType->isSuperTypeOf($rightSide)->yes() + $this->mathNumberType->isSuperTypeOf($leftSide)->yes() + || $this->mathNumberType->isSuperTypeOf($rightSide)->yes() ); } public function specifyType(string $operatorSigil, Type $leftSide, Type $rightSide): Type { - $bcMathNumberType = new ObjectType('BcMath\Number'); - $otherSide = $bcMathNumberType->isSuperTypeOf($leftSide)->yes() + $otherSide = $this->mathNumberType->isSuperTypeOf($leftSide)->yes() ? $rightSide : $leftSide; @@ -58,9 +58,9 @@ public function specifyType(string $operatorSigil, Type $leftSide, Type $rightSi if ( $otherSide->isInteger()->yes() || $otherSide->isNumericString()->yes() - || $bcMathNumberType->isSuperTypeOf($otherSide)->yes() + || $this->mathNumberType->isSuperTypeOf($otherSide)->yes() ) { - return $bcMathNumberType; + return $this->mathNumberType; } return new ErrorType(); diff --git a/src/Type/Php/GmpOperatorTypeSpecifyingExtension.php b/src/Type/Php/GmpOperatorTypeSpecifyingExtension.php index ef0a45a956d..8df569d510e 100644 --- a/src/Type/Php/GmpOperatorTypeSpecifyingExtension.php +++ b/src/Type/Php/GmpOperatorTypeSpecifyingExtension.php @@ -16,25 +16,29 @@ final class GmpOperatorTypeSpecifyingExtension implements OperatorTypeSpecifyingExtension { + private ObjectType $gmpType; + + public function __construct() + { + $this->gmpType = new ObjectType('GMP'); + } + public function isOperatorSupported(string $operatorSigil, Type $leftSide, Type $rightSide): bool { if ($leftSide instanceof NeverType || $rightSide instanceof NeverType) { return false; } - $gmpType = new ObjectType('GMP'); - return in_array($operatorSigil, ['+', '-', '*', '/', '**', '%', '&', '|', '^', '<<', '>>', '<', '<=', '>', '>=', '==', '!=', '<=>'], true) && ( - $gmpType->isSuperTypeOf($leftSide)->yes() - || $gmpType->isSuperTypeOf($rightSide)->yes() + $this->gmpType->isSuperTypeOf($leftSide)->yes() + || $this->gmpType->isSuperTypeOf($rightSide)->yes() ); } public function specifyType(string $operatorSigil, Type $leftSide, Type $rightSide): Type { - $gmpType = new ObjectType('GMP'); - $otherSide = $gmpType->isSuperTypeOf($leftSide)->yes() + $otherSide = $this->gmpType->isSuperTypeOf($leftSide)->yes() ? $rightSide : $leftSide; @@ -51,9 +55,9 @@ public function specifyType(string $operatorSigil, Type $leftSide, Type $rightSi if ( $otherSide->isInteger()->yes() || $otherSide->isNumericString()->yes() - || $gmpType->isSuperTypeOf($otherSide)->yes() + || $this->gmpType->isSuperTypeOf($otherSide)->yes() ) { - return $gmpType; + return $this->gmpType; } return new ErrorType(); diff --git a/src/Type/Php/GmpUnaryOperatorTypeSpecifyingExtension.php b/src/Type/Php/GmpUnaryOperatorTypeSpecifyingExtension.php index 4aaa3de4d85..f9b66116555 100644 --- a/src/Type/Php/GmpUnaryOperatorTypeSpecifyingExtension.php +++ b/src/Type/Php/GmpUnaryOperatorTypeSpecifyingExtension.php @@ -13,6 +13,13 @@ final class GmpUnaryOperatorTypeSpecifyingExtension implements UnaryOperatorTypeSpecifyingExtension { + private ObjectType $gmpType; + + public function __construct() + { + $this->gmpType = new ObjectType('GMP'); + } + public function isOperatorSupported(string $operatorSigil, Type $operand): bool { if ($operand instanceof NeverType) { @@ -23,13 +30,12 @@ public function isOperatorSupported(string $operatorSigil, Type $operand): bool return false; } - $gmpType = new ObjectType('GMP'); - return $gmpType->isSuperTypeOf($operand)->yes(); + return $this->gmpType->isSuperTypeOf($operand)->yes(); } public function specifyType(string $operatorSigil, Type $operand): Type { - return new ObjectType('GMP'); + return $this->gmpType; } }