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 (#104)
by
unknown
03:23
created

MarathonEntityUtils   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
c 1
b 0
f 0
lcom 0
cbo 0
dl 0
loc 87
ccs 38
cts 38
cp 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setPropertyIfExist() 0 9 3
A setAllPossibleProperties() 0 15 4
A noConv() 0 5 1
A convArray() 0 5 1
A convObject() 0 5 1
A convSortedObject() 0 7 1
A convClass() 0 5 1
A convArrayOfClass() 0 11 3
1
<?php
2
/**
3
 * @package: chapi
4
 *
5
 * @author: bthapaliya
6
 * @since: 2016-10-16
7
 *
8
 */
9
namespace Chapi\Entity\Marathon;
10
11
class MarathonEntityUtils
12
{
13 37
    public static function setPropertyIfExist($source, $target, $property)
14
    {
15 37
        if (isset($source[$property]) &&
16 37
            property_exists($target, $property)) {
17 37
            $target->{$property} = $source[$property];
18 37
            return true;
19
        }
20 5
        return false;
21
    }
22
23
    /**
24
     * Sets all possible properties in the class from $data.
25
     * If the type is array or object, then it is ignored if there is no conversion in $conversion_map.
26
     * @param $data
27
     * @param $target
28
     * @param $conversionMap
29
     *
30
     * @return array all fields in $data that weren't stored in $target
31
     */
32 47
    public static function setAllPossibleProperties($data, $target, $conversionMap = [])
33
    {
34 47
        $unknownProperties = [];
35 47
        foreach ($data as $attributeName => $attributeValue) {
36
            // Don't set array or objects if no conversion method is specified.
37
            // Because this would need further type information to properly set.
38 37
            if (isset($conversionMap[$attributeName])) {
39 26
                $data[$attributeName] = $conversionMap[$attributeName]($attributeValue);
40
            }
41 37
            if (!self::setPropertyIfExist($data, $target, $attributeName)) {
42 37
                $unknownProperties[$attributeName] = $attributeValue;
43
            }
44
        }
45 47
        return $unknownProperties;
46
    }
47
48
    /**
49
     * This is useful if you don't want an array or object to be skipped by setAllPossibleProperties().
50
     */
51 21
    public static function noConv() {
52
        return function($data) {
53 11
            return $data;
54 21
        };
55
    }
56
57 6
    public static function convArray() {
58
        return function($data) {
59 2
            return (array) $data;
60 6
        };
61
    }
62
63 11
    public static function convObject() {
64
        return function($data) {
65 3
            return (object) $data;
66 11
        };
67
    }
68
69
    /**
70
     * This is usefull for shorter and stable diff output.
71
     */
72 21
    public static function convSortedObject() {
73
        return function($data) {
74 12
            $a = (array) $data; // ksort is inplace, so we need a copy
75 12
            ksort($a);
76 12
            return (object) $a;
77 21
        };
78
    }
79
80 30
    public static function convClass($class) {
81
        return function($data) use ($class) {
82 8
            return new $class((array) $data);
83 30
        };
84
    }
85
86
    public static function convArrayOfClass($class) {
87 27
        return function($data) use ($class) {
88 6
            $return = [];
89 6
            if ($data !== null) {
90 6
                foreach ($data as $item) {
91 2
                    $return[] = new $class((array) $item);
92
                }
93
            }
94 6
            return $return;
95 27
        };
96
    }
97
}
98