Last active
November 6, 2019 15:58
-
-
Save jonathangreco/fa822fcb3d85085a16ccb1efa026866e to your computer and use it in GitHub Desktop.
Bitwise map with values as services
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace App\Api\Application\Service; | |
abstract class AbstractValue implements BitwiseCallback | |
{ | |
protected $flag; | |
public static function getFlag($value): BitwiseCallback | |
{ | |
$class = get_called_class(); | |
$var = new $class(); | |
$var->bitValue = $value ? $var->flag : 0; | |
return $var; | |
} | |
protected $bitValue; | |
public function bitValue(): int | |
{ | |
return $this->bitValue; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace App\Api\Application\Service; | |
interface BitwiseCallback | |
{ | |
static function getFlag($value): BitwiseCallback; | |
public function execute(); | |
public function bitValue(): int; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace App\Api\Domain\ValueObject; | |
use App\Api\Application\Service\VarA; | |
use App\Api\Application\Service\VarB; | |
use App\Api\Application\Service\VarC; | |
/** | |
* Object Calisthenics or how to use lookup map and biwise map to refactor a huge if statement. | |
* Class Test | |
* @package App\Api\Domain\ValueObject | |
*/ | |
class Test | |
{ | |
public function __construct() | |
{ | |
$a = VarA::getFlag("toto"); | |
$b = VarB::getFlag(null); | |
$c = VarC::getFlag("something sneaky"); | |
$flags = $a->bitValue() | $b->bitValue() | $c->bitValue(); | |
$map = [ | |
1 => function() use ($a) { $a->doStuff(); }, | |
3 => function() use ($a, $b) { return $a->doStuff() . $b->doOtherStuff() . $a->toStuffWithB($b);}, | |
5 => function() use ($c, $b) { return $c->doStuff() . $b->doOtherStuff() . $c->toStuffWithB($b);} | |
// and so on | |
]; | |
$result = $map[$flags]; | |
dd($result()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace App\Api\Application\Service; | |
class VarA extends AbstractValue | |
{ | |
protected $flag = 1; | |
public function execute() | |
{ | |
// TODO: Implement execute() method. | |
} | |
public function doStuff() | |
{ | |
return "shit it's working"; | |
} | |
public function toStuffWithB($b) | |
{ | |
return 'no way'; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace App\Api\Application\Service; | |
class VarB extends AbstractValue | |
{ | |
protected $flag = 2; | |
public function doOtherStuff() | |
{ | |
return "ok it's B guys"; | |
} | |
public function execute() | |
{ | |
// TODO: Implement execute() method. | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace App\Api\Application\Service; | |
class VarC extends AbstractValue | |
{ | |
protected $flag = 4; | |
public function doStuff() | |
{ | |
return "hey cool"; | |
} | |
public function toStuffWithB($b) | |
{ | |
return 'no way'; | |
} | |
public function execute() | |
{ | |
// TODO: Implement execute() method. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment