|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* PHPCompatibility, an external standard for PHP_CodeSniffer. |
|
4
|
|
|
* |
|
5
|
|
|
* @package PHPCompatibility |
|
6
|
|
|
* @copyright 2012-2019 PHPCompatibility Contributors |
|
7
|
|
|
* @license https://opensource.org/licenses/LGPL-3.0 LGPL3 |
|
8
|
|
|
* @link https://github.com/PHPCompatibility/PHPCompatibility |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace PHPCompatibility\Sniffs\MethodUse; |
|
12
|
|
|
|
|
13
|
|
|
use PHPCompatibility\Sniff; |
|
14
|
|
|
use PHP_CodeSniffer_File as File; |
|
15
|
|
|
use PHP_CodeSniffer_Tokens as Tokens; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* As of PHP 5.3, the __toString() magic method can no longer be passed arguments. |
|
19
|
|
|
* |
|
20
|
|
|
* Sister-sniff to PHPCompatibility.FunctionDeclarations.ForbiddenToStringParameters. |
|
21
|
|
|
* |
|
22
|
|
|
* @link https://www.php.net/manual/en/migration53.incompatible.php |
|
23
|
|
|
* |
|
24
|
|
|
* PHP version 5.3 |
|
25
|
|
|
* |
|
26
|
|
|
* @since 9.2.0 |
|
27
|
|
|
*/ |
|
28
|
|
|
class ForbiddenToStringParametersSniff extends Sniff |
|
29
|
|
|
{ |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Returns an array of tokens this test wants to listen for. |
|
33
|
|
|
* |
|
34
|
|
|
* @since 9.2.0 |
|
35
|
|
|
* |
|
36
|
|
|
* @return array |
|
37
|
|
|
*/ |
|
38
|
|
|
public function register() |
|
39
|
|
|
{ |
|
40
|
|
|
return array( |
|
41
|
|
|
\T_DOUBLE_COLON, |
|
42
|
|
|
\T_OBJECT_OPERATOR, |
|
43
|
|
|
); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Processes this test, when one of its tokens is encountered. |
|
48
|
|
|
* |
|
49
|
|
|
* @since 9.2.0 |
|
50
|
|
|
* |
|
51
|
|
|
* @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
52
|
|
|
* @param int $stackPtr The position of the current token |
|
53
|
|
|
* in the stack passed in $tokens. |
|
54
|
|
|
* |
|
55
|
|
|
* @return void |
|
56
|
|
|
*/ |
|
57
|
|
|
public function process(File $phpcsFile, $stackPtr) |
|
58
|
|
|
{ |
|
59
|
|
|
if ($this->supportsAbove('5.3') === false) { |
|
60
|
|
|
return; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$tokens = $phpcsFile->getTokens(); |
|
64
|
|
|
|
|
65
|
|
|
$nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true); |
|
66
|
|
|
if ($nextNonEmpty === false || $tokens[$nextNonEmpty]['code'] !== \T_STRING) { |
|
67
|
|
|
/* |
|
68
|
|
|
* Not a method call. |
|
69
|
|
|
* |
|
70
|
|
|
* Note: This disregards method calls with the method name in a variable, like: |
|
71
|
|
|
* $method = '__toString'; |
|
72
|
|
|
* $obj->$method(); |
|
73
|
|
|
* However, that would be very hard to examine reliably anyway. |
|
74
|
|
|
*/ |
|
75
|
|
|
return; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
if (strtolower($tokens[$nextNonEmpty]['content']) !== '__tostring') { |
|
79
|
|
|
// Not a call to the __clone() method. |
|
80
|
|
|
return; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$openParens = $phpcsFile->findNext(Tokens::$emptyTokens, ($nextNonEmpty + 1), null, true); |
|
84
|
|
|
if ($openParens === false || $tokens[$openParens]['code'] !== \T_OPEN_PARENTHESIS) { |
|
85
|
|
|
// Not a method call. |
|
86
|
|
|
return; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$closeParens = $phpcsFile->findNext(Tokens::$emptyTokens, ($openParens + 1), null, true); |
|
90
|
|
|
if ($closeParens === false || $tokens[$closeParens]['code'] === \T_CLOSE_PARENTHESIS) { |
|
91
|
|
|
// Not a method call. |
|
92
|
|
|
return; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
// If we're still here, then this is a call to the __toString() magic method passing parameters. |
|
96
|
|
|
$phpcsFile->addError( |
|
97
|
|
|
'The __toString() magic method will no longer accept passed arguments since PHP 5.3', |
|
98
|
|
|
$stackPtr, |
|
99
|
|
|
'Passed' |
|
100
|
|
|
); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|