Completed
Push — master ( ea22f3...f39064 )
by Derek Stephen
01:37
created

StringValue   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
lcom 0
cbo 1
dl 0
loc 40
c 1
b 0
f 0
ccs 16
cts 16
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A getValue() 0 4 1
B canBeString() 0 12 5
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
    /**
15
     * StringValue constructor.
16
     *
17
     * @param string $value
18
     */
19 1
    public function __construct($value)
20
    {
21 1
        if(!$this->canBeString($value)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->canBeString($value) of type boolean|null is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
22 1
            throw new InvalidArgumentException('You must supply a value.');
23
        }
24 1
        $this->value = (string) $value;
25 1
    }
26
27
    /**
28
     * @return string
29
     */
30 1
    public function getValue()
31
    {
32 1
        return parent::getValue();
33
    }
34
35
    /**
36
     * @param $value
37
     * @return bool
38
     */
39 1
    private function canBeString($value)
40
    {
41 1
        switch(true) {
42 1
            case is_object($value) && method_exists($value, '__toString'):
43 1
            case is_null($value):
44 1
            case is_scalar($value):
45 1
                return true;
46
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
47 1
            default:
48 1
                return false;
49 1
        }
50
    }
51
}