Skip to content

Commit

Permalink
Implement php version dependent loose const comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Feb 5, 2023
1 parent e5c1a59 commit 869c9fa
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 0 deletions.
41 changes: 41 additions & 0 deletions tests/PHPStan/Analyser/LooseConstComparisonTestPhp7.php
@@ -0,0 +1,41 @@
<?php declare(strict_types = 1);

namespace PHPStan\Analyser;

use EnumTypeAssertions\Foo;
use PHPStan\Testing\TypeInferenceTestCase;
use stdClass;
use function define;
use function extension_loaded;
use const PHP_INT_SIZE;
use const PHP_VERSION_ID;

class LooseConstComparisonTestPhp7 extends TypeInferenceTestCase
{
public function dataFileAsserts(): iterable
{
yield from $this->gatherAssertTypes(__DIR__ . '/data/loose-const-comparison-php7.php');
}

/**
* @dataProvider dataFileAsserts
* @param mixed ...$args
*/
public function testFileAsserts(
string $assertType,
string $file,
...$args,
): void
{
$this->assertFileAsserts($assertType, $file, ...$args);
}

public static function getAdditionalConfigFiles(): array
{
return [
__DIR__ . '/../../../conf/bleedingEdge.neon',
__DIR__ . '/looseConstComparisonPhp7.neon',
];
}

}
41 changes: 41 additions & 0 deletions tests/PHPStan/Analyser/LooseConstComparisonTestPhp8.php
@@ -0,0 +1,41 @@
<?php declare(strict_types = 1);

namespace PHPStan\Analyser;

use EnumTypeAssertions\Foo;
use PHPStan\Testing\TypeInferenceTestCase;
use stdClass;
use function define;
use function extension_loaded;
use const PHP_INT_SIZE;
use const PHP_VERSION_ID;

class LooseConstComparisonTestPhp8 extends TypeInferenceTestCase
{
public function dataFileAsserts(): iterable
{
yield from $this->gatherAssertTypes(__DIR__ . '/data/loose-const-comparison-php8.php');
}

/**
* @dataProvider dataFileAsserts
* @param mixed ...$args
*/
public function testFileAsserts(
string $assertType,
string $file,
...$args,
): void
{
$this->assertFileAsserts($assertType, $file, ...$args);
}

public static function getAdditionalConfigFiles(): array
{
return [
__DIR__ . '/../../../conf/bleedingEdge.neon',
__DIR__ . '/looseConstComparisonPhp8.neon',
];
}

}
16 changes: 16 additions & 0 deletions tests/PHPStan/Analyser/data/loose-const-comparison-php7.php
@@ -0,0 +1,16 @@
<?php

namespace LooseConstComparisonPhp7;

use function PHPStan\Testing\assertType;

function doFoo() {
assertType('true', 0 == "0");
assertType('true', 0 == "0.0");
assertType('true', 0 == "foo");
assertType('true', 0 == "");
assertType('true', 42 == " 42");
assertType('true', 42 == "42foo");

assertType('true', 0.0 == "");
}
16 changes: 16 additions & 0 deletions tests/PHPStan/Analyser/data/loose-const-comparison-php8.php
@@ -0,0 +1,16 @@
<?php

namespace LooseConstComparisonPhp8;

use function PHPStan\Testing\assertType;

function doFoo() {
assertType('true', 0 == "0");
assertType('true', 0 == "0.0");
assertType('false', 0 == "foo");
assertType('false', 0 == "");
assertType('true', 42 == " 42");
assertType('false', 42 == "42foo");

assertType('false', 0.0 == "");
}
2 changes: 2 additions & 0 deletions tests/PHPStan/Analyser/looseConstComparisonPhp7.neon
@@ -0,0 +1,2 @@
parameters:
phpVersion: 70400 # PHP 7.4
2 changes: 2 additions & 0 deletions tests/PHPStan/Analyser/looseConstComparisonPhp8.neon
@@ -0,0 +1,2 @@
parameters:
phpVersion: 80000 # PHP 8.0

0 comments on commit 869c9fa

Please sign in to comment.