Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added more integer range vs. int constants tests #1750

Merged
merged 2 commits into from Oct 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -476,4 +476,31 @@ public function testBug6356b(): void
]);
}

public function testIntegerRangesAndConstants(): void
{
$this->checkExplicitMixed = true;
$this->analyse([__DIR__ . '/data/int-ranges-and-constants.php'], [
[
'Property IntegerRangesAndConstants\HelloWorld::$i (0|1|3) does not accept int<0, 3>.',
17,
],
[
'Property IntegerRangesAndConstants\HelloWorld::$x (int<0, 3>) does not accept 0|1|2|3|string.',
42,
],
[
'Property IntegerRangesAndConstants\HelloWorld::$x (int<0, 3>) does not accept 0|1|2|3|bool.',
43,
],
[
'Property IntegerRangesAndConstants\HelloWorld::$x (int<0, 3>) does not accept 0|1|3|bool.',
44,
],
[
'Property IntegerRangesAndConstants\HelloWorld::$x (int<0, 3>) does not accept 0|1|3|4.',
45,
],
]);
}

}
56 changes: 56 additions & 0 deletions tests/PHPStan/Rules/Properties/data/int-ranges-and-constants.php
@@ -0,0 +1,56 @@
<?php

namespace IntegerRangesAndConstants;

class HelloWorld
{
/** @var 0|1|3 */
public $i = 0;
/** @var 0|1|2|3|string */
public $j = 0;
/** @var 0|1|bool|2|3 */
public $k = 0;
/** @var 0|1|2|3|4|5 */
public $l;

public function test(): void {
$this->i = random_int(0, 3);
$this->j = random_int(0, 3);
$this->k = random_int(0, 3);
$this->l = random_int(0, 3);
}

/**
* @var int<0,3>
*/
public $x = 0;

/**
* @param 0| $a
* @param 0|1 $b
* @param 0|1|3 $c
* @param 0|1|2|3|string $j
* @param 0|1|bool|2|3 $k
* @param 0|1|bool|3 $l
* @param 0|1|3|4 $m
*/
public function test2($a, $b, $c, $j, $k, $l, $m): void {
$this->x = $a;
$this->x = $b;
$this->x = $c;

$this->x = $j;
$this->x = $k;
$this->x = $l;
$this->x = $m;
}

const I_1=1;
const I_2=2;

/** @param int-mask<self::I_*> $flag */
public function sayHello($flag): void
{
$this->x = $flag;
}
}