Passed
Push — master ( aa56e8...0ba2e0 )
by Ehsan
03:08
created

ClassUtility::getSetMethodByAttribute()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 29
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 29
ccs 10
cts 10
cp 1
rs 8.5806
cc 4
eloc 10
nc 5
nop 2
crap 4
1
<?php
2
3
namespace Botonomous\utility;
4
5
use Botonomous\AbstractBaseSlack;
6
7
/**
8
 * Class ClassUtility.
9
 */
10
class ClassUtility
11
{
12
    /**
13
     * @param string $fullName
14
     *
15
     * @return mixed
16
     */
17 19
    public function extractClassNameFromFullName($fullName)
18
    {
19
        // get last part of the namespace
20 19
        $classParts = explode('\\', $fullName);
21
22 19
        return end($classParts);
23
    }
24
25
    /**
26
     * @param AbstractBaseSlack $object
27
     * @param                   $attributes
28
     *
29
     * @return AbstractBaseSlack
30
     */
31 11
    public function loadAttributes(AbstractBaseSlack $object, $attributes)
32
    {
33
        // if $attributes is not array convert it to array
34 11
        if (!is_array($attributes)) {
35 1
            $attributes = json_decode($attributes, true);
36
        }
37
38 11
        foreach ($attributes as $attributeKey => $attributeValue) {
39 11
            $method = $this->getSetMethodByAttribute($object, $attributeKey);
40
41
            // ignore if setter function does not exist
42 11
            if (empty($method)) {
43 2
                continue;
44
            }
45
46 11
            $object->$method($attributeValue);
47
        }
48
49 11
        return $object;
50
    }
51
52
    /**
53
     * @param AbstractBaseSlack $object
54
     * @param $attributeKey
55
     *
56
     * @return bool|string
57
     */
58 11
    private function getSetMethodByAttribute(AbstractBaseSlack $object, $attributeKey)
59
    {
60
        // For id, we cannot use 'set'.$stringUtility->snakeCaseToCamelCase($attributeKey) since it's named slackId
61 11
        if ($attributeKey === 'id') {
62 10
            return 'setSlackId';
63
        }
64
65
        // handle ts because there is setTimestamp instead of setTs
66 11
        $attributeKey = $this->processTimestamp($attributeKey);
67
68 11
        $camelCase = (new StringUtility())->snakeCaseToCamelCase($attributeKey);
69
70
        /**
71
         * If camel case attribute starts with 'is', 'has', ... following by an uppercase letter, remove it
72
         * This is used to handle calling functions such as setIm or setUserDeleted
73
         * instead of setIsIm or setIsUserDeleted.
74
         *
75
         * The style checkers complain about functions such as setIsIm, ...
76
         */
77 11
        $booleanPrefix = $this->findBooleanPrefix($camelCase);
78 11
        if (!empty($booleanPrefix)) {
79
            // found the boolean prefix - remove it
80 3
            $camelCase = substr($camelCase, strlen($booleanPrefix));
81
        }
82
83 11
        $function = 'set'.$camelCase;
84
85 11
        return method_exists($object, $function) ? $function : false;
86
    }
87
88
    /**
89
     * If text is 'ts' or ends with '_ts' replace it with 'timestamp'
90
     *
91
     * @param $text
92
     *
93
     * @return mixed
94
     */
95 11
    private function processTimestamp($text)
96
    {
97 11
        if ($text === 'ts' || (new StringUtility())->endsWith($text, '_ts')) {
98
            // replace the last ts with timestamp
99 3
            $text = preg_replace('/ts$/', 'timestamp', $text);
100
        }
101
102 11
        return $text;
103
    }
104
105
    /**
106
     * Check if the text starts with boolean prefixes such as 'is', 'has', ...
107
     *
108
     * @param $text
109
     *
110
     * @return mixed
111
     */
112 11
    private function findBooleanPrefix($text)
113
    {
114 11
        $booleanPrefixes = ['is', 'has'];
115 11
        sort($booleanPrefixes);
116
117 11
        foreach ($booleanPrefixes as $booleanPrefix) {
118 11
            if (!preg_match('/^((?i)'.$booleanPrefix.')[A-Z0-9]/', $text)) {
119 11
                continue;
120
            }
121
122 3
            return $booleanPrefix;
123
        }
124 11
    }
125
}
126