Passed
Push — develop ( 479492...2f2e47 )
by Mikaël
03:12
created

AbstractStructBase::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace WsdlToPhp\PackageBase;
4
5
abstract class AbstractStructBase implements StructInterface, \JsonSerializable
6
{
7
    /**
8
     * Returns the properties of this object
9
     * @return mixed[]
10
     */
11 5
    public function jsonSerialize()
12
    {
13 5
        return \get_object_vars($this);
14
    }
15
    /**
16
     * Generic method called when an object has been exported with var_export() functions
17
     * It allows to return an object instantiated with the values
18
     * @uses AbstractStructBase::_set()
19
     * @param array $array the exported values
20
     * @return Struct
21
     */
22 15
    public static function __set_state(array $array)
23
    {
24 15
        $className = get_called_class();
25 15
        $object = new $className();
26 15
        foreach ($array as $name => $value) {
27 15
            $object->_set($name, $value);
28 9
        }
29 10
        return $object;
30
    }
31
    /**
32
     * Generic method setting value
33
     * @throws \InvalidArgumentException
34
     * @param string $name property name to set
35
     * @param mixed $value property value to use
36
     * @return Struct
37
     */
38 65
    public function _set($name, $value)
39
    {
40 65
        $setMethod = 'set' . ucfirst($name);
41 65
        if (method_exists($this, $setMethod)) {
42 65
            $this->$setMethod($value);
43 39
        } else {
44 5
            throw new \InvalidArgumentException(sprintf('Setter does not exist for "%s" property', $name));
45
        }
46 65
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type WsdlToPhp\PackageBase\AbstractStructBase which is incompatible with the documented return type WsdlToPhp\PackageBase\Struct.
Loading history...
47
    }
48
    /**
49
     * Generic method getting value
50
     * @throws \InvalidArgumentException
51
     * @param string $name property name to get
52
     * @return mixed
53
     */
54 65
    public function _get($name)
55
    {
56 65
        $getMethod = 'get' . ucfirst($name);
57 65
        if (method_exists($this, $getMethod)) {
58 60
            return $this->$getMethod();
59
        }
60 5
        throw new \InvalidArgumentException(sprintf('Getter does not exist for "%s" property', $name));
61
    }
62
    /**
63
     * Default string representation of current object. Don't want to expose any sensible data
64
     * @return string
65
     */
66 10
    public function __toString()
67
    {
68 10
        return get_called_class();
69
    }
70
}
71