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
|
|
|
|
21
|
|
|
public function register() |
22
|
|
|
{ |
23
|
|
|
return array(T_CLASS); |
24
|
|
|
|
25
|
|
|
}//end register() |
26
|
|
|
|
27
|
|
|
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
28
|
|
|
{ |
29
|
|
|
if ($this->supportsAbove('7.0') === false) { |
30
|
|
|
return; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
if ($this->determineNamespace($phpcsFile, $stackPtr) !== '') { |
34
|
|
|
/* |
35
|
|
|
* Namespaced methods with the same name as the class are treated as |
36
|
|
|
* regular methods, so we can bow out if we're in a namespace. |
37
|
|
|
* |
38
|
|
|
* Note: the exception to this is PHP 5.3.0-5.3.2. This is currently |
39
|
|
|
* not dealt with. |
40
|
|
|
*/ |
41
|
|
|
return; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$tokens = $phpcsFile->getTokens(); |
45
|
|
|
|
46
|
|
|
$class = $tokens[$stackPtr]; |
47
|
|
|
|
48
|
|
|
if(!IsSet($class['scope_closer'])) { |
49
|
|
|
return; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$scopeCloser = $class['scope_closer']; |
53
|
|
|
$className = $phpcsFile->getDeclarationName($stackPtr); |
54
|
|
|
|
55
|
|
|
if (empty($className) || is_string($className) === false) { |
56
|
|
|
return; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$nextFunc = $stackPtr; |
60
|
|
|
$newConstructorFound = false; |
61
|
|
|
$oldConstructorFound = false; |
62
|
|
|
$oldConstructorPos = false; |
63
|
|
|
while (($nextFunc = $phpcsFile->findNext(T_FUNCTION, ($nextFunc + 1), $scopeCloser)) !== false) { |
64
|
|
|
$funcName = $phpcsFile->getDeclarationName($nextFunc); |
65
|
|
|
if (empty($funcName) || is_string($funcName) === false) { |
66
|
|
|
continue; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if ($funcName === '__construct') { |
70
|
|
|
$newConstructorFound = true; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if ($funcName === $className) { |
74
|
|
|
$oldConstructorFound = true; |
75
|
|
|
$oldConstructorPos = $phpcsFile->findNext(T_STRING, $nextFunc); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if ($newConstructorFound === false && $oldConstructorFound === true) { |
80
|
|
|
$phpcsFile->addError('Deprecated PHP4 style constructor are not supported since PHP7', $oldConstructorPos); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
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.