Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
5 / 5 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| ToString | |
100.00% |
5 / 5 |
|
100.00% |
2 / 2 |
4 | |
100.00% |
1 / 1 |
| invoke | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| verifySignature | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * ToString 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 ToString method |
| 13 | * @final |
| 14 | */ |
| 15 | final class ToString implements MagicallyInvokedMethod { |
| 16 | /** |
| 17 | * Invokes a method from an instance with the given parameters |
| 18 | * @access public |
| 19 | * @param object $instance Object on which the method will be invoked |
| 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 invoke(object $instance, string $name, array $arguments = []) : mixed { |
| 25 | $tmp = []; |
| 26 | foreach (\Code\Property\Analyzer::getAllProperties($instance) as $prop) |
| 27 | $tmp[] = join("=", [$prop->getName(),var_export($prop->getValue($instance), true)]); |
| 28 | return join('', [join('', [get_class($instance), '=(']), join("&", $tmp), ')']); |
| 29 | } |
| 30 | /** |
| 31 | * Checks if the called method has the matching signature |
| 32 | * @access public |
| 33 | * @param string $name Method name |
| 34 | * @param array $arguments Method arguments |
| 35 | * @return bool The called method has the matching signature or not |
| 36 | */ |
| 37 | public function verifySignature(string $name, array $arguments = []) : bool { |
| 38 | return in_array($name, ['__toString', 'toString']) && count($arguments) === 0; |
| 39 | } |
| 40 | } |