Datadog collects and monitors your PHP app metrics and distributed traces in real-time with application performance monitoring. Decrease downtime and performance issues with Datadog APM by tracing requests across service boundaries and drilling into individual traces end-to-end with flame graphs. Start your 14-day trial for free today.

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

Stay up to date with all things Laravel, PHP, and JavaScript.

Follow me on Twitter. I regularly tweet out programming tips, and what I myself have learned in ongoing projects.

Every two weeks I send out a newsletter containing lots of interesting stuff for the modern PHP developer.

Expect quick tips & tricks, interesting tutorials, opinions and packages. Because I work with Laravel every day there is an emphasis on that framework.

Rest assured that I will only use your email address to send you the newsletter and will not use it for any other purposes.

Comments