|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* PHPCompatibility_Sniffs_PHP_DeprecatedPHP4StyleConstructorsSniff. |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 7.0 |
|
6
|
|
|
* |
|
7
|
|
|
* @category PHP |
|
8
|
|
|
* @package PHPCompatibility |
|
9
|
|
|
* @author Koen Eelen <[email protected]> |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* PHPCompatibility_Sniffs_PHP_DeprecatedPHP4StyleConstructorsSniff. |
|
14
|
|
|
* |
|
15
|
|
|
* @category PHP |
|
16
|
|
|
* @package PHPCompatibility |
|
17
|
|
|
* @author Koen Eelen <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
class PHPCompatibility_Sniffs_PHP_DeprecatedPHP4StyleConstructorsSniff extends PHPCompatibility_Sniff { |
|
20
|
|
|
public function register() |
|
21
|
|
|
{ |
|
22
|
|
|
return array(T_CLASS); |
|
23
|
|
|
|
|
24
|
|
|
}//end register() |
|
25
|
|
|
|
|
26
|
|
|
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { |
|
27
|
|
|
$tokens = $phpcsFile->getTokens(); |
|
28
|
|
|
|
|
29
|
|
|
$class = $tokens[$stackPtr]; |
|
30
|
|
|
|
|
31
|
|
|
if(!IsSet($class['scope_closer'])) { |
|
32
|
|
|
return; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
$scopeCloser = $class['scope_closer']; |
|
36
|
|
|
|
|
37
|
|
|
//get the name of the class |
|
38
|
|
|
$classNamePos = $phpcsFile->findNext(T_STRING, $stackPtr); |
|
39
|
|
|
$className = $tokens[$classNamePos]['content']; |
|
40
|
|
|
|
|
41
|
|
|
$nextFunc = $stackPtr; |
|
42
|
|
|
$newConstructorFound = false; |
|
43
|
|
|
$oldConstructorFound = false; |
|
44
|
|
|
while (($nextFunc = $phpcsFile->findNext(T_FUNCTION, ($nextFunc + 1), $scopeCloser)) !== false) { |
|
45
|
|
|
$funcNamePos = $phpcsFile->findNext(T_STRING, $nextFunc); |
|
46
|
|
|
|
|
47
|
|
|
if ($tokens[$funcNamePos]['content'] === '__construct') { |
|
48
|
|
|
$newConstructorFound = true; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
if ($this->supportsAbove('7.0')) { |
|
52
|
|
|
if ($funcNamePos !== false && $tokens[$funcNamePos]['content'] === $className) { |
|
53
|
|
|
$oldConstructorFound = true; |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
if ($newConstructorFound === false && $oldConstructorFound === true) { |
|
59
|
|
|
$phpcsFile->addError('Deprecated PHP4 style constructor are not supported since PHP7', $funcNamePos); |
|
|
|
|
|
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: