squizlabs / PHP_CodeSniffer

PHP_CodeSniffer tokenizes PHP files and detects violations of a defined set of coding standards.
BSD 3-Clause "New" or "Revised" License
10.67k stars 1.48k forks source link

[Question] Manipulate the file order #1100

Closed grappler closed 7 years ago

grappler commented 8 years ago

I am working on some sniffs for testing WordPress themes.

I would like to build some sniffs that check all the files and returns a notice if a function does not exist across the code. It does not make sense to have a notice for every file.

In some cases it would make sense to add the warnings in the end to a specific file.

I could not find a way to manipulate the file order to have that specific file be tested last.


In another case it would be helpful to get data from code comments from a specific file and use that information in the rest of the sniff to test. In this case the specific file would need to tested first.

Do you have any ideas how this could be accomplished?

gsherwood commented 8 years ago

Your first example doesn't actually need the file order specified. You instead want to check every file in a project, record what functions are used, record what functions are defined, then error for every function that is used without being defined.

PHP_CodeSniffer is not well-suited to tasks like this. It acts upon a single file at a time. While you can record all the information you want and store it in a sniff member var, you wont be able to go back and actually put errors into files you've already processed.

You'll also not be able to properly determine what function is being reference when objects are used. For example, $asset->getId() and $page->getId() might reference two different methods in two different classes, but you can't tell what class the objects are in PHPCS because it isn't trying to track that through the code. Plus, that's really hard to do with static analysis because of all the ways PHP allows objects to be created.

In your second example, you could just specify the file to be checked first, on the command line: phpcs /path/to/project/file.php /path/to/project