Skip to content

Allow assertions for early exit strategies #32

Description

@ScreamingDev

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".

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions