Skip to content

Commit 9e0daad

Browse files
committed
Add tests for @pure-unless-parameter-passed
1 parent 7c8046e commit 9e0daad

3 files changed

Lines changed: 132 additions & 0 deletions

File tree

‎tests/PHPStan/Rules/Pure/PureFunctionRuleTest.php‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,4 +378,32 @@ public function testPureUnlessCallableIsImpurePhp84(): void
378378
]);
379379
}
380380

381+
public function testPureUnlessParameterPassed(): void
382+
{
383+
$this->analyse([__DIR__ . '/data/pure-unless-parameter-passed.php'], [
384+
[
385+
'Possibly impure call to function PureUnlessParameterPassedFunction\myReplace() in pure function PureUnlessParameterPassedFunction\purePassingByRef().',
386+
44,
387+
],
388+
[
389+
'Possibly impure call to function PureUnlessParameterPassedFunction\myReplacePhpstanAlias() in pure function PureUnlessParameterPassedFunction\purePassingByRefAlias().',
390+
62,
391+
],
392+
]);
393+
}
394+
395+
public function testPureUnlessParameterPassedBuiltin(): void
396+
{
397+
$this->analyse([__DIR__ . '/data/pure-unless-parameter-passed-builtin.php'], [
398+
[
399+
'Possibly impure call to function str_replace() in pure function PureUnlessParameterPassedBuiltin\pureStrReplaceWithCount().',
400+
22,
401+
],
402+
[
403+
'Possibly impure call to function preg_match() in pure function PureUnlessParameterPassedBuiltin\purePregMatchWithMatches().',
404+
40,
405+
],
406+
]);
407+
}
408+
381409
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PureUnlessParameterPassedBuiltin;
4+
5+
/**
6+
* @phpstan-pure
7+
*/
8+
function pureStrReplaceWithoutCount(string $s): string
9+
{
10+
// The by-ref $count is omitted, so str_replace() is pure.
11+
return str_replace('a', 'b', $s);
12+
}
13+
14+
/**
15+
* @phpstan-pure
16+
*/
17+
function pureStrReplaceWithCount(string $s): string
18+
{
19+
// The by-ref $count is passed, so str_replace() is possibly impure.
20+
$count = 0;
21+
22+
return str_replace('a', 'b', $s, $count);
23+
}
24+
25+
/**
26+
* @phpstan-pure
27+
*/
28+
function purePregMatchWithoutMatches(string $s): int
29+
{
30+
// The by-ref $matches is omitted, so preg_match() is pure.
31+
return (int) preg_match('/a/', $s);
32+
}
33+
34+
/**
35+
* @phpstan-pure
36+
*/
37+
function purePregMatchWithMatches(string $s): int
38+
{
39+
// The by-ref $matches is passed, so preg_match() is possibly impure.
40+
return (int) preg_match('/a/', $s, $matches);
41+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PureUnlessParameterPassedFunction;
4+
5+
/**
6+
* @param-out int $count
7+
* @pure-unless-parameter-passed $count
8+
*/
9+
function myReplace(string $subject, int &$count = 0): string
10+
{
11+
$count = 1;
12+
13+
return $subject;
14+
}
15+
16+
/**
17+
* @param-out int $count
18+
* @phpstan-pure-unless-parameter-passed $count
19+
*/
20+
function myReplacePhpstanAlias(string $subject, int &$count = 0): string
21+
{
22+
$count = 1;
23+
24+
return $subject;
25+
}
26+
27+
/**
28+
* @phpstan-pure
29+
*/
30+
function pureNotPassingByRef(string $s): string
31+
{
32+
// $count is omitted, so myReplace() stays pure.
33+
return myReplace($s);
34+
}
35+
36+
/**
37+
* @phpstan-pure
38+
*/
39+
function purePassingByRef(string $s): string
40+
{
41+
// $count is passed, so myReplace() is possibly impure.
42+
$count = 0;
43+
44+
return myReplace($s, $count);
45+
}
46+
47+
/**
48+
* @phpstan-pure
49+
*/
50+
function pureNotPassingByRefAlias(string $s): string
51+
{
52+
return myReplacePhpstanAlias($s);
53+
}
54+
55+
/**
56+
* @phpstan-pure
57+
*/
58+
function purePassingByRefAlias(string $s): string
59+
{
60+
$count = 0;
61+
62+
return myReplacePhpstanAlias($s, $count);
63+
}

0 commit comments

Comments
 (0)