Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
9 / 9 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| Equals | |
100.00% |
9 / 9 |
|
100.00% |
2 / 2 |
7 | |
100.00% |
1 / 1 |
| invoke | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
5 | |||
| verifySignature | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Equals 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 equals method |
| 13 | * @final |
| 14 | */ |
| 15 | final class Equals 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 | if( |
| 26 | \Code\Type\Analyzer::typeMatch($instance, $arguments[0]) |
| 27 | && count($otherProps = \Code\Property\Analyzer::getAllProperties($arguments[0])) |
| 28 | === count($myProps = \Code\Property\Analyzer::getAllProperties($instance)) |
| 29 | ) { |
| 30 | for($i=0;$i<count($arguments);$i++) |
| 31 | if(!($otherProps[$i]->getValue($arguments[0]) === $myProps[$i]->getValue($instance))) { |
| 32 | return false; |
| 33 | } |
| 34 | return true; |
| 35 | } else return false; |
| 36 | } |
| 37 | /** |
| 38 | * Checks if the called method has the matching signature |
| 39 | * @access public |
| 40 | * @param string $name Method name |
| 41 | * @param array $arguments Method arguments |
| 42 | * @return bool The called method has the matching signature or not |
| 43 | */ |
| 44 | public function verifySignature(string $name, array $arguments = []) : bool { |
| 45 | return $name === 'equals' && count($arguments) === 1; |
| 46 | } |
| 47 | } |