Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
11 / 11 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
| Simplify | |
100.00% |
11 / 11 |
|
100.00% |
5 / 5 |
8 | |
100.00% |
1 / 1 |
| __call | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getMethodSolver | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| jsonSerialize | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| __toString | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Simplify class implementation file |
| 4 | * @package Code\Simplify |
| 5 | * @author Felipe Gaspar <felipesouzalimagaspar@gmail.com> |
| 6 | */ |
| 7 | declare(strict_types=1); |
| 8 | namespace Code; |
| 9 | |
| 10 | use Code\Simplify\MagicallyInvokedMethod; |
| 11 | use Code\Simplify\Solver; |
| 12 | /** |
| 13 | * Simplified object abstraction |
| 14 | * @abstract |
| 15 | */ |
| 16 | abstract class Simplify implements \JsonSerializable { |
| 17 | /** |
| 18 | * Magic method to call any action |
| 19 | * @access public |
| 20 | * @param string $name Method name |
| 21 | * @param array $arguments Method arguments |
| 22 | * @return mixed The return varies according to the invoked method |
| 23 | */ |
| 24 | public function __call(string $name, array $arguments = []) : mixed { |
| 25 | return $this->getMethodSolver()->solve($this, $name, $arguments); |
| 26 | } |
| 27 | /** |
| 28 | * Get all methods available to this object |
| 29 | * @access private |
| 30 | * @return Solver Discover, validate and execute the desired method call |
| 31 | */ |
| 32 | private function getMethodSolver() : Solver { |
| 33 | $methods = []; |
| 34 | foreach((new \ReflectionClass($this))->getAttributes() as $method) { |
| 35 | $method = $method->newInstance(); |
| 36 | if($method instanceof MagicallyInvokedMethod) $methods[] = $method; |
| 37 | } |
| 38 | return new Solver(...$methods); |
| 39 | } |
| 40 | /** |
| 41 | * @access public |
| 42 | * @param mixed ...$params Variables passed by parameter on instanciation |
| 43 | */ |
| 44 | public function __construct(mixed ...$params) { |
| 45 | return $this->__call('__construct', func_get_args()); |
| 46 | } |
| 47 | /** |
| 48 | * Method to called in json serializable object |
| 49 | * @access public |
| 50 | * @return mixed Object data in json serializable format |
| 51 | */ |
| 52 | public function jsonSerialize() : mixed { |
| 53 | try { |
| 54 | return json_decode($this->__call('toJson'), true); |
| 55 | } catch(\Throwable $t) { |
| 56 | return (object) []; |
| 57 | } |
| 58 | } |
| 59 | /** |
| 60 | * Magic method __toString |
| 61 | * @access public |
| 62 | * @return string Object data in string format |
| 63 | */ |
| 64 | public function __toString() { |
| 65 | return $this->__call('toString'); |
| 66 | } |
| 67 | } |
| 68 |