|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace BestIt\CodeSniffer\Helper; |
|
6
|
|
|
|
|
7
|
|
|
use PHP_CodeSniffer\Files\File; |
|
8
|
|
|
use function substr; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class PropertyHelper |
|
12
|
|
|
* |
|
13
|
|
|
* @author Nick Lubisch <[email protected]> |
|
14
|
|
|
* @package BestIt\CodeSniffer\Helper |
|
15
|
|
|
*/ |
|
16
|
|
|
class PropertyHelper |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* The wrapped PHP_CodeSniffer_File |
|
20
|
|
|
* |
|
21
|
|
|
* @var File |
|
22
|
|
|
*/ |
|
23
|
|
|
private File $file; |
|
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* PropertyHelper constructor. |
|
27
|
|
|
* |
|
28
|
|
|
* @param File $file The wrapped PHP_CodeSniffer_File |
|
29
|
121 |
|
*/ |
|
30
|
|
|
public function __construct(File $file) |
|
31
|
121 |
|
{ |
|
32
|
121 |
|
$this->file = $file; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Returns the names of the class' properties. |
|
37
|
|
|
* |
|
38
|
|
|
* @param array $classToken The token array for the class like structure. |
|
39
|
|
|
* @param File|null $file The used file. If not given then the file from the construct is used. |
|
40
|
|
|
* |
|
41
|
26 |
|
* @return array The names of the properties. |
|
42
|
|
|
*/ |
|
43
|
26 |
|
public function getProperties(array $classToken, ?File $file = null): array |
|
44
|
|
|
{ |
|
45
|
26 |
|
if (!$file) { |
|
46
|
26 |
|
$file = $this->file; |
|
47
|
26 |
|
} |
|
48
|
26 |
|
|
|
49
|
26 |
|
$properties = []; |
|
50
|
|
|
$startPos = $classToken['scope_opener'] ?? 0; |
|
51
|
26 |
|
$tokens = $file->getTokens(); |
|
52
|
26 |
|
|
|
53
|
|
|
while (($propertyPos = $file->findNext([T_VARIABLE], $startPos, $classToken['scope_closer'])) > 0) { |
|
54
|
26 |
|
if ($this->isProperty($propertyPos)) { |
|
55
|
26 |
|
$properties[] = substr($tokens[$propertyPos]['content'], 1); |
|
56
|
|
|
} |
|
57
|
26 |
|
|
|
58
|
26 |
|
$startPos = $propertyPos + 1; |
|
59
|
26 |
|
} |
|
60
|
26 |
|
|
|
61
|
|
|
return $properties; |
|
62
|
26 |
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Determines if the given variable is a property of a class. |
|
66
|
|
|
* |
|
67
|
|
|
* @param int $variablePtr Pointer to the current variable |
|
68
|
|
|
* |
|
69
|
|
|
* @return bool Indicator if the current T_VARIABLE is a property of a class |
|
70
|
|
|
*/ |
|
71
|
|
|
public function isProperty(int $variablePtr): bool |
|
72
|
|
|
{ |
|
73
|
|
|
$tokens = $this->file->getTokens(); |
|
74
|
|
|
|
|
75
|
|
|
$propertyPointer = $this->file->findPrevious( |
|
76
|
|
|
[T_STATIC, T_WHITESPACE, T_COMMENT], |
|
77
|
|
|
$variablePtr - 1, |
|
78
|
|
|
null, |
|
79
|
|
|
true |
|
80
|
|
|
); |
|
81
|
|
|
$propertyToken = $tokens[$propertyPointer]; |
|
82
|
|
|
$propertyCode = $propertyToken['code']; |
|
83
|
|
|
|
|
84
|
|
|
return in_array( |
|
85
|
|
|
$propertyCode, |
|
86
|
|
|
[ |
|
87
|
|
|
T_PRIVATE, |
|
88
|
|
|
T_PROTECTED, |
|
89
|
|
|
T_PUBLIC, |
|
90
|
|
|
T_VAR |
|
91
|
|
|
], |
|
92
|
|
|
true |
|
93
|
|
|
); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|