Phpinspectionsea: This condition is duplicated in another if/elseif branch

Created on 2 Oct 2017  路  5Comments  路  Source: kalessil/phpinspectionsea

Code example:

<?php

$weakErrors = 0;
$criticalErrors = 0;

if ($weakErrors > 0 || $criticalErrors > 0) {
    LogService::info('Starting processing errors');

    if ($weakErrors > 0) {
        // Some actions about weak errors
    }

    if ($criticalErrors > 0) {
        // Some ANOTHER actions about critical errors
    }

    LogService::info('Errors processing finished');
}

Inspection result:

question

All 5 comments

Because it is really duplicated in another branch. The best solution for cases like that is you store it to a variable then reuse that. It should not affects performance too much (about 0.000001%, maybe).

@rentalhost yes, you right. But from my point of view, it's a bit another type of duplication, so I reported it. If it is not a bug, then ok, I will just live with it :)

Hmm... Well, if $criticalErrors variable was changed in mid time, I should agree with you if this warning is happening. But in this case, I think that is okay create a variable to store it ($hasCriticalErrors = $criticalErrors > 0, for instance).

Let see what @kalessil will think about that.

@rentalhost I thnik @kalessil will disable this inspection =)) There are a lot of edge cases

IMO logging should happen inside inner if-s:

  • a first message will say clearly what we are processing
  • a second message should report how many entries we processed.

The goal is clearer split responsibilities, somehow the warning and @erickskrauch intention are pointing to mixed up responsibilities.

Was this page helpful?
0 / 5 - 0 ratings