| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace Thunder\Shortcode\Serializer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use Thunder\Shortcode\Shortcode\Shortcode; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Thunder\Shortcode\Shortcode\ShortcodeInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @author Tomasz Kowalczyk <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 10 |  | View Code Duplication | final class JsonSerializer implements SerializerInterface | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 11 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 12 | 9 |  |     public function serialize(ShortcodeInterface $shortcode) | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 14 | 9 |  |         return json_encode(array( | 
            
                                                                        
                            
            
                                    
            
            
                | 15 | 9 |  |             'name' => $shortcode->getName(), | 
            
                                                                        
                            
            
                                    
            
            
                | 16 | 9 |  |             'parameters' => $shortcode->getParameters(), | 
            
                                                                        
                            
            
                                    
            
            
                | 17 | 9 |  |             'content' => $shortcode->getContent(), | 
            
                                                                        
                            
            
                                    
            
            
                | 18 | 9 |  |             'bbCode' => $shortcode->getBbCode(), | 
            
                                                                        
                            
            
                                    
            
            
                | 19 | 9 |  |         )); | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @param string $text | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @return Shortcode | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 15 |  |     public function unserialize($text) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         /** @psalm-var array{name:string,parameters:array<string,string|null>,bbCode:string|null,content:string|null}|null $data */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 15 |  |         $data = json_decode($text, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 15 |  |         if (!is_array($data)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 1 |  |             throw new \InvalidArgumentException('Invalid JSON, cannot unserialize Shortcode!'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 14 |  |         if (!array_diff_key($data, array('name', 'parameters', 'content'))) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 1 |  |             throw new \InvalidArgumentException('Malformed Shortcode JSON, expected name, parameters, and content!'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         /** @var string $name */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 13 |  |         $name = array_key_exists('name', $data) ? $data['name'] : null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 13 |  |         $parameters = array_key_exists('parameters', $data) ? $data['parameters'] : array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 13 |  |         $content = array_key_exists('content', $data) ? $data['content'] : null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 13 |  |         $bbCode = array_key_exists('bbCode', $data) ? $data['bbCode'] : null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         /** @psalm-suppress DocblockTypeContradiction */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 13 |  |         if(!is_array($parameters)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 1 |  |             throw new \InvalidArgumentException('Parameters must be an array!'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 12 |  |         return new Shortcode($name, $parameters, $content, $bbCode); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 52 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 53 |  |  |  | 
            
                        
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.