Practicing symmetry
In a new video Jason McCreary, the creator of the wonderful Laravel Shift, demonstrates a few good tips to clean up code. In the video below Jason uses a code snippet taken from my side project Oh Dear!
If you're interested in more tips from Jason be sure to check out his upcoming BaseCode field guide.
Meanwhile I've cleaned up (and deployed) the code in the actual app. This is what I ended up with:
class Check
{
public function needsToRun(): bool
{
if (!$this->belongsToTeamOnActiveSubscriptionOrOnGenericTrial()) {
return false;
}
if ($this->disabled()) {
return false;
}
if ($this->alreadyRunningOrScheduled()) {
return false;
}
if ($this->didNotRunBefore()) {
return true;
}
if ($this->checkType()->is(CheckType::UPTIME) && $this->latestRun()->failed()) {
return true;
}
if ($this->previousRunCrashed()) {
return true;
}
return $this->latestRun()->endedMoreThanMinutesAgo($this->checkType()->minutesBetweenRuns());
}
protected function checkType(): CheckType
{
return new CheckType($this->type);
}
}
use MyCLabs\Enum\Enum;
class CheckType extends Enum
{
const UPTIME = 'uptime';
const BROKEN_LINKS = 'broken_links';
const MIXED_CONTENT = 'mixed_content';
const CERTIFICATE_HEALTH = 'certificate_health';
const CERTIFICATE_TRANSPARENCY = 'certificate_transparency';
public function minutesBetweenRuns(): int
{
if ($this->getValue() === static::MIXED_CONTENT) {
return 60 * 12;
}
if ($this->getValue() === static::BROKEN_LINKS) {
return 60 * 12;
}
if ($this->getValue() === static::CERTIFICATE_HEALTH) {
return 5;
}
if ($this->getValue() === static::UPTIME) {
return 3;
}
throw new Exception("Minutes between runs not specified for type `{$this->getValue()}`");
}
public function is(string $type): bool
{
return $type === $this->getValue();
}
}
What are your thoughts on "Practicing symmetry"?