Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
Getters
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
3 / 3
5
100.00% covered (success)
100.00%
1 / 1
 invoke
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 verifySignature
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * Getters class implementation file
4 * @package Code\Simplify
5 * @author Felipe Gaspar <felipesouzalimagaspar@gmail.com>
6 */
7declare(strict_types=1);
8namespace Code\Simplify;
9
10#[\Attribute(\Attribute::TARGET_CLASS)]
11/**
12 * Implements a getter method
13 * @final
14 */
15final class Getters implements MagicallyInvokedMethod {
16    /**
17     * @var string Define a default prefix in method name
18     */
19    private string $prefix;
20    /**
21     * Invokes a method from an instance with the given parameters
22     * @access public
23     * @param object $instance Object on which the method will be invoked
24     * @param string $name Method name
25     * @param array $arguments Method arguments
26     * @return mixed The return varies according to the invoked method
27     */
28    public function invoke(object $instance, string $name, array $arguments = []) : mixed {
29        if($prop = \Code\Property\Analyzer::getPropertyAccessIfExists(str_replace($this->prefix, "", $name), $instance))
30            return $prop->getValue($instance);
31        throw \Code\Simplify\Solver::UndefinedMethodError($instance, $name, $arguments);
32    }
33    /**
34     * Checks if the called method has the matching signature
35     * @access public
36     * @param string $name Method name
37     * @param array $arguments Method arguments
38     * @return bool The called method has the matching signature or not
39     */
40    public function verifySignature(string $name, array $arguments = []) : bool {
41        return (strpos($name, $this->prefix) === 0 && count($arguments) === 0);
42    }
43    /**
44     * @access public
45     * @param string $prefix Define a default prefix in method name
46     */
47    public function __construct(string $prefix = 'get') { $this->prefix = $prefix; }
48}