GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

JsonSerializer   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 43
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 2
dl 43
loc 43
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A serialize() 9 9 1
B unserialize() 25 25 8

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

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.

Loading history...
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