1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* \PHPCompatibility\Sniffs\FunctionNameRestrictions\RemovedNamespacedAssertSniff. |
4
|
|
|
* |
5
|
|
|
* PHP version 7.3 |
6
|
|
|
* |
7
|
|
|
* @category PHP |
8
|
|
|
* @package PHPCompatibility |
9
|
|
|
* @author Juliette Reinders Folmer <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace PHPCompatibility\Sniffs\FunctionNameRestrictions; |
13
|
|
|
|
14
|
|
|
use PHPCompatibility\Sniff; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Removed Namespaced Assert. |
18
|
|
|
* |
19
|
|
|
* As of PHP 7.3, a compile-time deprecation warning will be thrown when a function |
20
|
|
|
* called `assert()` is declared. In PHP 8 this will become a compile-error. |
21
|
|
|
* |
22
|
|
|
* Methods are unaffected. |
23
|
|
|
* Global, non-namespaced, assert() function declarations were always a fatal |
24
|
|
|
* "function already declared" error, so not the concern of this sniff. |
25
|
|
|
* |
26
|
|
|
* @category PHP |
27
|
|
|
* @package PHPCompatibility |
28
|
|
|
* @author Juliette Reinders Folmer <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
class RemovedNamespacedAssertSniff extends Sniff |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* Scopes in which an `assert` function can be declared without issue. |
34
|
|
|
* |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
private $scopes = array( |
38
|
|
|
T_CLASS, |
39
|
|
|
T_INTERFACE, |
40
|
|
|
T_TRAIT, |
41
|
|
|
T_CLOSURE, |
42
|
|
|
); |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Returns an array of tokens this test wants to listen for. |
46
|
|
|
* |
47
|
|
|
* @return array |
48
|
|
|
*/ |
49
|
|
|
public function register() |
50
|
|
|
{ |
51
|
|
|
// Enrich the scopes list. |
52
|
|
|
if (defined('T_ANON_CLASS')) { |
53
|
|
|
$this->scopes[] = T_ANON_CLASS; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return array(T_FUNCTION); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Processes this test, when one of its tokens is encountered. |
61
|
|
|
* |
62
|
|
|
* @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
63
|
|
|
* @param int $stackPtr The position of the current token in the |
64
|
|
|
* stack passed in $tokens. |
65
|
|
|
* |
66
|
|
|
* @return void |
67
|
|
|
*/ |
68
|
|
View Code Duplication |
public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
if ($this->supportsAbove('7.3') === false) { |
71
|
|
|
return; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$funcName = $phpcsFile->getDeclarationName($stackPtr); |
75
|
|
|
|
76
|
|
|
if (strtolower($funcName) !== 'assert') { |
77
|
|
|
return; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if ($phpcsFile->hasCondition($stackPtr, $this->scopes) === true) { |
81
|
|
|
return; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
if ($this->determineNamespace($phpcsFile, $stackPtr) === '') { |
85
|
|
|
// Not a namespaced function declaration. Parse error, but not our concern. |
86
|
|
|
return; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$phpcsFile->addWarning('Declaring a free-standing function called assert() is deprecated since PHP 7.3.', $stackPtr, 'Found'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
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.