Failed Conditions
Pull Request — master (#1)
by Billie
02:52
created

src/YamlDecode.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Fitbug\SymfonySerializer\YamlEncoderDecoder;
4
5
use Symfony\Component\Serializer\Encoder\DecoderInterface;
6
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
7
use Symfony\Component\Yaml\Yaml;
8
9
class YamlDecode implements DecoderInterface
10
{
11
    const OPTION_EXCEPTION_ON_INVALID_TYPE = 'yaml_decode_exception_on_invalid_type';
12
    const OPTION_OBJECT                    = 'yaml_decode_object';
13
    const OPTION_OBJECT_FOR_MAP            = 'yaml_decode_object_for_map';
14
    const OPTION_DATE_TIME                 = 'yaml_decode_date_time';
15
    const SUPPORTED_ENCODING_YAML          = "yaml";
16
    /**
17
     * @var bool
18
     */
19
    private $exceptionOnInvalidType;
20
    /**
21
     * @var bool
22
     */
23
    private $object;
24
    /**
25
     * @var bool
26
     */
27
    private $objectForMap;
28
    /**
29
     * @var bool
30
     */
31
    private $dateTime;
32
33
    /**
34
     * Constructs a new YamlDecode instance.
35
     *
36
     * @param bool $exceptionOnInvalidType
37
     * @param bool $object
38
     * @param bool $objectForMap
39
     * @param bool $dateTime
40
     */
41
    public function __construct(
42
        $exceptionOnInvalidType = false,
43
        $object = false,
44
        $objectForMap = false,
45
        $dateTime = false
46
    ) {
47
        $this->exceptionOnInvalidType = $exceptionOnInvalidType;
48
        $this->object                 = $object;
49
        $this->objectForMap           = $objectForMap;
50
        $this->dateTime               = $dateTime;
51
    }
52
53
    /**
54
     * Decodes a string into PHP data.
55
     *
56
     * @param string $data    Data to decode
57
     * @param string $format  Format name
58
     * @param array  $context options that decoders have access to
59
     *
60
     * The format parameter specifies which format the data is in; valid values
61
     * depend on the specific implementation. The only format we support is 'yaml'
62
     *
63
     * @return mixed
64
     *
65
     * @throws UnexpectedValueException
66
     */
67
    public function decode($data, $format, array $context = [])
68
    {
69
        $results = null;
0 ignored issues
show
$results is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
70
        $context = $this->resolveContext($context);
71
72
        if ($this->isYamlOldStyleInterface()) {
73
            $results = Yaml::parse(
74
                $data,
75
                $context[ self::OPTION_EXCEPTION_ON_INVALID_TYPE ],
76
                $context[ self::OPTION_OBJECT ],
77
                $context[ self::OPTION_OBJECT_FOR_MAP ]
78
            );
79
        } else {
80
            $options = $this->contextToOptions($context);
81
82
            $results = Yaml::parse($data, $options);
83
        }
84
85
        return $results;
86
    }
87
88
    /**
89
     * Checks whether the deserializer can decode from given format.
90
     *
91
     * We only support yaml.
92
     *
93
     * @param string $format format name
94
     *
95
     * @return bool
96
     */
97
    public function supportsDecoding($format)
98
    {
99
        return $format == self::SUPPORTED_ENCODING_YAML;
100
    }
101
102
    /**
103
     * Merges the default options of the Yaml Decoder with the passed context.
104
     *
105
     * @param array $context
106
     *
107
     * @return array
108
     */
109
    private function resolveContext(array $context)
110
    {
111
        $defaultOptions = [
112
            self::OPTION_EXCEPTION_ON_INVALID_TYPE => $this->exceptionOnInvalidType,
113
            self::OPTION_OBJECT                    => $this->object,
114
            self::OPTION_OBJECT_FOR_MAP            => $this->objectForMap,
115
            self::OPTION_DATE_TIME                 => $this->dateTime,
116
        ];
117
118
        return array_merge($defaultOptions, $context);
119
    }
120
121
    /**
122
     * Convert the context to options understood by the parser
123
     *
124
     * @param array $options
125
     *
126
     * @return int
127
     */
128 View Code Duplication
    private function contextToOptions(array $options)
129
    {
130
        $optionToBitMap = [
131
            self::OPTION_EXCEPTION_ON_INVALID_TYPE => Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE,
132
            self::OPTION_OBJECT                    => Yaml::PARSE_OBJECT,
133
            self::OPTION_OBJECT_FOR_MAP            => Yaml::PARSE_OBJECT_FOR_MAP,
134
            self::OPTION_DATE_TIME                 => Yaml::PARSE_DATETIME,
135
        ];
136
137
        $bitMaskedOption = 0;
138
139
        foreach ($optionToBitMap as $option => $bitMask) {
140
            if ($options[ $option ]) {
141
                $bitMaskedOption = $bitMaskedOption | $bitMask;
142
            }
143
        }
144
145
        return $bitMaskedOption;
146
    }
147
148
    private function isYamlOldStyleInterface()
149
    {
150
        return !defined("Symfony\\Component\\Yaml\\Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE");
151
    }
152
}
153