Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File::contains*() #122

Open
GaryJones opened this issue Mar 29, 2020 · 4 comments
Open

File::contains*() #122

GaryJones opened this issue Mar 29, 2020 · 4 comments

Comments

@GaryJones
Copy link
Contributor

Idea: a way to check if the current file contains a class, interface, trait, function, assignment to a global constant and perhaps other items.

These would be stepping stones to then having other code which could then analyse them - for instance:

if ( File::containsClass( 'Foo' ) && Class:containsMethod( 'Foo', 'bar' ) ) {
	$has_return = Method::containsToken( [ 'Foo', 'bar' ], T_RETURN );
}

Maybe some of these utility methods take stack pointers rather than strings for classes, methods, etc., but the idea is that those less familiar with writing standards can think in terms of the PHP structures that they are familiar with, rather than tokens - PHPCSUtils can handle that layer between PHP structures and tokens.

@jrfnl
Copy link
Member

jrfnl commented Mar 29, 2020

I think I can guess your use-case for this question: checking if functions used as filters in WP have a return statement... Correct ?

I'm not keen on those kind of methods as they would always be highly inefficient. Those kind of things would be better handled by setting up an (abstract) sniff which listens to the right tokens.

Note: and would still not handle cross-file sniffing.

@GaryJones
Copy link
Contributor Author

As highlighted by that use case, the focus of the sniff may not be just about "Does this file contain X or not?", so an abstract sniff is not suitable, but it is one of the parts of the logic needed for a deeper look at whether, in this case, "Does a function have a return?".

I'm not keen on those kind of methods as they would always be highly inefficient.

Maybe, but that's all the more reason for PHPCSUtils to find what is the most efficient way, and explain the types of guard clauses that could be used, rather than everyone struggling to come up with their own version that is even more inefficient.

@jrfnl
Copy link
Member

jrfnl commented Mar 29, 2020

I'll have a think about this. Let me sleep on it. It may be a while before I get to these.

@jrfnl
Copy link
Member

jrfnl commented Mar 29, 2020

Side-note: something like Method::containsToken( [ 'Foo', 'bar' ], T_RETURN ); would still not be reliable as the method may not contain a T_RETURN for all exit paths in the method or could contain a nested closure which has a return, but the function itself does not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants