Passed
Push — master ( 61867f...694ec5 )
by Matthew
03:34 queued 01:30
created

BooleanHandler::handleBooleanType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 5
1
<?php
2
3
namespace Dynamic\Salsify\TypeHandler;
4
5
use SilverStripe\Core\Extension;
6
7
/**
8
 * Class BooleanHandler
9
 * @package Dynamic\Salsify\TypeHandler
10
 */
11
class BooleanHandler extends Extension
12
{
13
    /**
14
     * @var array
15
     */
16
    private static $field_types = [
0 ignored issues
show
introduced by
The private property $field_types is not used, and could be removed.
Loading history...
17
        'Boolean' => [
18
            'requiresWrite' => false,
19
            'requiresSalsifyObjects' => false,
20
            'allowsModification' => true,
21
        ],
22
    ];
23
24
    /**
25
     * @param string|SilverStripe\ORM\DataObject $class
0 ignored issues
show
Bug introduced by
The type Dynamic\Salsify\TypeHand...erStripe\ORM\DataObject was not found. Did you mean SilverStripe\ORM\DataObject? If so, make sure to prefix the type with \.
Loading history...
26
     * @param $data
27
     * @param $dataField
28
     * @param $config
29
     * @param $dbField
30
     * @return string|int
31
     *
32
     * @return string|boolean|int|double
33
     */
34
    public function handleBooleanType($class, $data, $dataField, $config, $dbField)
0 ignored issues
show
Unused Code introduced by
The parameter $class is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

34
    public function handleBooleanType(/** @scrutinizer ignore-unused */ $class, $data, $dataField, $config, $dbField)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $config is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

34
    public function handleBooleanType($class, $data, $dataField, /** @scrutinizer ignore-unused */ $config, $dbField)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $dbField is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

34
    public function handleBooleanType($class, $data, $dataField, $config, /** @scrutinizer ignore-unused */ $dbField)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
35
    {
36
        return $this->isTrue($data[$dataField]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->isTrue($data[$dataField]) also could return the type boolean which is incompatible with the documented return type integer|string.
Loading history...
37
    }
38
39
    /**
40
     * @param $val
41
     * @param bool $return_null
42
     * @return bool|mixed|null
43
     *
44
     * FROM https://www.php.net/manual/en/function.boolval.php#116547
45
     */
46
    public function isTrue($val, $return_null = false)
47
    {
48
        $boolval = (bool)$val;
49
50
        if (is_string($val)) {
51
            $boolval = filter_var($val, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
52
        }
53
54
        if ($boolval === null && !$return_null) {
55
            return false;
56
        }
57
58
        return $boolval;
59
    }
60
}
61