Skip to content

Commit ac77308

Browse files
staabmphpstan-bot
authored andcommitted
Added regression test (phpstan#5088)
1 parent 1200be0 commit ac77308

File tree

6 files changed

+102
-0
lines changed

6 files changed

+102
-0
lines changed

tests/PHPStan/Rules/Comparison/BooleanNotConstantConditionRuleTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,4 +229,10 @@ public function testBug5984(): void
229229
$this->analyse([__DIR__ . '/data/bug-5984.php'], []);
230230
}
231231

232+
public function testBug6702(): void
233+
{
234+
$this->treatPhpDocTypesAsCertain = true;
235+
$this->analyse([__DIR__ . '/data/bug-6702.php'], []);
236+
}
237+
232238
}

tests/PHPStan/Rules/Comparison/IfConstantConditionRuleTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,4 +194,10 @@ public function testBug13384b(): void
194194
]);
195195
}
196196

197+
public function testBug7699(): void
198+
{
199+
$this->treatPhpDocTypesAsCertain = true;
200+
$this->analyse([__DIR__ . '/data/bug-7699.php'], []);
201+
}
202+
197203
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Bug6702;
4+
5+
interface LineScanner
6+
{
7+
function isDone(): bool;
8+
9+
function getColumn(): int;
10+
11+
/**
12+
* Reads the char at the current position and moves the cursor.
13+
* @phpstan-impure
14+
*/
15+
function readChar(): string;
16+
17+
function peekChar(int $offset = 0): string;
18+
19+
function scanChar(string $char): bool;
20+
}
21+
22+
function minimumIndentation(LineScanner $scanner): ?int
23+
{
24+
while (!$scanner->isDone() && $scanner->readChar() !== "\n") {
25+
}
26+
27+
if ($scanner->isDone()) {
28+
return $scanner->peekChar(-1) === "\n" ? -1 : null;
29+
}
30+
31+
$min = null;
32+
while (!$scanner->isDone()) {
33+
// Consume the indentation
34+
while (!$scanner->isDone()) {
35+
$next = $scanner->peekChar();
36+
if ($next !== ' ' && $next !== "\t") {
37+
break;
38+
}
39+
$scanner->readChar();
40+
}
41+
42+
if ($scanner->isDone() || $scanner->scanChar("\n")) {
43+
continue;
44+
}
45+
46+
$min = $min === null ? $scanner->getColumn() : min($min, $scanner->getColumn());
47+
48+
// Consume the rest of the line
49+
while (!$scanner->isDone() && $scanner->readChar() !== "\n") {
50+
}
51+
}
52+
53+
return $min ?? -1;
54+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Bug7699;
4+
5+
$selections = isset($_GET['x']) ? explode(',', $_GET['x']) : [];
6+
while ($selections && $id = array_shift($selections)) {
7+
if ($selections) {
8+
var_dump('x');
9+
}
10+
}

tests/PHPStan/Rules/Variables/IssetRuleTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,4 +506,11 @@ public function testBug10640(): void
506506
$this->analyse([__DIR__ . '/../../Analyser/nsrt/bug-10640.php'], []);
507507
}
508508

509+
public function testBug9503(): void
510+
{
511+
$this->treatPhpDocTypesAsCertain = true;
512+
513+
$this->analyse([__DIR__ . '/data/bug-9503.php'], []);
514+
}
515+
509516
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Bug9503;
4+
5+
class HelloWorld
6+
{
7+
public function sayHello(string $str): bool
8+
{
9+
if (preg_match('~x(a)?(b)?~', $str, $matches) > 0) {
10+
if (isset($matches[2]) && preg_match('~x2(a)?(b)?~', $matches[2], $matches) > 0) {
11+
if (isset($matches[2])) {
12+
return true;
13+
}
14+
}
15+
}
16+
17+
return false;
18+
}
19+
}

0 commit comments

Comments
 (0)