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
|
|
|
for ($i=$stackPtr; $i < count($tokens); $i++) { |
|
|
|
|
63
|
|
|
if ($tokens[$i]['type'] === 'T_OPEN_CURLY_BRACKET') { |
64
|
|
|
$open = $i; |
65
|
|
|
} |
66
|
|
|
if ($tokens[$i]['type'] === 'T_CLOSE_CURLY_BRACKET') { |
67
|
|
|
$close = $i; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return [ |
72
|
|
|
'open' => $open, |
|
|
|
|
73
|
|
|
'close' => $close |
|
|
|
|
74
|
|
|
]; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Gets the warning message for this sniff. |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
|
|
protected function getWarningMessage(string $functionName): string |
82
|
|
|
{ |
83
|
|
|
return sprintf('Function %s use new keyword - consider to use DI.', $functionName); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
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: