markTestSkipped('Cannot run test, base class "GMP" does not exist!'); return; } $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 \Psalm\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 = \Psalm\Config::getInstance(); $config->strict_binary_operands = true; $this->addFile( 'somefile.php', 'expectException(\Psalm\Exception\CodeException::class); $this->expectExceptionMessage('RedundantIdentityWithTrue'); $this->analyzeFile('somefile.php', new \Psalm\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"; } } else { if (0 === ($a << $c)) { echo "Actually, zero\n"; } }' ], 'IntOverflowMul' => [ ' [ '$a' => 'float' ], ], 'IntOverflowPow' => [ ' [ '$a' => 'float' ], ], 'IntOverflowPlus' => [ ' [ '$a' => 'int', '$b' => 'float', ], ], 'IntOverflowPowSub' => [ ' [ '$a' => 'float' ], ], 'IntOverflowSub' => [ ' [ '$a' => 'float' ], ], 'literalConcatCreatesLiteral' => [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' */ 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', ], ]; } }