This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
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\EncoderInterface; |
||
6 | use Symfony\Component\Serializer\Encoder\scalar; |
||
7 | use Symfony\Component\Serializer\Exception\UnexpectedValueException; |
||
8 | use Symfony\Component\Yaml\Yaml; |
||
9 | |||
10 | class YamlEncode implements EncoderInterface |
||
11 | { |
||
12 | const OPTION_OBJECT = 'yaml_encode_object'; |
||
13 | const OPTION_EXCEPTION_ON_INVALID_TYPE = 'yaml_encode_exception_on_invalid_type'; |
||
14 | const OPTION_OBJECT_FOR_MAP = 'yaml_encode_object_for_map'; |
||
15 | const OPTION_MULTI_LINE_LITERAL_BLOCK = 'yaml_encode_multi_line_literal_block'; |
||
16 | const OPTION_INLINE = 'yaml_encode_inline'; |
||
17 | const OPTION_INDENT = 'yaml_encode_indent'; |
||
18 | const SUPPORTED_ENCODING_YAML = 'yaml'; |
||
19 | |||
20 | /** |
||
21 | * @var bool |
||
22 | */ |
||
23 | private $multiLineLiteralBlock; |
||
24 | |||
25 | /** |
||
26 | * @var bool |
||
27 | */ |
||
28 | private $exceptionOnInvalidType; |
||
29 | |||
30 | /** |
||
31 | * @var bool |
||
32 | */ |
||
33 | private $objectForMap; |
||
34 | |||
35 | /** |
||
36 | * @var bool |
||
37 | */ |
||
38 | private $object; |
||
39 | /** |
||
40 | * @var int |
||
41 | */ |
||
42 | private $indent; |
||
43 | /** |
||
44 | * @var int |
||
45 | */ |
||
46 | private $inline; |
||
47 | |||
48 | /** |
||
49 | * Constructs a new YamlDecode instance. |
||
50 | * |
||
51 | * @param bool $object |
||
52 | * @param bool $exceptionOnInvalidType |
||
53 | * @param bool $objectForMap |
||
54 | * @param bool $multiLineLiteralBlock |
||
55 | * @param int $inline |
||
56 | * @param int $indent |
||
57 | */ |
||
58 | public function __construct( |
||
59 | $object = false, |
||
60 | $exceptionOnInvalidType = false, |
||
61 | $objectForMap = false, |
||
62 | $multiLineLiteralBlock = false, |
||
63 | $inline = 2, |
||
64 | $indent = 2 |
||
65 | ) { |
||
66 | $this->object = $object; |
||
67 | $this->exceptionOnInvalidType = $exceptionOnInvalidType; |
||
68 | $this->objectForMap = $objectForMap; |
||
69 | $this->multiLineLiteralBlock = $multiLineLiteralBlock; |
||
70 | $this->indent = $indent; |
||
71 | |||
72 | $this->inline = $inline; |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Encodes data into the given format. |
||
77 | * |
||
78 | * The only supported is yaml |
||
79 | * |
||
80 | * @param mixed $data Data to encode |
||
81 | * @param string $format Format name |
||
82 | * @param array $context options that normalizers/encoders have access to |
||
83 | * |
||
84 | * @return string |
||
85 | * |
||
86 | * @throws UnexpectedValueException |
||
87 | */ |
||
88 | public function encode($data, $format, array $context = []) |
||
89 | { |
||
90 | $context = $this->resolveContext($context); |
||
91 | |||
92 | if ($this->isYamlOldStyleInterface()) { |
||
93 | $encodedData = Yaml::dump( |
||
94 | $data, |
||
95 | $context[ self::OPTION_INLINE ], |
||
96 | $context[ self::OPTION_INDENT ], |
||
97 | $context[ self::OPTION_EXCEPTION_ON_INVALID_TYPE ], |
||
98 | $context[ self::OPTION_OBJECT ] |
||
99 | ); |
||
100 | |||
101 | } else { |
||
102 | $options = $this->contextToOptions($context); |
||
103 | |||
104 | $encodedData = Yaml::dump( |
||
105 | $data, |
||
106 | $context[ self::OPTION_INLINE ], |
||
107 | $context[ self::OPTION_INDENT ], |
||
108 | $options |
||
109 | ); |
||
110 | } |
||
111 | |||
112 | return $encodedData; |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Merges the default options of the Yaml Encoder with the passed context. |
||
117 | * |
||
118 | * @param array $context |
||
119 | * |
||
120 | * @return array |
||
121 | */ |
||
122 | private function resolveContext(array $context) |
||
123 | { |
||
124 | $defaultOptions = [ |
||
125 | self::OPTION_OBJECT => $this->object, |
||
126 | self::OPTION_EXCEPTION_ON_INVALID_TYPE => $this->exceptionOnInvalidType, |
||
127 | self::OPTION_OBJECT_FOR_MAP => $this->objectForMap, |
||
128 | self::OPTION_MULTI_LINE_LITERAL_BLOCK => $this->multiLineLiteralBlock, |
||
129 | self::OPTION_INLINE => $this->inline, |
||
130 | self::OPTION_INDENT => $this->indent, |
||
131 | ]; |
||
132 | |||
133 | return array_merge($defaultOptions, $context); |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * Convert the context to options understood by the parser |
||
138 | * |
||
139 | * @param array $options |
||
140 | * |
||
141 | * @return int |
||
142 | */ |
||
143 | View Code Duplication | private function contextToOptions(array $options) |
|
0 ignored issues
–
show
|
|||
144 | { |
||
145 | $optionToBitMap = [ |
||
146 | self::OPTION_OBJECT => Yaml::DUMP_OBJECT, |
||
147 | self::OPTION_EXCEPTION_ON_INVALID_TYPE => Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE, |
||
148 | self::OPTION_OBJECT_FOR_MAP => Yaml::DUMP_OBJECT_AS_MAP, |
||
149 | self::OPTION_MULTI_LINE_LITERAL_BLOCK => Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK, |
||
150 | ]; |
||
151 | |||
152 | $bitMaskedOption = 0; |
||
153 | |||
154 | foreach ($optionToBitMap as $option => $bitMask) { |
||
155 | if ($options[ $option ]) { |
||
156 | $bitMaskedOption = $bitMaskedOption | $bitMask; |
||
157 | } |
||
158 | } |
||
159 | |||
160 | return $bitMaskedOption; |
||
161 | } |
||
162 | |||
163 | |||
164 | /** |
||
165 | * Checks whether the serializer can encode to given format. |
||
166 | * |
||
167 | * The only supported format is yaml |
||
168 | * |
||
169 | * @param string $format format name |
||
170 | * |
||
171 | * @return bool |
||
172 | */ |
||
173 | public function supportsEncoding($format) |
||
174 | { |
||
175 | return $format == self::SUPPORTED_ENCODING_YAML; |
||
176 | } |
||
177 | |||
178 | private function isYamlOldStyleInterface() |
||
179 | { |
||
180 | return !defined("Symfony\\Component\\Yaml\\Yaml::DUMP_OBJECT"); |
||
181 | } |
||
182 | } |
||
183 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.