Issues (210)

src/AbstractVisitor.php (1 issue)

Labels
Severity
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
use JMS\Serializer\Type\Type;
11
12
/**
13
 * @internal
14
 *
15
 * @phpstan-import-type TypeArray from Type
16
 */
17 317
abstract class AbstractVisitor implements VisitorInterface
18
{
19 317
    /**
20 317
     * @var GraphNavigatorInterface|null
21
     */
22 167
    protected $navigator;
23
24 167
    public function setNavigator(GraphNavigatorInterface $navigator): void
25
    {
26
        $this->navigator = $navigator;
27 113
    }
28
29 113
    /**
30 57
     * {@inheritdoc}
31
     */
32
    public function prepare($data)
33 59
    {
34 28
        return $data;
35
    }
36 40
37
    /**
38
     * @param TypeArray $typeArray
0 ignored issues
show
The type JMS\Serializer\TypeArray was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
39
     */
40
    protected function getElementType(array $typeArray): ?array
41
    {
42
        if (false === isset($typeArray['params'][0])) {
43
            return null;
44
        }
45
46
        if (isset($typeArray['params'][1]) && \is_array($typeArray['params'][1])) {
47
            return $typeArray['params'][1];
48
        } else {
49
            return $typeArray['params'][0];
50
        }
51
    }
52
53
    /**
54
     * logic according to strval https://www.php.net/manual/en/function.strval.php
55
     * "You cannot use strval() on arrays or on objects that do not implement the __toString() method."
56
     *
57
     * @param mixed $value
58
     */
59
    protected function assertValueCanBeCastToString($value): void
60
    {
61
        if (is_array($value)) {
62
            throw new NonStringCastableTypeException($value);
63
        }
64
65
        if (is_object($value) && !method_exists($value, '__toString')) {
66
            throw new NonStringCastableTypeException($value);
67
        }
68
    }
69
70
    /**
71
     * logic according to intval https://www.php.net/manual/en/function.intval.php
72
     * "intval() should not be used on objects, as doing so will emit an E_NOTICE level error and return 1."
73
     *
74
     * @param mixed $value
75
     */
76
    protected function assertValueCanBeCastToInt($value): void
77
    {
78
        if (is_object($value) && !$value instanceof \SimpleXMLElement) {
79
            throw new NonIntCastableTypeException($value);
80
        }
81
    }
82
83
    /**
84
     *  logic according to floatval https://www.php.net/manual/en/function.floatval.php
85
     * "floatval() should not be used on objects, as doing so will emit an E_NOTICE level error and return 1."
86
     *
87
     * @param mixed $value
88
     */
89
    protected function assertValueCanCastToFloat($value): void
90
    {
91
        if (is_object($value) && !$value instanceof \SimpleXMLElement) {
92
            throw new NonFloatCastableTypeException($value);
93
        }
94
    }
95
96
    protected function mapRoundMode(?string $roundMode = null): int
97
    {
98
        switch ($roundMode) {
99
            case 'HALF_DOWN':
100
                $roundMode = PHP_ROUND_HALF_DOWN;
101
                break;
102
            case 'HALF_EVEN':
103
                $roundMode = PHP_ROUND_HALF_EVEN;
104
                break;
105
            case 'HALF_ODD':
106
                $roundMode = PHP_ROUND_HALF_ODD;
107
                break;
108
            case 'HALF_UP':
109
            default:
110
                $roundMode = PHP_ROUND_HALF_UP;
111
        }
112
113
        return $roundMode;
114
    }
115
}
116