Skip to content

Commit

Permalink
Regression tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jan 5, 2025
1 parent a2d5bb1 commit 3ff4184
Show file tree
Hide file tree
Showing 9 changed files with 188 additions and 0 deletions.
12 changes: 12 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-10338.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php declare(strict_types = 1);

use function PHPStan\Testing\assertType;

function (): void {
$content = file_get_contents('');
if ($content == '') {
die;
}

assertType('string', $content);
};
Original file line number Diff line number Diff line change
Expand Up @@ -1968,4 +1968,9 @@ public function testBug11418(): void
$this->analyse([__DIR__ . '/data/bug-11418.php'], []);
}

public function testBug3107(): void
{
$this->analyse([__DIR__ . '/data/bug-3107.php'], []);
}

}
23 changes: 23 additions & 0 deletions tests/PHPStan/Rules/Functions/data/bug-3107.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Bug3107;

class Holder
{
/** @var string */
public $val;
}

/** @param mixed $mixed */
function test($mixed): void
{
$holder = new Holder();
$holder->val = $mixed;

$a = [];
$a[$holder->val] = 1;
take($a);
}

/** @param array<string, int> $a */
function take($a): void {}
46 changes: 46 additions & 0 deletions tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1171,4 +1171,50 @@ public function testShortGetPropertyHook(): void
]);
}

public function testBug1O580(): void
{
if (PHP_VERSION_ID < 80000) {
$this->markTestSkipped('Test requires PHP 8.0.');
}

$this->analyse([__DIR__ . '/data/bug-10580.php'], [
[
'Method Bug10580\FooA::fooThisInterface() should return $this(Bug10580\FooA) but returns Bug10580\FooA.',
18,
],
[
'Method Bug10580\FooA::fooThisClass() should return $this(Bug10580\FooA) but returns Bug10580\FooA.',
19,
],
[
'Method Bug10580\FooA::fooThisSelf() should return $this(Bug10580\FooA) but returns Bug10580\FooA.',
20,
],
[
'Method Bug10580\FooA::fooThisStatic() should return $this(Bug10580\FooA) but returns Bug10580\FooA.',
21,
],
[
'Method Bug10580\FooB::fooThisInterface() should return $this(Bug10580\FooB) but returns Bug10580\FooB.',
27,
],
[
'Method Bug10580\FooB::fooThisClass() should return $this(Bug10580\FooB) but returns Bug10580\FooB.',
29,
],
[
'Method Bug10580\FooB::fooThisSelf() should return $this(Bug10580\FooB) but returns Bug10580\FooB.',
31,
],
[
'Method Bug10580\FooB::fooThisStatic() should return $this(Bug10580\FooB) but returns Bug10580\FooB.',
33,
],
[
'Method Bug10580\FooB::fooThis() should return $this(Bug10580\FooB) but returns Bug10580\FooB.',
35,
],
]);
}

}
36 changes: 36 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-10580.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php // lint >= 8.0

namespace Bug10580;

interface FooI {
/** @return $this */
public function fooThisInterface(): FooI;
/** @return $this */
public function fooThisClass(): FooI;
/** @return $this */
public function fooThisSelf(): self;
/** @return $this */
public function fooThisStatic(): static;
}

final class FooA implements FooI
{
public function fooThisInterface(): FooI { return new FooA(); }
public function fooThisClass(): FooA { return new FooA(); }
public function fooThisSelf(): self { return new FooA(); }
public function fooThisStatic(): static { return new FooA(); }
}

final class FooB implements FooI
{
/** @return $this */
public function fooThisInterface(): FooI { return new FooB(); }
/** @return $this */
public function fooThisClass(): FooB { return new FooB(); }
/** @return $this */
public function fooThisSelf(): self { return new FooB(); }
/** @return $this */
public function fooThisStatic(): static { return new FooB(); }
/** @return $this */
public function fooThis(): static { return new FooB(); }
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,9 @@ public function testBug7310(): void
$this->analyse([__DIR__ . '/data/bug-7310.php'], []);
}

public function testBug11939(): void
{
$this->analyse([__DIR__ . '/data/bug-11939.php'], []);
}

}
36 changes: 36 additions & 0 deletions tests/PHPStan/Rules/PhpDoc/data/bug-11939.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php // lint >= 8.1

declare(strict_types=1);

namespace Bug11939;

enum What
{
case This;
case That;

/**
* @return ($this is self::This ? 'here' : 'there')
*/
public function where(): string
{
return match ($this) {
self::This => 'here',
self::That => 'there'
};
}
}

class Where
{
/**
* @return ($what is What::This ? 'here' : 'there')
*/
public function __invoke(What $what): string
{
return match ($what) {
What::This => 'here',
What::That => 'there'
};
}
}
6 changes: 6 additions & 0 deletions tests/PHPStan/Rules/Pure/PureMethodRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,10 @@ public function dataBug11207(): array
];
}

public function testBug12048(): void
{
$this->treatPhpDocTypesAsCertain = true;
$this->analyse([__DIR__ . '/data/bug-12048.php'], []);
}

}
19 changes: 19 additions & 0 deletions tests/PHPStan/Rules/Pure/data/bug-12048.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Bug12048;

class HelloWorld
{
/** @phpstan-pure */
public function sayHello(string $s): string
{
$a = md5( $s );
$a .= hash( 'md5', $s );
$a .= hash_hmac( 'sha1', $s, 'b' );

$a .= hash( 'sha256', $s );
$a .= hash_hmac( 'sha256', $s, 'b' );

return $a;
}
}

0 comments on commit 3ff4184

Please sign in to comment.