php-assumptions
php-assumptions copied to clipboard
Allow assertions for early exit strategies
This would be reported:
<?php
if ( 'cli' !== PHP_SAPI ) { // THIS LINE
throw new \Exception('only from CLI');
}
echo 'hi';
We check if the current system state is something that we can work with. In general we reject everything except the one working thing which is an assertion IMHO.
This wouldn't be reported:
<?php
if ( 'cli' === PHP_SAPI ) {
echo 'hi';
} else {
throw new \Exception('CLI!');
}
Which makes the code unreadable if you think of 4-5 paths which are then nested damn deep. Also PHPMD may nag about the "else" and the linux kernel standards says
Now, some people will claim that having 8-character indentations makes the code move too far to the right, and makes it hard to read on a 80-character terminal screen. The answer to that is that if you need more than 3 levels of indentation, you’re screwed anyway, and should fix your program.
So please tell me how we can avoid the first expression to be marked as an "assumption".
There's currently no way to exclude this. This tool is also not in active development. But feel free to create a PR with a proposal and we can look into implementing it.
I would also argue that it shouldn't be a goal to have php-assumptions report 0 warnings. There are always edge-cases.
Thanks for your answer.
Before I do a fork I need to ask: Has there been a specific reason or problem with feasibility why this is not based on PHPCS?
Just curious because having a configurable way (so using the PHPCS xml) for all those edges would be great!
@ScreamingDev I'm not too sure whether I considered that. I may have gone down this path to learn more about how to write a static analyser. If you can somehow turn it into a rule that would be awesome of course.