Passed
Push — master ( ff0b4a...b3e34b )
by Alec
03:32
created

Picklock   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 69
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A callMethod() 0 23 2
A getValue() 0 21 2
1
<?php declare(strict_types=1);
2
3
namespace AlecRabbit\Helpers\Objects;
4
5
/**
6
 * Class Picklock
7
 *
8
 * @link https://gitlab.com/m0rtis/picklock
9
 * @license Apache License 2.0
10
 * @author Anton Fomichev aka m0rtis - [email protected]
11
 *
12
 * @package AlecRabbit\Helpers\Objects
13
 * @author AlecRabbit
14
 *
15
 * @internal
16
 */
17
final class Picklock
18
{
19
    public const EXCEPTION_TEMPLATE = 'Class [%s] does not have `%s` %s';
20
    public const METHOD = 'method';
21
    public const PROPERTY = 'property';
22
23
    /**
24
     * Calls method $methodName of object $object using arguments ...$args.
25
     *
26
     * @psalm-suppress InvalidScope
27
     *
28
     * @param object $object
29
     * @param string $methodName
30
     * @param mixed ...$args
31
     * @return mixed
32
     */
33 2
    public static function callMethod(object $object, string $methodName, ...$args)
34
    {
35
        $closure =
36
            /**
37
             * @param string $methodName
38
             * @param array $args
39
             * @return mixed
40
             */
41
            function (string $methodName, ...$args) {
42 2
                if (\method_exists($this, $methodName)) {
43 2
                    return $this->$methodName(...$args);
44
                }
45 2
                throw new \RuntimeException(
46 2
                    sprintf(
47 2
                        Picklock::EXCEPTION_TEMPLATE,
48 2
                        \get_class($this),
49 2
                        $methodName,
50 2
                        Picklock::METHOD
51
                    )
52
                );
53 2
            };
54
        return
55 2
            $closure->call($object, $methodName, ...$args);
56
    }
57
58
    /**
59
     * @psalm-suppress InvalidScope
60
     *
61
     * @param object $object
62
     * @param string $propName
63
     * @return mixed
64
     */
65 2
    public static function getValue(object $object, string $propName)
66
    {
67
        $closure =
68
            /**
69
             * @return mixed
70
             */
71
            function () use ($propName) {
72 2
                if (\property_exists($this, $propName)) {
73 2
                    return $this->$propName;
74
                }
75 2
                throw new \RuntimeException(
76 2
                    sprintf(
77 2
                        Picklock::EXCEPTION_TEMPLATE,
78 2
                        \get_class($this),
79 2
                        $propName,
80 2
                        Picklock::PROPERTY
81
                    )
82
                );
83 2
            };
84
        return
85 2
            $closure->bindTo($object, $object)();
86
    }
87
}
88