Thanks to everyone who commented. Please keep them coming.
In the meantime, I've pushed my first attempt at a sniff to detect files that both declare symbols and cause side effects. It's in the Github repo.
I'd appreciate it if people could test on their code to see if there are any other code blocks that need considering. The best way to do this is to clone the git repo and run PHPCS directly from there:
php scripts/phpcs --standard=PSR1 --sniffs=PSR1.Files.SideEffects /path/to/code
The commands above clone the repo and run the PSR1 standard with just this new sniff so it is easy to identify the files causing this specific warning. If you'd like to also do a test run of the (now complete) PSR1 or PSR2 standards, you can use the commands:
php scripts/phpcs --standard=PSR1 /path/to/code
php scripts/phpcs --standard=PSR2 /path/to/code
Just as an example of what to expect, if you run it on the sample code in the PSR-1 standard:
1: <?php
2: // side effect: change ini settings
3: ini_set('error_reporting', E_ALL);
4:
5: // side effect: loads a file
6: include "file.php";
7:
8: // side effect: generates output
9: echo "<html>\n";
10:
11: // declaration
12: function foo()
13: {
14: // function body
15: }
You'll get:
--------------------------------------------------------------------------------
FOUND 0 ERROR(S) AND 1 WARNING(S) AFFECTING 1 LINE(S)
--------------------------------------------------------------------------------
1 | WARNING | A file should declare new symbols (classes, functions,
| | constants, etc.) and cause no other side effects, or it should
| | execute logic with side effects, but should not do both. The
| | first symbol is defined on line 12 and the first side effect is
| | on line 3.
--------------------------------------------------------------------------------
If you're reporting issues, it would be great if you can also include the message you got so I can see what lines PHPCS thought the symbol and side effect were on.
All testing and feedback is greatly appreciated.