1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Required Optional Functions Parameter Sniff test file |
4
|
|
|
* |
5
|
|
|
* @package PHPCompatibility |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Required Optional Parameter Sniff test file |
11
|
|
|
* |
12
|
|
|
* @uses BaseSniffTest |
13
|
|
|
* @package PHPCompatibility |
14
|
|
|
* @author Wim Godden <[email protected]> |
15
|
|
|
*/ |
16
|
|
View Code Duplication |
class RequiredOptionalFunctionParameterSniffTest extends BaseSniffTest |
|
|
|
|
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
const TEST_FILE = 'sniff-examples/required_optional_function_parameters.php'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* testRequiredOptionalParameter |
23
|
|
|
* |
24
|
|
|
* @dataProvider dataRequiredOptionalParameter |
25
|
|
|
* |
26
|
|
|
* @param string $functionName Function name. |
27
|
|
|
* @param string $parameterName Parameter name. |
28
|
|
|
* @param string $requiredUpTo The last PHP version in which the parameter was still required. |
29
|
|
|
* @param array $lines The line numbers in the test file which apply to this class. |
30
|
|
|
* @param string $okVersion A PHP version in which to test for no violation. |
31
|
|
|
* |
32
|
|
|
* @return void |
33
|
|
|
*/ |
34
|
|
|
public function testRequiredOptionalParameter($functionName, $parameterName, $requiredUpTo, $lines, $okVersion) |
35
|
|
|
{ |
36
|
|
|
$file = $this->sniffFile(self::TEST_FILE, $requiredUpTo); |
37
|
|
|
foreach ($lines as $line) { |
38
|
|
|
$this->assertError($file, $line, "The \"{$parameterName}\" parameter for function {$functionName} is missing, but was required for PHP version {$requiredUpTo} and lower"); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$file = $this->sniffFile(self::TEST_FILE, $okVersion); |
42
|
|
|
foreach ($lines as $line) { |
43
|
|
|
$this->assertNoViolation($file, $line); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Data provider. |
49
|
|
|
* |
50
|
|
|
* @see testRequiredOptionalParameter() |
51
|
|
|
* |
52
|
|
|
* @return array |
53
|
|
|
*/ |
54
|
|
|
public function dataRequiredOptionalParameter() |
55
|
|
|
{ |
56
|
|
|
return array( |
57
|
|
|
array('preg_match_all', 'matches', '5.3', array(8), '5.4'), |
58
|
|
|
array('stream_socket_enable_crypto', 'crypto_type', '5.5', array(9), '5.6'), |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* testValidParameter |
65
|
|
|
* |
66
|
|
|
* @dataProvider dataValidParameter |
67
|
|
|
* |
68
|
|
|
* @param int $line The line number. |
69
|
|
|
* |
70
|
|
|
* @return void |
71
|
|
|
*/ |
72
|
|
|
public function testValidParameter($line) |
73
|
|
|
{ |
74
|
|
|
$file = $this->sniffFile(self::TEST_FILE, '7.0'); |
75
|
|
|
$this->assertNoViolation($file, $line); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Data provider. |
80
|
|
|
* |
81
|
|
|
* @see testValidParameter() |
82
|
|
|
* |
83
|
|
|
* @return array |
84
|
|
|
*/ |
85
|
|
|
public function dataValidParameter() |
86
|
|
|
{ |
87
|
|
|
return array( |
88
|
|
|
array(4), |
89
|
|
|
array(5), |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.