Passed
Pull Request — master (#265)
by Gareth
13:13
created

MagicMethodsTrait::add()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.0123

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
nc 4
nop 2
dl 0
loc 16
rs 10
c 1
b 0
f 0
ccs 8
cts 9
cp 0.8889
crap 3.0123
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: gareth
5
 * Date: 27-8-15
6
 * Time: 10:27
7
 */
8
9
namespace garethp\ews\API;
10
11
use garethp\ews\Caster;
12
13
trait MagicMethodsTrait
14
{
15
    protected $_typeMap = [];
16
17 1
    public function __call($name, $arguments)
18
    {
19 1
        $callTypeIndex = 3;
20 1
        if (substr($name, 0, 2) == "is") {
21
            $callTypeIndex = 2;
22
        }
23
24 1
        $callType = substr($name, 0, $callTypeIndex);
25 1
        $propertyName = substr($name, $callTypeIndex);
26
27 1
        if (in_array($callType, array('get', 'is')) && count($arguments) == 0) {
28
            return $this->{$callType}($propertyName);
29
        }
30
31 1
        if (in_array($callType, array('add', 'set')) && count($arguments) == 1) {
32 1
            return $this->{$callType}($propertyName, $arguments[0]);
33
        }
34
35
        throw new \Exception("The method you tried to call doesn't exist");
36
    }
37
38 31
    public function __set($name, $value)
39
    {
40 31
        if (is_object($value) && !($value instanceof Type) && property_exists($value, "Entry")) {
41 4
            $value = $value->Entry;
42
        }
43
44 31
        if ($this->methodExists("set" . ucfirst($name))) {
45 31
            $this->{"set" . ucfirst($name)}($this->castValueIfNeeded(lcfirst($name), $value));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->castValueIfNeeded(lcfirst($name), $value) targeting garethp\ews\API\MagicMet...it::castValueIfNeeded() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
46 31
            return;
47
        }
48
49 5
        if (!$this->exists($name) && $this->exists(lcfirst($name))) {
50
            $name = lcfirst($name);
51
        }
52
53 5
        $this->$name = $value;
54
    }
55
56 30
    public function exists($name)
57
    {
58 30
        return property_exists($this, $name);
59
    }
60
61 31
    public function methodExists($name)
62
    {
63 31
        return method_exists($this, $name);
64
    }
65
66
    public function get($name)
67
    {
68
        $name = $this->getValidNameInCorrectCase([$name, "get$name"]);
69
        return $this->$name;
70
    }
71
72
    public function set($name, $value)
73
    {
74
        $name = $this->getNameInCorrectCase($name);
75
        $value = $this->castValueIfNeeded($name, $value);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $value is correct as $this->castValueIfNeeded($name, $value) targeting garethp\ews\API\MagicMet...it::castValueIfNeeded() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
76
77
        $this->$name = $value;
78
79
        return $this;
80
    }
81
82 1
    public function add($name, $value)
83
    {
84 1
        $name = $this->getNameInCorrectCase($name);
85 1
        $value = $this->castValueIfNeeded($name, $value);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $value is correct as $this->castValueIfNeeded($name, $value) targeting garethp\ews\API\MagicMet...it::castValueIfNeeded() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
86
87 1
        if ($this->$name == null) {
88 1
            $this->$name = array();
89
        }
90
91 1
        if (!is_array($this->$name)) {
92
            $this->$name = array($this->$name);
93
        }
94
95 1
        $this->{$name}[] = $value;
96
97 1
        return $this;
98
    }
99
100
    public function is($name)
101
    {
102
        $nameWithIs = "Is$name";
103
        $name = $this->getValidNameInCorrectCase([$nameWithIs, $name]);
104
105
        return ((bool) $this->$name);
106
    }
107
108 10
    public function cast($value, $type)
109
    {
110 10
        return Caster::cast($value, $type);
111
    }
112
113
    public function castToExchange($value, $type)
114
    {
115
        if (Caster::castExists($type, 'ExchangeFormat')) {
116
            $value = Caster::cast($value, 'ExchangeFormat');
117
        }
118
119
        return $value;
120
    }
121
122
    protected function getValidNameInCorrectCase($names)
123
    {
124
        foreach ($names as $name) {
125
            try {
126
                return $this->getNameInCorrectCase($name);
127
            } catch (\Exception $e) {
128
                //Nothing needed here. If everything errors out, we'll throw a new exception below
129
            }
130
        }
131
132
        throw new \Exception('Property ' . $names[0] . ' does not exist');
133
    }
134
135
    /**
136
     * @param $name
137
     * @return string
138
     * @throws \Exception
139
     */
140 1
    protected function getNameInCorrectCase($name)
141
    {
142 1
        if (!$this->exists($name) && $this->exists(lcfirst($name))) {
143 1
            $name = lcfirst($name);
144
        }
145
146 1
        if (!$this->exists($name)) {
147
            throw new \Exception('Property ' . $name . ' does not exist');
148
        }
149
150 1
        return $name;
151
    }
152
153
    /**
154
     * @param $name
155
     * @param $value
156
     * @return null
157
     */
158 35
    protected function castValueIfNeeded($name, $value)
159
    {
160 35
        if (isset($this->_typeMap[$name])) {
161 9
            $value = $this->cast($value, $this->_typeMap[$name]);
162
163 9
            return $value;
164
        }
165
166 34
        return $value;
167
    }
168
}
169