Completed
Branch master (09022f)
by Gareth
05:56 queued 03:06
created

MagicMethodsTrait::__call()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 20
Code Lines 11

Duplication

Lines 6
Ratio 30 %

Code Coverage

Tests 11
CRAP Score 6
Metric Value
dl 6
loc 20
ccs 11
cts 11
cp 1
rs 8.8571
cc 6
eloc 11
nc 6
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 35
    public function __call($name, $arguments)
18
    {
19 35
        $callTypeIndex = 3;
20 35
        if (substr($name, 0, 2) == "is") {
21 4
            $callTypeIndex = 2;
22
        }
23
24 35
        $callType = substr($name, 0, $callTypeIndex);
25 35
        $propertyName = substr($name, $callTypeIndex);
26
27 35 View Code Duplication
        if (in_array($callType, array('get', 'is')) && count($arguments) == 0) {
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...
28 31
            return $this->{$callType}($propertyName);
29
        }
30
31 12 View Code Duplication
        if (in_array($callType, array('add', 'set')) && count($arguments) == 1) {
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...
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 29
    public function __set($name, $value)
39
    {
40 29 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 23
            $name = lcfirst($name);
42
        }
43
44 29
        $this->$name = $value;
45
    }
46
47 35
    public function exists($name)
48
    {
49 35
        return property_exists($this, $name);
50
    }
51
52 28
    public function get($name)
53
    {
54 28
        if (!$this->exists($name) && $this->exists("get$name")) {
55
            $name = "get$name";
56
        }
57
58 28 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...
59 26
            $name = lcfirst($name);
60
        }
61
62 28
        if (!$this->exists($name)) {
63 1
            throw new \Exception('Property ' . $name . ' does not exist');
64
        }
65
66 27
        return $this->$name;
67
    }
68
69 10
    public function set($name, $value)
70
    {
71 10 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...
72 8
            $name = lcfirst($name);
73
        }
74
75 10
        if (!$this->exists($name)) {
76 1
            throw new \Exception('Property ' . $name . ' does not exist');
77
        }
78
79 9
        if (isset($this->_typeMap[$name])) {
80 1
            $value = $this->cast($value, $this->_typeMap[$name]);
81
        }
82
83 9
        $this->$name = $value;
84
85 9
        return $this;
86
    }
87
88 5
    public function add($name, $value)
89
    {
90 5 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...
91 4
            $name = lcfirst($name);
92
        }
93
94 5
        if (!$this->exists($name)) {
95 1
            throw new \Exception('Property ' . $name . ' does not exist');
96
        }
97
98 4
        if (isset($this->_typeMap[$name])) {
99
            $value = $this->cast($value, $this->_typeMap[$name]);
100
        }
101
102 4
        if ($this->$name == null) {
103 4
            $this->$name = array();
104
        }
105
106 4
        if (!is_array($this->$name)) {
107 1
            $this->$name = array($this->$name);
108
        }
109
110 4
        $this->{$name}[] = $value;
111
112 4
        return $this;
113
    }
114
115 4
    public function is($name)
116
    {
117 4
        $nameWithIs = "Is$name";
118 4
        if ($this->exists($nameWithIs)) {
119
            $name = $nameWithIs;
120 4
        } elseif ($this->exists(lcfirst($nameWithIs))) {
121 2
            $name = lcfirst($nameWithIs);
122 3
        } elseif ($this->exists(lcfirst($name))) {
123 2
            $name = lcfirst($name);
124
        }
125
126 4
        if (!$this->exists($name)) {
127 1
            throw new \Exception('Property ' . $name . ' does not exist');
128
        }
129
130 3
        return ((bool) $this->$name);
131
    }
132
133 2
    public function cast($value, $type)
134
    {
135 2
        return Caster::cast($value, $type);
136
    }
137
138
    public function castToExchange($value, $type)
139
    {
140
        if (Caster::castExists($type, 'ExchangeFormat')) {
141
            $value = Caster::cast($value, 'ExchangeFormat');
142
        }
143
144
        return $value;
145
    }
146
}
147