DynamicParams   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 0
dl 0
loc 34
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B __call() 0 25 6
1
<?php
2
3
namespace Robo\Common;
4
5
/**
6
 * Simplifies generating of configuration chanined methods.
7
 * You can only define configuration properties and use magic methods to set them.
8
 * Methods will be named the same way as properties.
9
 * * Boolean properties are switched on/off if no values is provided.
10
 * * Array properties can accept non-array values, in this case value will be appended to array.
11
 * You should also define phpdoc for methods.
12
 */
13
trait DynamicParams
14
{
15
    /**
16
     * @param string $property
17
     * @param array $args
18
     *
19
     * @return $this
20
     */
21
    public function __call($property, $args)
22
    {
23
        if (!property_exists($this, $property)) {
24
            throw new \RuntimeException("Property $property in task " . get_class($this) . ' does not exists');
25
        }
26
27
        // toggle boolean values
28
        if (!isset($args[0]) and (is_bool($this->$property))) {
29
            $this->$property = !$this->$property;
30
            return $this;
31
        }
32
33
        // append item to array
34
        if (is_array($this->$property)) {
35
            if (is_array($args[0])) {
36
                $this->$property = $args[0];
37
            } else {
38
                array_push($this->$property, $args[0]);
39
            }
40
            return $this;
41
        }
42
43
        $this->$property = $args[0];
44
        return $this;
45
    }
46
}
47