Completed
Push — master ( a7d441...9e9528 )
by Gareth
03:30
created

MagicMethodsTrait::castToExchange()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 0
cts 5
cp 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
crap 6
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 39
    public function __call($name, $arguments)
18
    {
19 39
        $callTypeIndex = 3;
20 39
        if (substr($name, 0, 2) == "is") {
21 4
            $callTypeIndex = 2;
22 4
        }
23
24 39
        $callType = substr($name, 0, $callTypeIndex);
25 39
        $propertyName = substr($name, $callTypeIndex);
26
27 39
        if (in_array($callType, array('get', 'is')) && count($arguments) == 0) {
28 35
            return $this->{$callType}($propertyName);
29
        }
30
31 12
        if (in_array($callType, array('add', 'set')) && count($arguments) == 1) {
32 11
            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 33
    public function __set($name, $value)
39
    {
40 33 View Code Duplication
        if (!$this->exists($name) && $this->exists(lcfirst($name))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41 27
            $name = lcfirst($name);
42 27
        }
43
44 33
        $this->$name = $value;
45 33
    }
46
47 39
    public function exists($name)
48
    {
49 39
        return property_exists($this, $name);
50
    }
51
52 32
    public function get($name)
53
    {
54 32
        $name = $this->getValidNameInCorrectCase([$name, "get$name"]);
55 31
        return $this->$name;
56
    }
57
58 10
    public function set($name, $value)
59
    {
60 10
        $name = $this->getNameInCorrectCase($name);
61 9
        $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) (which targets 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 9
        $this->$name = $value;
64
65 9
        return $this;
66
    }
67
68 5
    public function add($name, $value)
69
    {
70 5
        $name = $this->getNameInCorrectCase($name);
71 4
        $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) (which targets 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 4
        if ($this->$name == null) {
74 4
            $this->$name = array();
75 4
        }
76
77 4
        if (!is_array($this->$name)) {
78 1
            $this->$name = array($this->$name);
79 1
        }
80
81 4
        $this->{$name}[] = $value;
82
83 4
        return $this;
84
    }
85
86 4
    public function is($name)
87
    {
88 4
        $nameWithIs = "Is$name";
89 4
        $name = $this->getValidNameInCorrectCase([$nameWithIs, $name]);
90
91 3
        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 35
    protected function getValidNameInCorrectCase($names)
109
    {
110 35
        foreach ($names as $name) {
111
            try {
112 35
                return $this->getNameInCorrectCase($name);
113 4
            } catch (\Exception $e) {
114
                //Nothing needed here. If everything errors out, we'll throw a new exception below
115
            }
116 4
        }
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 38
    protected function getNameInCorrectCase($name)
127
    {
128 38 View Code Duplication
        if (!$this->exists($name) && $this->exists(lcfirst($name))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
129 33
            $name = lcfirst($name);
130 33
        }
131
132 38
        if (!$this->exists($name)) {
133 6
            throw new \Exception('Property ' . $name . ' does not exist');
134
        }
135
136 34
        return $name;
137
    }
138
139
    /**
140
     * @param $name
141
     * @param $value
142
     * @return null
143
     */
144 9
    protected function castValueIfNeeded($name, $value)
145
    {
146 9
        if (isset($this->_typeMap[$name])) {
147 1
            $value = $this->cast($value, $this->_typeMap[$name]);
148
149 1
            return $value;
150
        }
151
152 8
        return $value;
153
    }
154
}
155