Passed
Push — master ( 0a4514...70449d )
by Vincent
04:28
created

BooleanElement::tryCast()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 10
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
namespace Bdf\Form\Leaf;
4
5
use BadMethodCallException;
6
use Bdf\Form\Child\Http\HttpFieldPath;
7
use Bdf\Form\Leaf\View\BooleanElementView;
8
use Bdf\Form\Transformer\TransformerInterface;
9
use Bdf\Form\Validator\ValueValidatorInterface;
10
use Bdf\Form\View\ElementViewInterface;
11
use Bdf\Form\View\FieldViewInterface;
12
use LogicException;
13
14
/**
15
 * Handle a boolean value, like with checkbox input
16
 * A value is considered as true when a value is present, and equals to the defined value
17
 *
18
 * @see BooleanElementBuilder for build the element
19
 *
20
 * @method bool value()
21
 * @extends LeafElement<bool>
22
 */
23
class BooleanElement extends LeafElement
24
{
25
    /**
26
     * @var string
27
     */
28
    private $httpValue = '1';
29
30
    /**
31
     * BooleanElement constructor.
32
     *
33
     * @param ValueValidatorInterface|null $validator
34
     * @param TransformerInterface|null $transformer
35
     * @param string $httpValue Value to use as "true" value for HTTP value
36
     */
37 36
    public function __construct(?ValueValidatorInterface $validator = null, ?TransformerInterface $transformer = null, string $httpValue = '1')
38
    {
39 36
        parent::__construct($validator, $transformer);
40
41 36
        $this->httpValue = $httpValue;
42 36
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 11
    protected function toPhp($httpValue)
48
    {
49 11
        return (bool) $httpValue;
0 ignored issues
show
Bug Best Practice introduced by
The expression return (bool)$httpValue returns the type boolean which is incompatible with the return type mandated by Bdf\Form\Leaf\LeafElement::toPhp() of Bdf\Form\Leaf\T|null.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 6
    protected function toHttp($phpValue)
56
    {
57 6
        return $phpValue ? $this->httpValue : null;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 15
    protected function tryCast($value): ?bool
64
    {
65 15
        if ($value === null) {
66 1
            return null;
67
        }
68
69 14
        if (!is_scalar($value)) {
70 3
            throw new \TypeError('The import()\'ed value of a '.static::class.' must be a scalar value or null');
71
        }
72
73 11
        return (bool) $value;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     *
79
     * @return FieldViewInterface
80
     */
81 2
    public function view(?HttpFieldPath $field = null): ElementViewInterface
82
    {
83 2
        return new BooleanElementView(self::class, (string) $field, $this->httpValue(), $this->httpValue, (bool) $this->value(), $this->error()->global());
84
    }
85
}
86