Completed
Pull Request — master (#1167)
by Nik
10:38
created

AbstractVisitor::assertValueCanCastToFloat()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 2
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 4
rs 10
ccs 0
cts 0
cp 0
crap 12
1
<?php
2
3
declare(strict_types=1);
4
5
namespace JMS\Serializer;
6
7
use JMS\Serializer\Exception\NonFloatCastableTypeException;
8
use JMS\Serializer\Exception\NonIntCastableTypeException;
9
use JMS\Serializer\Exception\NonStringCastableTypeException;
10
11
/**
12
 * @internal
13
 */
14
abstract class AbstractVisitor implements VisitorInterface
15
{
16
    /**
17 317
     * @var GraphNavigatorInterface
18
     */
19 317
    protected $navigator;
20 317
21
    public function setNavigator(GraphNavigatorInterface $navigator): void
22 167
    {
23
        $this->navigator = $navigator;
24 167
    }
25
26
    /**
27 113
     * {@inheritdoc}
28
     */
29 113
    public function prepare($data)
30 57
    {
31
        return $data;
32
    }
33 59
34 28
    protected function getElementType(array $typeArray): ?array
35
    {
36 40
        if (false === isset($typeArray['params'][0])) {
37
            return null;
38
        }
39
40
        if (isset($typeArray['params'][1]) && \is_array($typeArray['params'][1])) {
41
            return $typeArray['params'][1];
42
        } else {
43
            return $typeArray['params'][0];
44
        }
45
    }
46
47
    /**
48
     * logic according to strval https://www.php.net/manual/en/function.strval.php
49
     * "You cannot use strval() on arrays or on objects that do not implement the __toString() method."
50
     *
51
     * @param mixed $value
52
     */
53
    protected function assertValueCanBeCastToString($value): void
54
    {
55
        if (is_array($value)) {
56
            throw new NonStringCastableTypeException($value);
57
        }
58
59
        if (is_object($value) && !method_exists($value, '__toString')) {
60
            throw new NonStringCastableTypeException($value);
61
        }
62
    }
63
64
    /**
65
     * logic according to intval https://www.php.net/manual/en/function.intval.php
66
     * "intval() should not be used on objects, as doing so will emit an E_NOTICE level error and return 1."
67
     *
68
     * @param mixed $value
69
     */
70
    protected function assertValueCanBeCastToInt($value): void
71
    {
72
        if (is_object($value) && !$value instanceof \SimpleXMLElement) {
73
            throw new NonIntCastableTypeException($value);
74
        }
75
    }
76
77
    /**
78
     *  logic according to floatval https://www.php.net/manual/en/function.floatval.php
79
     * "floatval() should not be used on objects, as doing so will emit an E_NOTICE level error and return 1."
80
     *
81
     * @param mixed $value
82
     */
83
    protected function assertValueCanCastToFloat($value): void
84
    {
85
        if (is_object($value) && !$value instanceof \SimpleXMLElement) {
86
            throw new NonFloatCastableTypeException($value);
87
        }
88
    }
89
}
90