markTestSkipped('Cannot run test, base class "GMP" does not exist!'); } $this->addFile( 'somefile.php', ' 'GMP', '$b' => 'GMP', '$c' => 'GMP', '$d' => 'GMP', '$f' => 'GMP', '$g' => 'GMP', '$h' => 'GMP', '$i' => 'GMP', '$j' => 'GMP', '$k' => 'GMP', '$l' => 'GMP', '$m' => 'GMP', '$n' => 'GMP', '$o' => 'GMP', '$p' => 'GMP', '$q' => 'GMP', '$r' => 'GMP', '$s' => 'GMP', '$t' => 'GMP', ]; $context = new Context(); $this->analyzeFile('somefile.php', $context); $actual_vars = []; foreach ($assertions as $var => $_) { if (isset($context->vars_in_scope[$var])) { $actual_vars[$var] = (string)$context->vars_in_scope[$var]; } } $this->assertSame($assertions, $actual_vars); } public function testDecimalOperations(): void { if (!class_exists('Decimal\\Decimal')) { $this->markTestSkipped('Cannot run test, base class "Decimal\\Decimal" does not exist!'); } $this->addFile( 'somefile.php', ' 'Decimal\\Decimal', '$b' => 'Decimal\\Decimal', '$c' => 'Decimal\\Decimal', '$d' => 'Decimal\\Decimal', '$f' => 'Decimal\\Decimal', '$g' => 'Decimal\\Decimal', '$h' => 'Decimal\\Decimal', '$i' => 'Decimal\\Decimal', '$j' => 'Decimal\\Decimal', '$k' => 'Decimal\\Decimal', '$l' => 'Decimal\\Decimal', '$m' => 'Decimal\\Decimal', '$n' => 'Decimal\\Decimal', '$o' => 'Decimal\\Decimal', '$p' => 'Decimal\\Decimal', '$q' => 'Decimal\\Decimal', '$r' => 'Decimal\\Decimal', '$s' => 'Decimal\\Decimal', '$t' => 'Decimal\\Decimal', ]; $context = new Context(); $this->analyzeFile('somefile.php', $context); $actual_vars = []; foreach ($assertions as $var => $_) { if (isset($context->vars_in_scope[$var])) { $actual_vars[$var] = (string)$context->vars_in_scope[$var]; } } $this->assertSame($assertions, $actual_vars); } public function testStrictTrueEquivalence(): void { $config = Config::getInstance(); $config->strict_binary_operands = true; $this->addFile( 'somefile.php', 'expectException(CodeException::class); $this->expectExceptionMessage('RedundantIdentityWithTrue'); $this->analyzeFile('somefile.php', new Context()); } public function testStringFalseInequivalence(): void { $config = Config::getInstance(); $config->strict_binary_operands = true; $this->addFile( 'somefile.php', 'expectException(CodeException::class); $this->expectExceptionMessage('RedundantIdentityWithTrue'); $this->analyzeFile('somefile.php', new Context()); } /** * @return iterable,error_levels?:string[]}> */ public function providerValidCodeParse(): iterable { return [ 'regularAddition' => [ ' [ '$a' => 'int', ], ], 'differingNumericTypesAdditionInWeakMode' => [ ' [ '$a' => 'float', ], ], 'modulo' => [ ' [ '$a' => 'int', '$b' => 'int', '$c' => 'int', '$d' => 'int', '$e' => 'int', ], ], 'numericAddition' => [ ' [ ' [ ' [ ' [ '$a' => 'string',//will contain "75" ] ], 'concatenationWithTwoInt' => [ ' [ ' false, "foobaz" => true, "barbaz" => true]; $foo = random_int(0, 1) ? "foo" : "bar"; $foo .= "baz"; $val = $arr[$foo]; ', 'assertions' => ['$val' => 'true'], ], 'concatenateLiteralIntAndString' => [ ' false, "foo123" => true]; $foo = "foo"; $foo .= 123; $val = $arr[$foo]; ', 'assertions' => ['$val' => 'true'], ], 'concatenateNonEmptyResultsInNonEmpty' => [ ' [ ' [ ' [ ' [ ' [ ' [ ' [], 'error_levels' => ['PossiblyFalseOperand'], ], 'bitwiseoperations' => [ '> 2; $f = "a" & "b";', 'assertions' => [ '$a' => 'int', '$b' => 'int', '$c' => 'int', '$d' => 'int', '$e' => 'int', '$f' => 'string', ], ], 'ComplexLiteralBitwise' => [ ' [ ' [ '$a' => 'int', '$b' => 'int', '$c' => 'bool', '$d' => 'bool', ], ], 'ternaryAssignment' => [ ' [ ' [ ' [ '$a' => 'float', '$b' => 'float', ], ], 'exponent' => [ ' [ '$b' => 'int', ], ], 'bitwiseNot' => [ ' [ '$a' => 'int', '$b' => 'int', '$c' => 'int', '$d' => 'string', ], ], 'stringIncrementSuppressed' => [ ' [ '$a' => 'string', ], ], 'stringIncrementWithCheck' => [ ' [ '$a===' => 'non-empty-string', ], ], 'nullCoalescingAssignment' => [ ' [], 'error_levels' => [], '7.4', ], 'nullCoalescingArrayAssignment' => [ ' $arr */ function foo(array $arr) : void { $b = []; foreach ($arr as $a) { $b[0] ??= $a; } }', 'assertions' => [], 'error_levels' => [], '7.4', ], 'addArrays' => [ ' 5]; }' ], 'addIntToZero' => [ ' [ ' [ ' [ ' $b); }' ], 'notAlwaysPositiveBitOperations' => [ '> $b)) { echo "Actually, zero\n"; } if (8 === PHP_INT_SIZE) { if (0 === ($a << $d)) { echo "Actually, zero\n"; } }' ], 'IntOverflowMul' => [ ' [ '$a' => 'float' ], ], 'IntOverflowPow' => [ ' [ '$a' => 'float' ], ], 'IntOverflowPlus' => [ ' [ '$a' => 'int', '$b' => 'float', ], ], 'IntOverflowPowSub' => [ ' [ '$a' => 'float' ], ], 'IntOverflowSub' => [ ' [ '$a' => 'float' ], ], 'literalConcatCreatesLiteral' => [ ' [ ' [ ' [ ' [ ' ['$interpolated===' => '"12.3foobar"'], ], 'concatenatedStringIsInferredAsLiteral' => [ ' ['$concatenated===' => '"12.3foobar"'], ], 'encapsedNonEmptyNonSpecificLiteralString' => [ ' ['$interpolated===' => 'non-empty-literal-string'], ], 'concatenatedNonEmptyNonSpecificLiteralString' => [ ' ['$concatenated===' => 'non-empty-literal-string'], ], 'encapsedPossiblyEmptyLiteralString' => [ ' ['$interpolated===' => 'literal-string'], ], 'literalIntConcatCreatesLiteral' => [ ' [ ' [ ' [ '$a' => 'float|int', '$b' => 'float|int', '$c' => 'float|int', '$d' => 'float|int', ], ], 'encapsedStringWithIntIncludingLiterals' => [ ' [ ' [ ' [ ' [ '$a' => 'float|int', '$b' => 'float|int', ], ], 'coalesceFilterOutNullEvenWithTernary' => [ 'toString() : null) ?? "Not a stringable foo"; }', ], ]; } /** * @return iterable */ public function providerInvalidCodeParse(): iterable { return [ 'badAddition' => [ ' 'InvalidOperand', ], 'differingNumericTypesAdditionInStrictMode' => [ ' 'InvalidOperand', 'error_levels' => [], 'strict_mode' => true, ], 'concatenationWithNumberInStrictMode' => [ ' 'InvalidOperand', 'error_levels' => [], 'strict_mode' => true, ], 'concatenateNegativeIntRightSideIsNotNumeric' => [ ' 'ArgumentTypeCoercion', ], 'addArrayToNumber' => [ ' 'InvalidOperand', 'error_levels' => [], 'strict_mode' => true, ], 'additionWithClassInWeakMode' => [ ' 'InvalidOperand', ], 'possiblyInvalidOperand' => [ ' 'PossiblyInvalidOperand', ], 'possiblyInvalidConcat' => [ ' 'PossiblyInvalidOperand', ], 'invalidGMPOperation' => [ ' 'InvalidOperand - src' . DIRECTORY_SEPARATOR . 'somefile.php:3:26 - Cannot add GMP to non-numeric type', ], 'stringIncrement' => [ ' 'StringIncrement', ], 'falseIncrement' => [ ' 'FalseOperand', ], 'trueIncrement' => [ ' 'InvalidOperand', ], 'possiblyDivByZero' => [ ' 'PossiblyNullOperand', ], 'invalidExponent' => [ ' 'InvalidOperand', ], 'invalidBitwiseOr' => [ ' 'InvalidOperand', ], 'invalidBitwiseNot' => [ ' 'InvalidOperand', ], 'possiblyInvalidBitwiseNot' => [ ' 'PossiblyInvalidOperand', ], 'invalidBooleanBitwiseNot' => [ ' 'InvalidOperand', ], 'substrImpossible' => [ ' 'TypeDoesNotContainType', ], 'literalConcatWithStringCreatesString' => [ ' 'LessSpecificReturnStatement', ], 'encapsedConcatWithStringCreatesString' => [ ' 'LessSpecificReturnStatement', ], ]; } }