GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#6)
by Cees-Jan
02:37
created

functions.php ➔ call_method_array_property()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 4
nop 3
dl 0
loc 18
ccs 0
cts 0
cp 0
crap 20
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace WyriHaximus\ApiClient;
4
5
use ReflectionClass;
6
use ReflectionProperty;
7
use WyriHaximus\ApiClient\Resource\ResourceInterface;
8
9
/**
10
 * @param ResourceInterface $resource
11
 */
12
function resource_pretty_print(ResourceInterface $resource)
13
{
14
    $printResource = clone $resource;
15
16
    call_method($printResource, 'unsetTransport');
17
18
    var_export($printResource);
19
}
20
21
/**
22
 * @param ResourceInterface $resource
23
 * @param string $method
24
 * @return ResourceInterface
25
 */
26
function call_method(ResourceInterface $resource, string $method): ResourceInterface
27
{
28
    if (method_exists($resource, $method)) {
29
        $resource->$method();
30
    }
31
32
    $properties = get_properties($resource);
33
    call_method_properties($resource, $properties, $method);
34
35
    return $resource;
36
}
37
38
/**
39
 * @param ResourceInterface $resource
40
 * @param array $properties
41
 * @param string $method
42
 */
43
function call_method_properties(ResourceInterface $resource, array $properties, string $method)
44
{
45
    foreach ($properties as $property) {
46
        call_method_property($resource, $property->getName(), $method);
47
        call_method_array_property($resource, $property->getName(), $method);
48
    }
49
}
50
51
/**
52
 * @param ResourceInterface $resource
53
 * @param string $propertyName
54
 * @param string $method
55
 */
56
function call_method_property(ResourceInterface $resource, string $propertyName, string $method)
57
{
58
    $property = get_property($resource, $propertyName);
59
    if (!($property->getValue($resource) instanceof ResourceInterface)) {
60
        return;
61
    }
62
63
    $property->setValue(
64
        $resource,
65
        call_method(
66
            $property->getValue($resource),
67
            $method
68
        )
69
    );
70
}
71
72
/**
73
 * @param ResourceInterface $resource
74
 * @param string $propertyName
75
 * @param string $method
76
 */
77
function call_method_array_property(ResourceInterface $resource, string $propertyName, string $method)
78
{
79
    $property = get_property($resource, $propertyName);
80
    if (!is_array($property->getValue($resource))) {
81
        return;
82
    }
83
    $newCollection = [];
84
85
    foreach ($property->getValue($resource) as $key => $value) {
86
        if (!($value instanceof ResourceInterface)) {
87
            continue;
88
        }
89
90
        $newCollection[$key] = call_method($value, $method);
91
    }
92
93
    $property->setValue($resource, $newCollection);
94
}
95
96
/**
97
 * @param ResourceInterface $resource
98
 * @return array
99
 */
100
function get_properties(ResourceInterface $resource): array
101
{
102 1
    $class = new ReflectionClass($resource);
103 1
    return $class->getProperties();
104
}
105
106
/**
107
 * @param ResourceInterface $resource
108
 * @param string $property
109
 * @return ReflectionProperty
110
 */
111
function get_property(ResourceInterface $resource, string $property)
112
{
113 1
    $class = new ReflectionClass($resource);
114 1
    $prop = $class->getProperty($property);
115 1
    $prop->setAccessible(true);
116 1
    return $prop;
117
}
118