StringValue   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 76
ccs 29
cts 29
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A getValue() 0 4 1
A canBeString() 0 8 1
A isStringableObject() 0 5 2
A isNullValue() 0 5 1
A isScalarValue() 0 5 1
A updateStringable() 0 6 2
1
<?php
2
/**
3
 * User: delboy1978uk
4
 * Date: 13/11/2016
5
 * Time: 13:26
6
 */
7
8
namespace Del\Common\Value;
9
10
use InvalidArgumentException;
11
12
class StringValue extends AbstractValue
13
{
14
    /** @var bool $stringable */
15
    private $stringable;
16
17
    /**
18
     * StringValue constructor.
19
     *
20
     * @param string $value
21
     */
22 1
    public function __construct($value)
23
    {
24 1
        if(!$this->canBeString($value)) {
25 1
            throw new InvalidArgumentException('You must supply a value.');
26
        }
27 1
        $this->value = (string) $value;
28 1
    }
29
30
    /**
31
     * @return string
32
     */
33 1
    public function getValue()
34
    {
35 1
        return parent::getValue();
36
    }
37
38
    /**
39
     * @param $value
40
     * @return bool
41
     */
42 1
    private function canBeString($value)
43
    {
44 1
        $this->stringable = false;
45 1
        $this->isStringableObject($value);
46 1
        $this->isNullValue($value);
47 1
        $this->isScalarValue($value);
48 1
        return $this->stringable;
49
    }
50
51
    /**
52
     * @param $value
53
     */
54 1
    private function isStringableObject($value)
55
    {
56 1
        $bool = is_object($value) && method_exists($value, '__toString');
57 1
        $this->updateStringable($bool);
58 1
    }
59
60
    /**
61
     * @param $value
62
     */
63 1
    private function isNullValue($value)
64
    {
65 1
        $bool = is_null($value);
66 1
        $this->updateStringable($bool);
67 1
    }
68
69
    /**
70
     * @param $value
71
     */
72 1
    private function isScalarValue($value)
73
    {
74 1
        $bool = is_scalar($value);
75 1
        $this->updateStringable($bool);
76 1
    }
77
78
    /**
79
     * @param $bool
80
     */
81 1
    private function updateStringable($bool)
82
    {
83 1
        if ($this->stringable === false) {
84 1
            $this->stringable = $bool;
85
        }
86
    }
87
}