function File::getMethodProperties
Returns the visibility and implementation properties of a method.
The format of the return value is: <code> array( 'scope' => string, // Public, private, or protected 'scope_specified' => boolean, // TRUE if the scope keyword was found. 'return_type' => string, // The return type of the method. 'return_type_token' => integer|false, // The stack pointer to the start of the return type // or FALSE if there is no return type. 'return_type_end_token' => integer|false, // The stack pointer to the end of the return type // or FALSE if there is no return type. 'nullable_return_type' => boolean, // TRUE if the return type is preceded by the // nullability operator. 'is_abstract' => boolean, // TRUE if the abstract keyword was found. 'is_final' => boolean, // TRUE if the final keyword was found. 'is_static' => boolean, // TRUE if the static keyword was found. 'has_body' => boolean, // TRUE if the method has a body ); </code>
Parameters
int $stackPtr The position in the stack of the function token to: acquire the properties for.
Return value
array
Throws
\PHP_CodeSniffer\Exceptions\RuntimeException If the specified position is not a T_FUNCTION, T_CLOSURE, or T_FN token.
File
-
vendor/
squizlabs/ php_codesniffer/ src/ Files/ File.php, line 1667
Class
Namespace
PHP_CodeSniffer\FilesCode
public function getMethodProperties($stackPtr) {
if ($this->tokens[$stackPtr]['code'] !== T_FUNCTION && $this->tokens[$stackPtr]['code'] !== T_CLOSURE && $this->tokens[$stackPtr]['code'] !== T_FN) {
throw new RuntimeException('$stackPtr must be of type T_FUNCTION or T_CLOSURE or T_FN');
}
if ($this->tokens[$stackPtr]['code'] === T_FUNCTION) {
$valid = [
T_PUBLIC => T_PUBLIC,
T_PRIVATE => T_PRIVATE,
T_PROTECTED => T_PROTECTED,
T_STATIC => T_STATIC,
T_FINAL => T_FINAL,
T_ABSTRACT => T_ABSTRACT,
T_WHITESPACE => T_WHITESPACE,
T_COMMENT => T_COMMENT,
T_DOC_COMMENT => T_DOC_COMMENT,
];
}
else {
$valid = [
T_STATIC => T_STATIC,
T_WHITESPACE => T_WHITESPACE,
T_COMMENT => T_COMMENT,
T_DOC_COMMENT => T_DOC_COMMENT,
];
}
$scope = 'public';
$scopeSpecified = false;
$isAbstract = false;
$isFinal = false;
$isStatic = false;
for ($i = $stackPtr - 1; $i > 0; $i--) {
if (isset($valid[$this->tokens[$i]['code']]) === false) {
break;
}
switch ($this->tokens[$i]['code']) {
case T_PUBLIC:
$scope = 'public';
$scopeSpecified = true;
break;
case T_PRIVATE:
$scope = 'private';
$scopeSpecified = true;
break;
case T_PROTECTED:
$scope = 'protected';
$scopeSpecified = true;
break;
case T_ABSTRACT:
$isAbstract = true;
break;
case T_FINAL:
$isFinal = true;
break;
case T_STATIC:
$isStatic = true;
break;
}
//end switch
}
//end for
$returnType = '';
$returnTypeToken = false;
$returnTypeEndToken = false;
$nullableReturnType = false;
$hasBody = true;
if (isset($this->tokens[$stackPtr]['parenthesis_closer']) === true) {
$scopeOpener = null;
if (isset($this->tokens[$stackPtr]['scope_opener']) === true) {
$scopeOpener = $this->tokens[$stackPtr]['scope_opener'];
}
$valid = [
T_STRING => T_STRING,
T_CALLABLE => T_CALLABLE,
T_SELF => T_SELF,
T_PARENT => T_PARENT,
T_STATIC => T_STATIC,
T_FALSE => T_FALSE,
T_TRUE => T_TRUE,
T_NULL => T_NULL,
T_NAMESPACE => T_NAMESPACE,
T_NS_SEPARATOR => T_NS_SEPARATOR,
T_TYPE_UNION => T_TYPE_UNION,
T_TYPE_INTERSECTION => T_TYPE_INTERSECTION,
T_TYPE_OPEN_PARENTHESIS => T_TYPE_OPEN_PARENTHESIS,
T_TYPE_CLOSE_PARENTHESIS => T_TYPE_CLOSE_PARENTHESIS,
];
for ($i = $this->tokens[$stackPtr]['parenthesis_closer']; $i < $this->numTokens; $i++) {
if ($scopeOpener === null && $this->tokens[$i]['code'] === T_SEMICOLON || $scopeOpener !== null && $i === $scopeOpener) {
// End of function definition.
break;
}
if ($this->tokens[$i]['code'] === T_USE) {
// Skip over closure use statements.
for ($j = $i + 1; $j < $this->numTokens && isset(Tokens::$emptyTokens[$this->tokens[$j]['code']]) === true; $j++) {
}
if ($this->tokens[$j]['code'] === T_OPEN_PARENTHESIS) {
if (isset($this->tokens[$j]['parenthesis_closer']) === false) {
// Live coding/parse error, stop parsing.
break;
}
$i = $this->tokens[$j]['parenthesis_closer'];
continue;
}
}
if ($this->tokens[$i]['code'] === T_NULLABLE) {
$nullableReturnType = true;
}
if (isset($valid[$this->tokens[$i]['code']]) === true) {
if ($returnTypeToken === false) {
$returnTypeToken = $i;
}
$returnType .= $this->tokens[$i]['content'];
$returnTypeEndToken = $i;
}
}
//end for
if ($this->tokens[$stackPtr]['code'] === T_FN) {
$bodyToken = T_FN_ARROW;
}
else {
$bodyToken = T_OPEN_CURLY_BRACKET;
}
$end = $this->findNext([
$bodyToken,
T_SEMICOLON,
], $this->tokens[$stackPtr]['parenthesis_closer']);
$hasBody = $this->tokens[$end]['code'] === $bodyToken;
}
//end if
if ($returnType !== '' && $nullableReturnType === true) {
$returnType = '?' . $returnType;
}
return [
'scope' => $scope,
'scope_specified' => $scopeSpecified,
'return_type' => $returnType,
'return_type_token' => $returnTypeToken,
'return_type_end_token' => $returnTypeEndToken,
'nullable_return_type' => $nullableReturnType,
'is_abstract' => $isAbstract,
'is_final' => $isFinal,
'is_static' => $isStatic,
'has_body' => $hasBody,
];
}