Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 29 additions & 9 deletions src/Node/MethodDeclaration.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,38 @@ class MethodDeclaration extends Node implements FunctionLike {
// FunctionBody
'compoundStatementOrSemicolon'
];

public function hasModifier(int $targetModifier) : bool {
if ($this->modifiers === null) {
return false;
}
foreach ($this->modifiers as $modifier) {
if ($modifier->kind === $targetModifier) {
return true;
}
}
return false;
}

public function isStatic() : bool {
if ($this->modifiers === null) {
return false;
}
foreach ($this->modifiers as $modifier) {
if ($modifier->kind === TokenKind::StaticKeyword) {
return true;
}
}
return false;
return $this->hasModifier(TokenKind::StaticKeyword);
}

public function isPublic() : bool {
return $this->hasModifier(TokenKind::PublicKeyword);
Copy link
Contributor

@TysonAndre TysonAndre Jan 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

class X { function test() {}} still has an implicitly public method X::test(), but this returns false - I can see this being misunderstood in applications using this helper, for that edge case.

This is low priority for me, though

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that's a good point. Could change isPublic to just check !isPrivate but it might be better to just expose hasModifier, I think the goal is to expose the AST but not necessarily "interpret" it...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be useful to have a link to projects using tolerant-php-parser in the documentation, as examples for people writing their own applications/libraries.

E.g. there's https://github.com/tysonandre/tolerant-php-parser-to-php-ast (part of Phan), https://github.com/felixfbecker/php-language-server , etc.

The first one is notable in trying to convert all of the information provided by various node types.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made that change and just left hasModifier, FYI @inxilpro

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could also see changing these to hasPublicModifier()/etc to be more clear about what question it's answering.

}

public function isProtected() : bool {
return $this->hasModifier(TokenKind::ProtectedKeyword);
}

public function isPrivate() : bool {
return $this->hasModifier(TokenKind::PrivateKeyword);
}

public function isAbstract() : bool {
return $this->hasModifier(TokenKind::AbstractKeyword);
}

public function getName() {
return $this->name->getText($this->getFileContents());
Expand Down