Skip to content

Commit b9cbe77

Browse files
committed
Add functionIsArrayAccess helper
1 parent aa68a3d commit b9cbe77

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

Inpsyde/Helpers.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
namespace Inpsyde\InpsydeCodingStandard;
1717

18+
use PHP_CodeSniffer\Exceptions\RuntimeException as CodeSnifferRuntimeException;
1819
use PHP_CodeSniffer\Files\File;
1920

2021
/**
@@ -131,6 +132,34 @@ public static function functionIsMethod(File $file, int $position)
131132
&& $closerPosition > $position + 4; // 4 because: (){}
132133
}
133134

135+
/**
136+
* @param File $file
137+
* @param int $position
138+
* @return bool
139+
*/
140+
public static function functionIsArrayAccess(File $file, int $position)
141+
{
142+
$token = $file->getTokens()[$position] ?? null;
143+
if (!$token || $token['code'] !== T_FUNCTION) {
144+
return false;
145+
}
146+
147+
try {
148+
return in_array(
149+
$file->getDeclarationName($position),
150+
[
151+
'offsetSet',
152+
'offsetGet',
153+
'offsetUnset',
154+
'offsetExists',
155+
],
156+
true
157+
);
158+
} catch (CodeSnifferRuntimeException $exception) {
159+
return false;
160+
}
161+
}
162+
134163
/**
135164
* @param File $file
136165
* @param int $position

0 commit comments

Comments
 (0)