Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
9 / 9 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| Setters | |
100.00% |
9 / 9 |
|
100.00% |
3 / 3 |
9 | |
100.00% |
1 / 1 |
| invoke | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
6 | |||
| verifySignature | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Setters 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 | |
| 10 | #[\Attribute(\Attribute::TARGET_CLASS)] |
| 11 | /** |
| 12 | * Implements a setter method |
| 13 | * @final |
| 14 | */ |
| 15 | final class Setters implements MagicallyInvokedMethod { |
| 16 | /** |
| 17 | * @var string Define a default prefix in method name |
| 18 | */ |
| 19 | private string $prefix; |
| 20 | /** |
| 21 | * @var bool Allows chaining of calls to setter methods |
| 22 | */ |
| 23 | private bool $chain; |
| 24 | /** |
| 25 | * Invokes a method from an instance with the given parameters |
| 26 | * @access public |
| 27 | * @param object $instance Object on which the method will be invoked |
| 28 | * @param string $name Method name |
| 29 | * @param array $arguments Method arguments |
| 30 | * @return mixed The return varies according to the invoked method |
| 31 | */ |
| 32 | public function invoke(object $instance, string $name, array $arguments = []) : mixed { |
| 33 | if($prop = \Code\Property\Analyzer::getPropertyAccessIfExists(str_replace($this->prefix, "", $name), $instance)) { |
| 34 | if($prop->getType() === null || ($arguments[0] === null) || (\Code\Type\Analyzer::typeMatch($arguments[0], $prop->getType()))) { |
| 35 | $prop->setValue($instance, $arguments[0]); |
| 36 | return ($this->chain) ? $instance : null; |
| 37 | }else throw new \TypeError("Typed property ".get_class($instance)."::{$prop->getName()} must be {$prop->getType()}, ".\Code\Type\Analyzer::getType($arguments[0])." used"); |
| 38 | } |
| 39 | throw \Code\Simplify\Solver::UndefinedMethodError($instance, $name, $arguments); |
| 40 | } |
| 41 | /** |
| 42 | * Checks if the called method has the matching signature |
| 43 | * @access public |
| 44 | * @param string $name Method name |
| 45 | * @param array $arguments Method arguments |
| 46 | * @return bool The called method has the matching signature or not |
| 47 | */ |
| 48 | public function verifySignature(string $name, array $arguments = []) : bool { |
| 49 | return (strpos($name, $this->prefix) === 0 && count($arguments) === 1); |
| 50 | } |
| 51 | /** |
| 52 | * @access public |
| 53 | ** @param string $prefix Define a default prefix in method name |
| 54 | * @param string $chain Allows chaining of calls to setter methods |
| 55 | */ |
| 56 | public function __construct(string $prefix = 'set', bool $chain = true) { |
| 57 | $this->prefix = $prefix; |
| 58 | $this->chain = $chain; |
| 59 | } |
| 60 | } |