zeus / anti-guard-clause
a small library that will save you from typing tons of if-else
Installs: 13
Dependents: 0
Suggesters: 0
Security: 0
Stars: 4
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/zeus/anti-guard-clause
Requires
- php: ^8.2
Requires (Dev)
- orchestra/testbench: ^7.21
- phpunit/phpunit: ^9
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