zeus/anti-guard-clause

a small library that will save you from typing tons of if-else

v1.0.1 2023-02-27 08:04 UTC

This package is auto-updated.

Last update: 2024-04-27 11:57:07 UTC


README

Anti guard clause

I will not tell you what a guard clause is, those who want can research what it is, this library is written to prevent guard clause formation.

for install with composer

composer require zeus/anti-guard-clause

The using

An Example of the anti-guard clause

let's create a some class

Age class

use Zues\Less\IfInterface;

readonly class Age implements IfInterface
{

    public function __construct(private int $age)
    {
    }

    /**
     * @return mixed
     */
    public function make(): mixed
    {
        return 'age must be greater than 18';
    }

    /**
     * @return bool
     */
    public function isTrue(): bool
    {
        return  18<=$this->age;
    }
}

The Man class

use Zues\Less\IfInterface;

readonly class Man implements IfInterface
{


    public function __construct(private string $gender)
    {
    }
    
    public function make(): string
    {
        return 'gender must be a man';
    }

    /**
     * @return bool
     */
    public function isTrue(): bool
    {
        return $this->gender === 'man';
    }
}

let's create e default class for else

use Zues\Less\ElseInterface;

readonly class ElseGender implements ElseInterface
{

    /**
     * @return mixed
     */
    public function make(): string
    {
        return 'else condition';
    }
}

and using it

$gender = 'man';
$age = 16;

$condition = new Condition();
 $condition
    ->if(new Age($age))
    ->ifNot(new Man($gender))
    ->else(new ElseGender());
            
echo $condition->getMake(); //get Result