1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Token scope test file |
4
|
|
|
* |
5
|
|
|
* @package PHPCompatibility |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Token scope function tests |
11
|
|
|
* |
12
|
|
|
* @uses BaseSniffTest |
13
|
|
|
* @package PHPCompatibility |
14
|
|
|
* @author Juliette Reinders Folmer <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class TokenScopeTest extends BaseAbstractClassMethodTest |
|
|
|
|
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
public $filename = 'sniff-examples/utility-functions/token_has_scope.php'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* testTokenHasScope |
23
|
|
|
* |
24
|
|
|
* @group utilityFunctions |
25
|
|
|
* |
26
|
|
|
* @dataProvider dataTokenHasScope |
27
|
|
|
* |
28
|
|
|
* @param int $stackPtr Stack pointer for an arbitrary token in the test file. |
29
|
|
|
* @param string $expected The expected boolean return value. |
30
|
|
|
*/ |
31
|
|
|
public function testTokenHasScope($stackPtr, $expected, $validTokens = null) |
32
|
|
|
{ |
33
|
|
|
$result = $this->helperClass->tokenHasScope($this->_phpcsFile, $stackPtr, $validTokens); |
34
|
|
|
$this->assertSame($expected, $result); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* dataTokenHasScope |
39
|
|
|
* |
40
|
|
|
* @see testTokenHasScope() |
41
|
|
|
* |
42
|
|
|
* @return array |
43
|
|
|
*/ |
44
|
|
|
public function dataTokenHasScope() |
45
|
|
|
{ |
46
|
|
|
return array( |
47
|
|
|
// No scope. |
48
|
|
|
array(2, false), // $var |
49
|
|
|
|
50
|
|
|
// Various scopes. |
51
|
|
|
array(23, true), // echo within if |
52
|
|
|
array(23, true, array( T_IF ) ), // echo within if |
53
|
|
|
array(23, false, array( T_SWITCH ) ), // echo within if |
54
|
|
|
|
55
|
|
|
array(45, true), // echo within else-if |
|
|
|
|
56
|
|
|
array(45, true, array( T_ELSEIF ) ), // echo within else-if |
|
|
|
|
57
|
|
|
array(45, false, array( T_IF ) ), // echo within else-if |
|
|
|
|
58
|
|
|
|
59
|
|
|
array(57, true), // echo within else |
60
|
|
|
array(86, true), // echo within for |
61
|
|
|
array(107, true), // echo within foreach |
62
|
|
|
|
63
|
|
|
array(123, true), // case within switch |
64
|
|
|
array(123, true, array(T_SWITCH)), // case within switch |
65
|
|
|
array(123, false, array(T_CASE)), // case within switch |
66
|
|
|
|
67
|
|
|
array(129, true), // echo within case within switch |
68
|
|
|
array(129, true, array(T_SWITCH)), // echo within case within switch |
69
|
|
|
array(129, true, array(T_CASE)), // echo within case within switch |
70
|
|
|
array(129, true, array(T_SWITCH, T_CASE)), // echo within case within switch |
71
|
|
|
array(129, true, array(T_SWITCH, T_IF)), // echo within case within switch |
72
|
|
|
array(129, false, array(T_ELSEIF, T_IF)), // echo within case within switch |
73
|
|
|
|
74
|
|
|
array(139, true), // default within switch |
75
|
|
|
array(143, true), // echo within default within switch |
76
|
|
|
|
77
|
|
|
array(164, true), // echo within function |
78
|
|
|
array(164, true, array(T_FUNCTION)), // echo within function |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* testInClassScope |
84
|
|
|
* |
85
|
|
|
* @group utilityFunctions |
86
|
|
|
* |
87
|
|
|
* @dataProvider dataInClassScope |
88
|
|
|
* |
89
|
|
|
* @param int $stackPtr Stack pointer for an arbitrary token in the test file. |
90
|
|
|
* @param string $expected The expected boolean return value. |
91
|
|
|
*/ |
92
|
|
|
public function testInClassScope($stackPtr, $expected) |
93
|
|
|
{ |
94
|
|
|
$result = $this->helperClass->inClassScope($this->_phpcsFile, $stackPtr); |
95
|
|
|
$this->assertSame($expected, $result); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* dataInClassScope |
100
|
|
|
* |
101
|
|
|
* @see testInClassScope() |
102
|
|
|
* |
103
|
|
|
* @return array |
104
|
|
|
*/ |
105
|
|
|
public function dataInClassScope() |
106
|
|
|
{ |
107
|
|
|
return array( |
108
|
|
|
array(181, true), // $property |
109
|
|
|
array(185, true), // function |
110
|
|
|
array(202, false), // function |
111
|
|
|
array(220, true), // function |
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
} |
116
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.