Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
8 / 8 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| Constructor | |
100.00% |
8 / 8 |
|
100.00% |
3 / 3 |
8 | |
100.00% |
1 / 1 |
| invoke | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
6 | |||
| verifySignature | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Constructor class implementation file |
| 4 | * @package Code\Simplify |
| 5 | * @author Felipe Gaspar <felipesouzalimagaspar@gmail.com> |
| 6 | */ |
| 7 | declare(strict_types=1); |
| 8 | namespace Code\Simplify; |
| 9 | #[\Attribute(\Attribute::TARGET_CLASS)] |
| 10 | /** |
| 11 | * Implements a constructor method |
| 12 | */ |
| 13 | class Constructor implements MagicallyInvokedMethod { |
| 14 | /** |
| 15 | * @var bool Define a constructor method without any attributes |
| 16 | */ |
| 17 | private bool $noArgs; |
| 18 | /** |
| 19 | * @var bool Define a constructor method with all attributes |
| 20 | */ |
| 21 | private bool $allArgs; |
| 22 | /** |
| 23 | * Invokes a method from an instance with the given parameters |
| 24 | * @access public |
| 25 | * @param object $instance Object on which the method will be invoked |
| 26 | * @param string $name Method name |
| 27 | * @param array $arguments Method arguments |
| 28 | * @return mixed The return varies according to the invoked method |
| 29 | */ |
| 30 | public function invoke(object $instance, string $name, array $arguments = []) : mixed { |
| 31 | if($this->noArgs && count($arguments) === 0) return $instance; |
| 32 | else if($this->allArgs && count($arguments) === count($props = \Code\Property\Analyzer::getAllProperties($instance))) { |
| 33 | for($i=0;$i<count($arguments);$i++) $props[$i]->setValue($instance, $arguments[$i]); |
| 34 | return $instance; |
| 35 | } |
| 36 | throw \Code\Simplify\Solver::UndefinedMethodError($instance, $name, $arguments); |
| 37 | } |
| 38 | /** |
| 39 | * Checks if the called method has the matching signature |
| 40 | * @access public |
| 41 | * @param string $name Method name |
| 42 | * @param array $arguments Method arguments |
| 43 | * @return bool The called method has the matching signature or not |
| 44 | */ |
| 45 | public function verifySignature(string $name, array $arguments = []) : bool { |
| 46 | return $name === '__construct'; |
| 47 | } |
| 48 | /** |
| 49 | * @access public |
| 50 | * @param bool $noArgs Constructor method without any attributes |
| 51 | * @param bool $allArgs Constructor method with all attributes |
| 52 | */ |
| 53 | public function __construct(bool $noArgs = true, bool $allArgs = true) { |
| 54 | $this->noArgs = $noArgs; |
| 55 | $this->allArgs = $allArgs; |
| 56 | } |
| 57 | } |