Passed
Push — master ( 2f6942...d6d802 )
by Chris
28:11
created

PropertyResolver::convertToStudlyCaps()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace WebTheory\Collection\Resolution;
4
5
use ErrorException;
6
use LogicException;
7
use WebTheory\Collection\Contracts\PropertyResolverInterface;
8
9
class PropertyResolver implements PropertyResolverInterface
10
{
11
    protected array $defined = [];
12
13
    protected array $methods = [];
14
15
    protected array $members = [];
16
17 309
    public function __construct(array $defined = [])
18
    {
19 309
        $this->defined = $defined;
20
    }
21
22 309
    public function resolveProperty(object $object, string $property)
23
    {
24 309
        if ($definedMethod = $this->getDefinedMethod($property)) {
25 21
            return $object->{$definedMethod}();
26
        }
27
28 309
        if (property_exists($object, $property)) {
29
            try {
30 309
                return $object->{$property};
31
            } catch (ErrorException $e) {
0 ignored issues
show
Unused Code introduced by
catch (\ErrorException $e) is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
32
                // move on
33
            }
34
        }
35
36 6
        if (method_exists($object, $inferredMethod = $this->getInferredMethod($property))) {
37 3
            return $object->{$inferredMethod}();
38
        }
39
40 3
        if (property_exists($object, $inferredMember = $this->getInferredMember($property))) {
41
            try {
42
                return $object->{$inferredMember};
43
            } catch (ErrorException $e) {
44
                // move on
45
            }
46
        }
47
48 3
        throw new LogicException(
49 3
            sprintf(
50
                'No method of access has been defined or can be resolved for value "%s" in instances of class %s.',
51
                $property,
52 3
                get_class($object)
53
            )
54
        );
55
    }
56
57 309
    protected function getDefinedMethod(string $property): ?string
58
    {
59 309
        return $this->defined[$property] ?? null;
60
    }
61
62 6
    protected function getInferredMethod(string $property): string
63
    {
64 6
        if (!isset($this->methods[$property])) {
65 6
            $this->methods[$property] = $this->getPropertyAsMethod($property);
66
        }
67
68 6
        return $this->methods[$property];
69
    }
70
71 3
    protected function getInferredMember(string $property): string
72
    {
73 3
        if (!isset($this->members[$property])) {
74 3
            $this->members[$property] = $this->getPropertyAsMember($property);
75
        }
76
77 3
        return $this->members[$property];
78
    }
79
80 6
    protected function getPropertyAsMethod(string $property): string
81
    {
82 6
        return 'get' . $this->convertToStudlyCaps($property);
83
    }
84
85 3
    protected function getPropertyAsMember(string $property): string
86
    {
87 3
        return lcfirst($this->convertToStudlyCaps($property));
88
    }
89
90 6
    protected function convertToStudlyCaps(string $string): string
91
    {
92 6
        return str_replace(' ', '', ucwords(str_replace('_', ' ', $string)));
93
    }
94
}
95