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

StringValue::canBeString()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 12
c 0
b 0
f 0
ccs 9
cts 9
cp 1
rs 8.8571
nc 4
cc 5
eloc 9
nop 1
crap 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
}