Completed
Push — master ( 975c17...8f2777 )
by Adam
04:20 queued 01:49
created

ValueObject::count()   B

Complexity

Conditions 10
Paths 10

Size

Total Lines 20
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 10

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 18
cts 18
cp 1
rs 7.2765
c 0
b 0
f 0
cc 10
eloc 17
nc 10
nop 0
crap 10

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 139
    public function __construct($value)
35
    {
36 139
        $this->value = $value;
37 139
        $this->type  = gettype($value);
38 139
    }
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 32
    public function __toString()
57
    {
58 32
        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 2
    public static function fromString($string)
92
    {
93 2
        return new static((string) $string);
94
    }
95
96
    /**
97
     * @return mixed
98
     */
99 108
    public function getValue()
100
    {
101 108
        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
     * @param  ValueObject $object
115
     * @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...
116
     */
117 2
    public function diff(ValueObject $object)
118
    {
119 2
        switch ($this->getType()) {
120 2
            case 'boolean':
121 2
            case 'double':
122 2
            case 'integer':
123 1
                return new static($this->getValue() - $object->getValue());
124 2
            case 'array':
125 2
                return new static(
126 2
                    $object->getType() === 'array'
127 2
                        ? array_diff_assoc($this->getValue(), $object->getValue())
128 2
                        : $this->getValue()
129 2
                );
130 1
            case 'string':
131 1
                return new static(
132 1
                    str_replace($object->getValue(), '', $this->getValue())
133 1
                );
134 1
            case 'NULL':
135 1
                return new static($object->getValue());
136 1
            case 'object':
137 1
            case 'resource':
138 1
                return null;
139 1
            case 'unknown type':
140 1
            default:
141 1
                throw new InvalidArgumentException('Cannot diff type [' . $this->type . '].');
142 1
        }
143
    }
144
145 9
    public function count()
146
    {
147 9
        switch ($this->getType()) {
148 9
            case 'boolean':
149 9
            case 'double':
150 9
            case 'integer':
151 3
                return $this->getValue();
152 7
            case 'array':
153 7
            case 'NULL':
154 7
                return count($this->getValue());
155 1
            case 'object':
156 1
            case 'resource':
157 1
                return null;
158 1
            case 'string':
159 1
                return strlen($this->getValue());
160 1
            case 'unknown type':
161 1
            default:
162 1
                throw new InvalidArgumentException('Cannot count type [' . $this->type . '].');
163 1
        }
164
    }
165
}
166