Skip to content

Commit fee671c

Browse files
committed
Add Helpers::functionIsMethod
and rename Helpers::isProperty to Helpers::variableIsProperty
1 parent e843eb0 commit fee671c

File tree

2 files changed

+49
-6
lines changed

2 files changed

+49
-6
lines changed

Inpsyde/Helpers.php

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public static function classPropertiesTokenIndexes(
5252
$pointer = (int)$token['scope_opener'];
5353

5454
while ($pointer) {
55-
if (self::isProperty($file, $pointer)) {
55+
if (self::variableIsProperty($file, $pointer)) {
5656
$propertyList[] = $pointer;
5757
}
5858
$pointer = (int)$file->findNext(
@@ -67,14 +67,19 @@ public static function classPropertiesTokenIndexes(
6767

6868
/**
6969
* @param File $file
70-
* @param int $variablePosition
70+
* @param int $position
7171
* @return bool
7272
*/
73-
public static function isProperty(File $file, int $variablePosition): bool
73+
public static function variableIsProperty(File $file, int $position): bool
7474
{
75+
$token = $file->getTokens()[$position];
76+
if ($token['code'] !== T_VARIABLE) {
77+
return false;
78+
}
79+
7580
$propertyPointer = $file->findPrevious(
7681
[T_STATIC, T_WHITESPACE, T_COMMENT],
77-
$variablePosition - 1,
82+
$position - 1,
7883
null,
7984
true
8085
);
@@ -88,6 +93,44 @@ public static function isProperty(File $file, int $variablePosition): bool
8893
);
8994
}
9095

96+
/**
97+
* @param File $file
98+
* @param int $position
99+
* @return bool
100+
*/
101+
public static function functionIsMethod(File $file, int $position)
102+
{
103+
$tokens = $file->getTokens();
104+
$functionToken = $tokens[$position];
105+
if ($functionToken['code'] !== T_FUNCTION) {
106+
return false;
107+
}
108+
109+
$classPointer = $file->findPrevious(
110+
[T_CLASS, T_INTERFACE, T_TRAIT],
111+
$position - 1
112+
);
113+
114+
if (!$classPointer) {
115+
return false;
116+
}
117+
118+
$classToken = $tokens[$classPointer];
119+
if ($classToken['level'] !== $functionToken['level'] - 1) {
120+
return false;
121+
}
122+
123+
$openerPosition = $classToken['scope_opener'] ?? -1;
124+
$closerPosition = $classToken['scope_closer'] ?? -1;
125+
126+
return
127+
$openerPosition > 0
128+
&& $closerPosition > 0
129+
&& $closerPosition > ($openerPosition + 1)
130+
&& $openerPosition < ($position - 1)
131+
&& $closerPosition > $position + 4; // 4 because: (){}
132+
}
133+
91134
/**
92135
* @param File $file
93136
* @param int $position
@@ -102,7 +145,7 @@ public static function tokenTypeName(File $file, int $position): string
102145
}
103146

104147
if ($token['code'] === T_VARIABLE) {
105-
if (self::isProperty($file, $position)) {
148+
if (self::variableIsProperty($file, $position)) {
106149
return 'Property';
107150
}
108151

Inpsyde/Sniffs/CodeQuality/ForbiddenPublicPropertySniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function register()
4040
*/
4141
public function process(File $file, $position)
4242
{
43-
if (!Helpers::isProperty($file, $position)) {
43+
if (!Helpers::variableIsProperty($file, $position)) {
4444
return;
4545
}
4646

0 commit comments

Comments
 (0)