|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Type; |
| 4 | + |
| 5 | +use Closure; |
| 6 | +use PHPStan\Fixture\TestDecimal; |
| 7 | +use PHPStan\Php\PhpVersion; |
| 8 | +use PHPStan\Testing\PHPStanTestCase; |
| 9 | +use PHPStan\Type\Constant\ConstantIntegerType; |
| 10 | +use PHPStan\Type\TypeCombinator; |
| 11 | +use PHPStan\Type\VerbosityLevel; |
| 12 | +use function sprintf; |
| 13 | + |
| 14 | +class BcMathNumberOperatorTypeSpecifyingExtensionTest extends PHPStanTestCase |
| 15 | +{ |
| 16 | + |
| 17 | + /** |
| 18 | + * @dataProvider dataSigilAndSidesProvider |
| 19 | + */ |
| 20 | + public function test(string $sigil, Type $left, Type $right, string $expected): void |
| 21 | + { |
| 22 | + $extension = self::getContainer()->getByType(BcMathNumberOperatorTypeSpecifyingExtension::class); |
| 23 | + |
| 24 | + $this->assertTrue($extension->isOperatorSupported($sigil, $left, $right)); |
| 25 | + |
| 26 | + $actualType = $extension->specifyType($sigil, $left, $right)->describe(VerbosityLevel::precise()); |
| 27 | + $this->assertSame($expected, $actualType); |
| 28 | + } |
| 29 | + |
| 30 | + public static function dataSigilAndSidesProvider(): iterable |
| 31 | + { |
| 32 | + $phpVersion = self::getContainer()->getByType(PhpVersion::class); |
| 33 | + if (!$phpVersion->supportsBcMathNumberOperatorOverloading()) { |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + $supportedOperators = Closure::bind( |
| 38 | + static fn () => BcMathNumberOperatorTypeSpecifyingExtension::OPERATORS, |
| 39 | + null, |
| 40 | + BcMathNumberOperatorTypeSpecifyingExtension::class, |
| 41 | + )(); |
| 42 | + foreach ($supportedOperators as $operator => $_) { |
| 43 | + yield sprintf('BcMath\Number %s BcMath\Number', $operator) => [ |
| 44 | + 'sigil' => $operator, |
| 45 | + 'left' => new ObjectType('BcMath\Number'), |
| 46 | + 'right' => new ObjectType('BcMath\Number'), |
| 47 | + 'expected' => 'BcMath\Number', |
| 48 | + ]; |
| 49 | + } |
| 50 | + |
| 51 | + $oneType = new ConstantIntegerType(1); |
| 52 | + |
| 53 | + |
| 54 | + yield 'BcMath\Number + 1' => [ |
| 55 | + 'sigil' => '+', |
| 56 | + 'left' => new ObjectType('BcMath\Number'), |
| 57 | + 'right' => new ConstantIntegerType(1), |
| 58 | + 'expected' => 'BcMath\Number', |
| 59 | + ]; |
| 60 | + |
| 61 | + yield 'BcMath\Number + 1' => [ |
| 62 | + 'sigil' => '+', |
| 63 | + 'left' => new ObjectType('BcMath\Number'), |
| 64 | + 'right' => TypeCombinator::union( |
| 65 | + new ObjectType('BcMath\Number'), |
| 66 | + new StringType(), |
| 67 | + ), |
| 68 | + 'expected' => 'BcMath\Number', |
| 69 | + ]; |
| 70 | + |
| 71 | + yield '1|2|BcMath\Number + 3|4|BcMath\Number' => [ |
| 72 | + 'sigil' => '+', |
| 73 | + 'left' => TypeCombinator::union( |
| 74 | + new ObjectType('BcMath\Number'), |
| 75 | + new ConstantIntegerType(1), |
| 76 | + new ConstantIntegerType(2), |
| 77 | + ), |
| 78 | + 'right' => TypeCombinator::union( |
| 79 | + new ObjectType('BcMath\Number'), |
| 80 | + new ConstantIntegerType(3), |
| 81 | + new ConstantIntegerType(4), |
| 82 | + ), |
| 83 | + 'expected' => '4|5|6|BcMath\Number', |
| 84 | + ]; |
| 85 | + |
| 86 | + yield 'int|BcMath\Number + float|BcMath\Number' => [ |
| 87 | + 'sigil' => '+', |
| 88 | + 'left' => TypeCombinator::union( |
| 89 | + new ObjectType('BcMath\Number'), |
| 90 | + new IntegerType(), |
| 91 | + ), |
| 92 | + 'right' => TypeCombinator::union( |
| 93 | + new ObjectType('BcMath\Number'), |
| 94 | + new IntegerType(), |
| 95 | + new FloatType(), |
| 96 | + ), |
| 97 | + 'expected' => 'BcMath\Number|float|int', |
| 98 | + ]; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * @dataProvider dataNotMatchingSidesProvider |
| 103 | + */ |
| 104 | + public function testNotSupportsNotMatchingSides(string $sigil, Type $left, Type $right): void |
| 105 | + { |
| 106 | + $extension = self::getContainer()->getByType(BcMathNumberOperatorTypeSpecifyingExtension::class); |
| 107 | + |
| 108 | + $this->assertFalse($extension->isOperatorSupported($sigil, $left, $right)); |
| 109 | + } |
| 110 | + |
| 111 | + public static function dataNotMatchingSidesProvider(): iterable |
| 112 | + { |
| 113 | + $phpVersion = self::getContainer()->getByType(PhpVersion::class); |
| 114 | + if (!$phpVersion->supportsBcMathNumberOperatorOverloading()) { |
| 115 | + $desc = sprintf("BcMath\Number is supported in PHP 8.4.0 and above. %s was given.", $phpVersion->getVersionString()); |
| 116 | + yield $desc => [ |
| 117 | + 'sigil' => '+', |
| 118 | + 'left' => new ObjectType('BcMath\Number'), |
| 119 | + 'right' => new ObjectType('BcMath\Number'), |
| 120 | + ]; |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + $notSupportedOperators = ['&', '|', '^', '||', '&&']; |
| 125 | + foreach ($notSupportedOperators as $notSupportedOperator) { |
| 126 | + yield sprintf('Do not support %s operator', $notSupportedOperator) => [ |
| 127 | + $notSupportedOperator, |
| 128 | + new ObjectType('BcMath\Number'), |
| 129 | + new ObjectType('BcMath\Number'), |
| 130 | + ]; |
| 131 | + } |
| 132 | + |
| 133 | + yield sprintf('Do not support %s operator', $notSupportedOperator) => [ |
| 134 | + $notSupportedOperator, |
| 135 | + new ObjectType('BcMath\Number'), |
| 136 | + new ObjectType('BcMath\Number'), |
| 137 | + ]; |
| 138 | + } |
| 139 | + |
| 140 | +} |
0 commit comments