|
1
|
|
|
<?php declare(strict_types = 1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Codor\Sniffs\Classes; |
|
4
|
|
|
|
|
5
|
|
|
use PHP_CodeSniffer\Sniffs\Sniff as PHP_CodeSniffer_Sniff; |
|
6
|
|
|
use PHP_CodeSniffer\Files\File as PHP_CodeSniffer_File; |
|
7
|
|
|
|
|
8
|
|
|
class NewInstanceSniff implements PHP_CodeSniffer_Sniff |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Returns the token types that this sniff is interested in. |
|
12
|
|
|
* @return array |
|
13
|
|
|
*/ |
|
14
|
|
|
public function register(): array |
|
15
|
|
|
{ |
|
16
|
|
|
return [T_FUNCTION]; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Processes the tokens that this sniff is interested in. |
|
21
|
|
|
* |
|
22
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file where the token was found. |
|
23
|
|
|
* @param integer $stackPtr The position in the stack where |
|
24
|
|
|
* the token was found. |
|
25
|
|
|
* @return void |
|
26
|
|
|
*/ |
|
27
|
|
|
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
28
|
|
|
{ |
|
29
|
|
|
$tokens = $phpcsFile->getTokens(); |
|
30
|
|
|
$functionName = $tokens[$stackPtr + 2]['content']; |
|
31
|
|
|
|
|
32
|
|
|
$scopes = $this->getBracketsIndex($tokens, $stackPtr); |
|
33
|
|
|
if (true === empty($scopes['open']) || true === empty($scopes['close'])) { |
|
34
|
|
|
return; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
for ($index=$scopes['open']; $index <= $scopes['close']; $index++) { |
|
38
|
|
|
if ($tokens[$index]['type'] === 'T_NEW') { |
|
39
|
|
|
$phpcsFile->addWarning($this->getWarningMessage($functionName), $tokens[$index]['column'], __CLASS__); |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
private function getBracketsIndex(array $tokens, int $stackPtr) : array |
|
45
|
|
|
{ |
|
46
|
|
|
$token = $tokens[$stackPtr]; |
|
47
|
|
|
$open = $token['scope_opener'] ?? null; |
|
48
|
|
|
$close = $token['scope_closer'] ?? null; |
|
49
|
|
|
|
|
50
|
|
|
if (true === empty($open)) { |
|
51
|
|
|
return $this->searchBrackets($tokens, $stackPtr); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return [ |
|
55
|
|
|
'open' => $open, |
|
56
|
|
|
'close' => $close |
|
57
|
|
|
]; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
private function searchBrackets(array $tokens, int $stackPtr) : array |
|
61
|
|
|
{ |
|
62
|
|
|
$open = $close = null; |
|
63
|
|
|
for ($i=$stackPtr; $i < count($tokens); $i++) { |
|
|
|
|
|
|
64
|
|
|
if ($tokens[$i]['type'] === 'T_OPEN_CURLY_BRACKET') { |
|
65
|
|
|
$open = $i; |
|
66
|
|
|
} |
|
67
|
|
|
if ($tokens[$i]['type'] === 'T_CLOSE_CURLY_BRACKET') { |
|
68
|
|
|
$close = $i; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return [ |
|
73
|
|
|
'open' => $open, |
|
74
|
|
|
'close' => $close |
|
75
|
|
|
]; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Gets the warning message for this sniff. |
|
80
|
|
|
* @return string |
|
81
|
|
|
*/ |
|
82
|
|
|
protected function getWarningMessage(string $functionName): string |
|
83
|
|
|
{ |
|
84
|
|
|
return sprintf('Function %s use new keyword - consider to use DI.', $functionName); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: