Completed
Push — master ( 5c637b...29a871 )
by Adam
02:41
created

ValueObject   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 155
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 155
c 0
b 0
f 0
wmc 31
lcom 1
cbo 0
ccs 67
cts 67
cp 1
rs 9.8

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __set() 0 6 1
A __toString() 0 4 1
A getType() 0 4 1
A hash() 0 4 1
A cloneObject() 0 4 1
A fromString() 0 4 1
A getValue() 0 4 1
A equals() 0 4 1
A __destruct() 0 4 1
C diff() 0 27 11
B count() 0 20 10
1
<?php
2
3
namespace BestServedCold\PhalueObjects;
4
5
use InvalidArgumentException;
6
use BestServedCold\PhalueObjects\Contract\ValueObject as ValueObjectInterface;
7
8
/**
9
 * Class ValueObject
10
 *
11
 * @package   BestServedCold\PhalueObjects
12
 * @author    Adam Lewis <[email protected]>
13
 * @copyright Copyright (c) 2015 Best Served Cold Media Limited
14
 * @license      http://http://opensource.org/licenses/GPL-3.0 GPL License
15
 * @link      http://bestservedcold.com
16
 * @since      0.0.1-alpha
17
 * @version   0.0.2-alpha
18
 */
19
class ValueObject implements ValueObjectInterface
20
{
21
    /**
22
     * @var mixed
23
     */
24
    protected $value;
25
26
    /**
27
     * @var string
28
     */
29
    protected $type;
30
31
    /**
32
     * Class Constructor
33
     */
34 179
    public function __construct($value)
35
    {
36 179
        $this->value = $value;
37 179
        $this->type  = gettype($value);
38 179
    }
39
40
    /**
41
     * @param  $field
42
     * @param  $value
43
     * @throws \RuntimeException
44
     * @return void
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use NoType.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
45
     */
46 1
    public function __set($field, $value)
47
    {
48 1
        throw new \RuntimeException(
49
            "You cannot set a value of a Value Object, that's the whole point!"
50 1
        );
51
    }
52
53
    /**
54
     * @return string
55
     */
56 31
    public function __toString()
57
    {
58 31
        return (string) $this->getValue();
59
    }
60
61
    /**
62
     * @return string
63
     */
64 11
    public function getType()
65
    {
66 11
        return $this->type;
67
    }
68
69
70
    /**
71
     * @return string
72
     */
73 1
    public function hash()
74
    {
75 1
        return spl_object_hash($this);
76
    }
77
78
    /**
79
     * @param  $object
80
     * @return mixed
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use object.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
81
     */
82 1
    public function cloneObject($object)
83
    {
84 1
        return clone($object);
85
    }
86
87
    /**
88
     * @param $string
89
     * @return static
90
     */
91 14
    public static function fromString($string)
92
    {
93 14
        return new static((string) $string);
94
    }
95
96
    /**
97
     * @return mixed
98
     */
99 147
    public function getValue()
100
    {
101 147
        return $this->value;
102
    }
103
104
    /**
105
     * @param  ValueObject $object
106
     * @return bool
107
     */
108 2
    public function equals(ValueObject $object)
0 ignored issues
show
Coding Style introduced by
function equals() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
109
    {
110 2
        return $this->value === $object->value;
111
    }
112
113
    /**
114
     * @inheritdoc
115
     */
116 169
    public function __destruct()
117
    {
118 169
        $this->value = null;
119 169
    }
120
121
    /**
122
     * @param  ValueObject $object
123
     * @return ValueObject
0 ignored issues
show
Documentation introduced by
Should the return type not be ValueObject|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
124
     */
125 2
    public function diff(ValueObject $object)
126
    {
127 2
        switch ($this->getType()) {
128 2
            case 'boolean':
129 2
            case 'double':
130 2
            case 'integer':
131 1
                return new static($this->getValue() - $object->getValue());
132 2
            case 'array':
133 2
                return new static(
134 2
                    $object->getType() === 'array'
135 2
                        ? array_diff_assoc($this->getValue(), $object->getValue())
136 2
                        : $this->getValue()
137 2
                );
138 1
            case 'string':
139 1
                return new static(
140 1
                    str_replace($object->getValue(), '', $this->getValue())
141 1
                );
142 1
            case 'NULL':
143 1
                return new static($object->getValue());
144 1
            case 'object':
145 1
            case 'resource':
146 1
                return null;
147 1
            case 'unknown type':
148 1
            default:
149 1
                throw new InvalidArgumentException('Cannot diff type [' . $this->type . '].');
150 1
        }
151
    }
152
153 9
    public function count()
154
    {
155 9
        switch ($this->getType()) {
156 9
            case 'boolean':
157 9
            case 'double':
158 9
            case 'integer':
159 3
                return $this->getValue();
160 7
            case 'array':
161 7
            case 'NULL':
162 7
                return count($this->getValue());
163 1
            case 'object':
164 1
            case 'resource':
165 1
                return null;
166 1
            case 'string':
167 1
                return strlen($this->getValue());
168 1
            case 'unknown type':
169 1
            default:
170 1
                throw new InvalidArgumentException('Cannot count type [' . $this->type . '].');
171 1
        }
172
    }
173
}
174