Passed
Branch master (6fbc1b)
by Gareth
10:31
created

MagicMethodsTrait::is()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
rs 10
c 1
b 0
f 0
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 44
    public function __call($name, $arguments)
18
    {
19 44
        $callTypeIndex = 3;
20 44
        if (substr($name, 0, 2) == "is") {
21 28
            $callTypeIndex = 2;
22 28
        }
23
24 44
        $callType = substr($name, 0, $callTypeIndex);
25 44
        $propertyName = substr($name, $callTypeIndex);
26
27 44
        if (in_array($callType, array('get', 'is')) && count($arguments) == 0) {
28 39
            return $this->{$callType}($propertyName);
29
        }
30
31 16
        if (in_array($callType, array('add', 'set')) && count($arguments) == 1) {
32 15
            return $this->{$callType}($propertyName, $arguments[0]);
33
        }
34
35 1
        throw new \Exception("The method you tried to call doesn't exist");
36
    }
37
38 38
    public function __set($name, $value)
39
    {
40 38
        if (!$this->exists($name) && $this->exists(lcfirst($name))) {
41 31
            $name = lcfirst($name);
42 31
        }
43
44 38
        $this->$name = $value;
45 38
    }
46
47 45
    public function exists($name)
48
    {
49 45
        return property_exists($this, $name);
50
    }
51
52 36
    public function get($name)
53
    {
54 36
        $name = $this->getValidNameInCorrectCase([$name, "get$name"]);
55 35
        return $this->$name;
56
    }
57
58 14
    public function set($name, $value)
59
    {
60 14
        $name = $this->getNameInCorrectCase($name);
61 13
        $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...
62
63 13
        $this->$name = $value;
64
65 13
        return $this;
66
    }
67
68 6
    public function add($name, $value)
69
    {
70 6
        $name = $this->getNameInCorrectCase($name);
71 5
        $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...
72
73 5
        if ($this->$name == null) {
74 5
            $this->$name = array();
75 5
        }
76
77 5
        if (!is_array($this->$name)) {
78 1
            $this->$name = array($this->$name);
79 1
        }
80
81 5
        $this->{$name}[] = $value;
82
83 5
        return $this;
84
    }
85
86 28
    public function is($name)
87
    {
88 28
        $nameWithIs = "Is$name";
89 28
        $name = $this->getValidNameInCorrectCase([$nameWithIs, $name]);
90
91 27
        return ((bool) $this->$name);
92
    }
93
94 2
    public function cast($value, $type)
95
    {
96 2
        return Caster::cast($value, $type);
97
    }
98
99
    public function castToExchange($value, $type)
100
    {
101
        if (Caster::castExists($type, 'ExchangeFormat')) {
102
            $value = Caster::cast($value, 'ExchangeFormat');
103
        }
104
105
        return $value;
106
    }
107
108 39
    protected function getValidNameInCorrectCase($names)
109
    {
110 39
        foreach ($names as $name) {
111
            try {
112 39
                return $this->getNameInCorrectCase($name);
113 29
            } catch (\Exception $e) {
114
                //Nothing needed here. If everything errors out, we'll throw a new exception below
115
            }
116 29
        }
117
118 2
        throw new \Exception('Property ' . $names[0] . ' does not exist');
119
    }
120
121
    /**
122
     * @param $name
123
     * @return string
124
     * @throws \Exception
125
     */
126 43
    protected function getNameInCorrectCase($name)
127
    {
128 43
        if (!$this->exists($name) && $this->exists(lcfirst($name))) {
129 38
            $name = lcfirst($name);
130 38
        }
131
132 43
        if (!$this->exists($name)) {
133 31
            throw new \Exception('Property ' . $name . ' does not exist');
134
        }
135
136 39
        return $name;
137
    }
138
139
    /**
140
     * @param $name
141
     * @param $value
142
     * @return null
143
     */
144 13
    protected function castValueIfNeeded($name, $value)
145
    {
146 13
        if (isset($this->_typeMap[$name])) {
147 1
            $value = $this->cast($value, $this->_typeMap[$name]);
148
149 1
            return $value;
150
        }
151
152 12
        return $value;
153
    }
154
}
155