Oh Dear is the all-in-one monitoring tool for your entire website. We monitor uptime, SSL certificates, broken links, scheduled tasks and more. You'll get a notifications for us when something's wrong. All that paired with a developer friendly API and kick-ass documentation. O, and you'll also be able to create a public status page under a minute. Start monitoring using our free trial now.

Practicing symmetry

Link –

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();
    }
}
Share Post LinkedIn

I write about Laravel, PHP, AI and building better software.

Every two weeks, I share practical tips, tutorials, and behind-the-scenes insights from maintaining 300+ open source packages. Join thousands of developers who read along.

No spam. Unsubscribe anytime. You can also follow me on X.

Found something interesting to share?

The community section is a place where developers share links to articles, tutorials and videos. Submit a link and help fellow developers discover great content. As a thank you, you'll receive a coupon for a discount on Spatie products.