Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
Solver
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
3 / 3
5
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 solve
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 UndefinedMethodError
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * Solver class implementation file
4 * @package Code\Simplify
5 * @author Felipe Gaspar <felipesouzalimagaspar@gmail.com>
6 */
7declare(strict_types=1);
8namespace Code\Simplify;
9/**
10 * Discover, validate and execute the desired method call
11 * @final
12 */
13final class Solver {
14    /**
15     * @var array<MagicallyInvokedMethod> List of available methods
16     */
17    private array $declared_methods = [];
18    /**
19     * @access public
20     * @param MagicallyInvokedMethod ...$methods List of available methods
21     */
22    public function __construct(MagicallyInvokedMethod ...$methods) {
23        $this->declared_methods = $methods;
24    }
25    /**
26     * Checks if the called method has the matching signature
27     * @access public
28     * @param object $instance Object for method invocation
29     * @param string $name Method name
30     * @param array $arguments Method arguments
31     * @return mixed The return varies according to the invoked method
32     * @throws \Error Error when method is not defined
33     */
34    public function solve(object $instance, string $name, array $arguments = []) : mixed {
35        foreach($this->declared_methods as $method)
36            if($method->verifySignature($name, $arguments))
37                return $method->invoke($instance, $name, $arguments);
38        throw self::UndefinedMethodError($instance, $name, $arguments);
39    }
40    /**
41     * Checks if the called method has the matching signature
42     * @access public
43     * @param object $instance Object for method invocation
44     * @param string $name Method name
45     * @param array $arguments Method arguments
46     * @return Error Create an error when method is undefined
47     */
48    public static function UndefinedMethodError(object $instance, string $name, array $arguments) : \Error {
49        return new \Error("Call to undefined method ".get_class($instance)."::{$name}(".join(", ", $arguments).")");
50    }
51}