Completed
Push — master ( 8c9cd6...ef07c2 )
by Adam
08:13
created

ValueObject::count()   B

Complexity

Conditions 9
Paths 9

Size

Total Lines 18
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 0
cts 0
cp 0
rs 7.756
c 0
b 0
f 0
cc 9
eloc 15
nc 9
nop 0
crap 90
1
<?php
2
3
namespace BestServedCold\PhalueObjects;
4
5
use phpDocumentor\Compiler\Pass\PackageTreeBuilder;
6
7
/**
8
 * Class ValueObject
9
 *
10
 * @package   BestServedCold\PhalueObjects
11
 * @author    Adam Lewis <[email protected]>
12
 * @copyright Copyright (c) 2015 Best Served Cold Media Limited
13
 * @license	  http://http://opensource.org/licenses/GPL-3.0 GPL License
14
 * @link	  http://bestservedcold.com
15
 * @since	  0.0.1-alpha
16
 * @version   0.0.2-alpha
17
 */
18
class ValueObject implements ValueObjectInterface
19
{
20
    /**
21
     * @var \ReflectionClass
22
     */
23
    protected $reflection;
24
25
    /**
26
     * @var mixed
27
     */
28
    protected $value;
29
30
    /**
31 107
     * @var string
32
     */
33 107
    protected $type;
34 107
35 107
    /**
36
     * Class Constructor
37
     */
38
    public function __construct($value)
39
    {
40 1
        $this->value      = $value;
41
        $this->type       = gettype($value);
42 1
        $this->reflection = new \ReflectionClass($this);
43
44
    }
45
46
    /**
47
     * @return string
48
     */
49 1
    public function getType()
50
    {
51 1
        return $this->type;
52
    }
53 1
54
    /**
55
     * @return string
56
     */
57
    public function getShortName()
58
    {
59 1
        return $this->reflection->getShortName();
60
    }
61 1
62
    /**
63
     * @param  $field
64
     * @param  $value
65
     * @throws \RuntimeException
66
     * @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...
67
     */
68 1
    public function __set($field, $value)
69
    {
70 1
        throw new \RuntimeException(
71
            "You cannot set a value of a Value Object, that's the whole point!"
72
        );
73
    }
74
75
    /**
76
     * @return string
77
     */
78
    public function hash()
79
    {
80
        return spl_object_hash($this);
81
    }
82
83
    /**
84
     * @param  $object
85 81
     * @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...
86
     */
87 81
    public function cloneObject($object)
88
    {
89
        return clone($object);
90
    }
91
92
    /**
93 30
     * @param $string
94
     * @return static
95 30
     */
96
    public static function fromString($string)
97
    {
98
        return new static((string) $string);
99
    }
100
101
    /**
102
     * @return mixed
103
     */
104
    public function getValue()
105
    {
106
        return $this->value;
107
    }
108
109
    /**
110
     * @return string
111
     */
112
    public function __toString()
113
    {
114
        return (string) $this->value;
115
    }
116
117
    /**
118
     * @param  ValueObject $object
119
     * @return bool
120
     */
121
    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...
122
    {
123
        return $this->value === $object->value;
124
    }
125
126
    /**
127
     * @return bool
128
     */
129
    public function isArray()
130
    {
131
        return is_array($this->getValue());
132
    }
133
134
    /**
135
     * @param  ValueObject $object
136
     * @return array|mixed
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use ValueObject|string.

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...
137
     */
138
    public function diff(ValueObject $object)
139
    {
140
        switch ($this->type) {
141
            case 'boolean':
142
            case 'double':
143
            case 'integer':
144
                return new static($this->getValue() - $object->getValue());
145
            case 'array':
146
                return new static(array_diff_assoc($this->getValue(), $object->getValue()));
147
            case 'object':
148
            case 'resource':
149
            case 'NULL':
150
                return 'todo';
151
            case 'unknown type':
152
            default:
153
                throw new \InvalidArgumentException('Unknown type');
154
        }
155
    }
156
157
    public function count()
158
    {
159
        switch ($this->type) {
160
            case 'boolean':
161
            case 'double':
162
            case 'integer':
163
                return $this->getValue();
164
            case 'array':
165
                return count($this->getValue());
166
            case 'object':
167
            case 'resource':
168
            case 'NULL':
169
                return 'todo';
170
            case 'unknown type':
171
            default:
172
                throw new \InvalidArgumentException('Unknown type');
173
        }
174
    }
175
}
176