Skip to content

Commit 155d937

Browse files
committed
added more tests
1 parent 414af5c commit 155d937

4 files changed

Lines changed: 133 additions & 0 deletions

File tree

tests/PHPStan/Rules/Pure/PureFunctionRuleTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,4 +214,9 @@ public function testBug14504(): void
214214
$this->analyse([__DIR__ . '/data/bug-14504.php'], []);
215215
}
216216

217+
public function testBug14511(): void
218+
{
219+
$this->analyse([__DIR__ . '/data/bug-14511.php'], []);
220+
}
221+
217222
}

tests/PHPStan/Rules/Pure/PureMethodRuleTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,4 +383,10 @@ public function testBug14504(): void
383383
$this->analyse([__DIR__ . '/data/bug-14504-method.php'], []);
384384
}
385385

386+
public function testBug14511(): void
387+
{
388+
$this->treatPhpDocTypesAsCertain = true;
389+
$this->analyse([__DIR__ . '/data/bug-14511-method.php'], []);
390+
}
391+
386392
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug14511Method;
4+
5+
class Foo
6+
{
7+
8+
/**
9+
* @phpstan-pure
10+
* @template T of mixed
11+
* @param T $val
12+
*/
13+
public function testStringCast(mixed $val): ?string
14+
{
15+
if (is_int($val)) {
16+
return (string) $val;
17+
}
18+
return null;
19+
}
20+
21+
/**
22+
* @phpstan-pure
23+
* @template T of mixed
24+
* @param T $val
25+
*/
26+
public function testStringConcat(mixed $val): ?string
27+
{
28+
if (is_int($val)) {
29+
return 'value: ' . $val;
30+
}
31+
return null;
32+
}
33+
34+
/**
35+
* @phpstan-pure
36+
* @template T of mixed
37+
* @param T $val
38+
*/
39+
public function testEmptyNonArray(mixed $val): ?string
40+
{
41+
if (empty($val) && !\is_array($val)) {
42+
return (string) $val;
43+
}
44+
return null;
45+
}
46+
47+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug14511;
4+
5+
/**
6+
* @phpstan-pure
7+
* @template T of mixed
8+
* @param T $val
9+
*/
10+
function testStringCast(mixed $val): ?string {
11+
if (is_int($val)) {
12+
return (string) $val;
13+
}
14+
return null;
15+
}
16+
17+
/**
18+
* @phpstan-pure
19+
* @template T of mixed
20+
* @param T $val
21+
*/
22+
function testStringConcat(mixed $val): ?string {
23+
if (is_int($val)) {
24+
return 'value: ' . $val;
25+
}
26+
return null;
27+
}
28+
29+
/**
30+
* @phpstan-pure
31+
* @template T of mixed
32+
* @param T $val
33+
*/
34+
function testFloatCast(mixed $val): ?string {
35+
if (is_float($val)) {
36+
return (string) $val;
37+
}
38+
return null;
39+
}
40+
41+
/**
42+
* @phpstan-pure
43+
* @template T of mixed
44+
* @param T $val
45+
*/
46+
function testBoolCast(mixed $val): ?string {
47+
if (is_bool($val)) {
48+
return (string) $val;
49+
}
50+
return null;
51+
}
52+
53+
/**
54+
* @phpstan-pure
55+
* @template T of mixed
56+
* @param T $val
57+
*/
58+
function testStringVal(mixed $val): ?string {
59+
if (is_string($val)) {
60+
return (string) $val;
61+
}
62+
return null;
63+
}
64+
65+
/**
66+
* @phpstan-pure
67+
* @template T of mixed
68+
* @param T $val
69+
*/
70+
function testEmptyNonArray(mixed $val): ?string {
71+
if (empty($val) && !\is_array($val)) {
72+
return (string) $val;
73+
}
74+
return null;
75+
}

0 commit comments

Comments
 (0)