-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathStaticStrreplaceUnitTest.inc
More file actions
101 lines (81 loc) · 3.2 KB
/
StaticStrreplaceUnitTest.inc
File metadata and controls
101 lines (81 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
/*
* Not the sniff target.
*/
use str_replace;
my\ns\str_replace('foo', 'bar', 'foobar');
$this->str_replace('foo', 'bar', 'foobar');
$this?->str_replace('foo', 'bar', 'foobar');
MyClass::str_replace('foo', 'bar', 'foobar');
echo STR_REPLACE;
namespace\str_replace('foo', 'bar', 'foobar', $count);
// Looks like a function call, but is a PHP 8.0+ class instantiation via an attribute.
#[Str_Replace('foo', 'bar', 'foobar')]
function foo() {}
// Ignore PHP 5.6 argument unpacking.
str_replace('foo', 'bar', ...$params);
// Ignore PHP 8.1 first class callable.
add_filter('my_filter', str_replace(...));
// Incomplete function calls, should be ignored by the sniff.
str_replace();
str_replace('foo', 'bar');
/*
* These should all be okay.
*/
str_replace( $foo->getSearch(), 'bar', 'foobar' );
\str_replace( 'foo', $bar, 'foobar' );
str_replace( 'foo', 'bar', $foobar, );
STR_REPLACE( $foo, $bar, 'foobar' );
str_replace( 'foo', get_replacements(), $foobar, $count );
str_replace( array( 'foo', "$bar" ), $bar, 'foobar' );
str_replace( array( $foo, $bar ), array( 'bar', 'foo' ), array( 'foobar', 'foobar' ) );
\Str_Replace( [ 'foo', 'bar' ], array( $foo, SOME_CONSTANT ), 'foobar' );
str_replace( array( 'foo', "bar" ), [ 'bar', 'foo' ], $foobar, );
str_replace( [ $foo, $bar->prop ], /*comment*/ [ 'bar', 'foo' ], $foobar );
/*
* These should all be flagged with a warning.
*/
str_replace( 'foo', 'bar', 'foobar' ); // Bad.
Str_replace(
'foo', // Comment.
'bar', /* comment */
'foobar'
); // Bad.
str_replace( array( 'foo', 'bar' ), array( 'bar', 'foo' ), array( 'foobar', 'foobar' ) ); // Bad.
str_replace( 'foo', 'bar', 'foobar', $count ); // Bad.
// Handle PHP 5.4+ short array syntax correctly.
\str_replace( [ 'foo', 'bar' ], [ 'bar', 'foo' ], [ 'foobar', 'foobar' ], ); // Bad.
// Don't confuse PHP 7.1+ short list with short array.
str_replace( [ $a, $b ] = $search, [ 'bar', 'foo' ], 'foobar', ); // OK.
// Safeguard support for PHP 8.0+ named parameters.
str_replace( search: [ 'foo', 'bar' ], replace: 'baz', count: $count ); // OK, well not really, missing required $subject param, but not our concern.
str_replace( search: 'foo', replace: 'baz', subjects: [ 'foobar', 'foobar' ] ); // OK, well not really, typo in $subject param name, not our concern.
str_replace( subject: $subject, count: $count, replace: 'baz', search: 'foo', ); // OK, different parameter order.
str_replace( count: $count, subject: [ 'foobar', 'foobar' ], search: 'foo', replace: 'baz', ); // Bad with different parameter order.
str_replace( [ 'foo', 'bar' ], 'baz', count: $count, subject: 'foobar' ); // Bad, with mixed named and unnamed params.
// Make sure some variations of how plain strings can be passed are handled.
str_replace( 'fo' . 'o', 'bar', 'foobar' ); // Bad.
str_replace( array( 'foo', 'bar' ) + array( 'baz', 'fop' ), array( 'bar', 'foo' ), 'foobar' ); // Bad.
// Safeguard correct handling of PHP 5.3+ nowdocs.
str_replace(
'foo',
'bar',
<<<'EOD'
foobar
EOD
); // Bad.
// Safeguard correct handling of heredocs without interpolation.
str_replace( array(<<<EOD
foo
EOD
),
'bar',
<<<"EOD"
foobar
EOD
); // Bad.
// Ensure heredocs with interpolation are ignored.
str_replace( 'foo', 'bar', <<<EOD
Some text and $foobar
EOD
); // OK.