Skip to content

Instantly share code, notes, and snippets.

@iRynoh
Last active September 6, 2018 16:28
Show Gist options
  • Save iRynoh/8a5df9e4e05538a5d65b89726b8eab67 to your computer and use it in GitHub Desktop.
Save iRynoh/8a5df9e4e05538a5d65b89726b8eab67 to your computer and use it in GitHub Desktop.
<?php
public function verify() {
$statistic = $this->getStatistic();
if ($statistic) {
if ($statistic->getTotalCount() === 0) {
$incident = IncidentFactory::CtIncident();
$incident->registerIncident();
} else {
$failurePercentage = $statistic->getFailCount() / $statistic->getTotalCount() * 100;
if ($failurePercentage > 40) {
$incident = IncidentFactory::CtIncident();
$incident->registerIncident();
}
}
}
}
@adamwathan
Copy link

I'd probably at least rewrite it like this:

<?php

public function verify() {  
    $statistic = $this->getStatistic();

    if (!$statistic) {
        return;
    }

    if ($statistic->getTotalCount() === 0 || $statistic->failurePercentage() > 40) {
        IncidentFactory::CtIncident()->registerIncident();
    }
}

Treat the falsey check like a guard clause and handle it separately at the beginning of the method, move the logic for calculating the failure percentage right into the statistic since it holds all the relevant data, and make it all a single compound conditional to avoid duplicating the incident factory stuff.

@iRynoh
Copy link
Author

iRynoh commented Sep 6, 2018

Awesome! Thank you so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment