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.
Completed
Pull Request — master (#36)
by Tomasz
02:11
created

ShortcodeFacade   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 124
Duplicated Lines 16.13 %

Coupling/Cohesion

Components 2
Dependencies 12

Test Coverage

Coverage 100%

Importance

Changes 11
Bugs 2 Features 2
Metric Value
wmc 23
c 11
b 2
f 2
lcom 2
cbo 12
dl 20
loc 124
ccs 52
cts 52
cp 1
rs 10

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
A create() 0 10 1
A rebuildProcessor() 0 5 1
A process() 0 4 1
A parse() 0 4 1
A setParser() 0 7 1
A addHandler() 0 6 1
A addHandlerAlias() 0 4 1
A addEventHandler() 0 6 1
B serialize() 10 10 5
B unserialize() 10 10 5
A serializeToText() 0 1 1
A serializeToJson() 0 1 1
A unserializeFromText() 0 1 1
A unserializeFromJson() 0 1 1

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;
3
4
use Thunder\Shortcode\EventContainer\EventContainer;
5
use Thunder\Shortcode\HandlerContainer\HandlerContainer;
6
use Thunder\Shortcode\HandlerContainer\HandlerContainerInterface;
7
use Thunder\Shortcode\Parser\ParserInterface;
8
use Thunder\Shortcode\Parser\RegularParser;
9
use Thunder\Shortcode\Processor\Processor;
10
use Thunder\Shortcode\Processor\ProcessorInterface;
11
use Thunder\Shortcode\Serializer\JsonSerializer;
12
use Thunder\Shortcode\Serializer\SerializerInterface;
13
use Thunder\Shortcode\Serializer\TextSerializer;
14
use Thunder\Shortcode\Serializer\XmlSerializer;
15
use Thunder\Shortcode\Serializer\YamlSerializer;
16
use Thunder\Shortcode\Shortcode\ShortcodeInterface;
17
use Thunder\Shortcode\Syntax\CommonSyntax;
18
use Thunder\Shortcode\Syntax\SyntaxInterface;
19
20
/**
21
 * @author Tomasz Kowalczyk <[email protected]>
22
 */
23
class ShortcodeFacade
24
{
25
    /** @var ProcessorInterface */
26
    private $processor;
27
    /** @var ParserInterface */
28
    private $parser;
29
    /** @var SyntaxInterface */
30
    private $syntax;
31
32
    /** @var HandlerContainer */
33
    private $handlers;
34
    /** @var EventContainer */
35
    private $events;
36
37
    /** @var SerializerInterface */
38
    private $textSerializer;
39
    /** @var SerializerInterface */
40
    private $jsonSerializer;
41
    /** @var SerializerInterface */
42
    private $xmlSerializer;
43
    /** @var SerializerInterface */
44
    private $yamlSerializer;
45
46 5
    public function __construct()
47
    {
48 5
        $this->syntax = new CommonSyntax();
49 5
        $this->handlers = new HandlerContainer();
50 5
        $this->events = new EventContainer();
51
52 5
        $this->parser = new RegularParser($this->syntax);
53 5
        $this->rebuildProcessor();
54
55 5
        $this->textSerializer = new TextSerializer();
56 5
        $this->jsonSerializer = new JsonSerializer();
57 5
        $this->yamlSerializer = new YamlSerializer();
58 5
        $this->xmlSerializer = new XmlSerializer();
59 5
    }
60
61
    /** @deprecated use constructor and customize using exposed methods */
62 1
    public static function create(HandlerContainerInterface $handlers, SyntaxInterface $syntax)
63
    {
64 1
        $self = new self();
65
66 1
        $self->handlers = $handlers;
0 ignored issues
show
Documentation Bug introduced by
$handlers is of type object<Thunder\Shortcode...dlerContainerInterface>, but the property $handlers was declared to be of type object<Thunder\Shortcode...ainer\HandlerContainer>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
67 1
        $self->syntax = $syntax;
68 1
        $self->rebuildProcessor();
69
70 1
        return $self;
71
    }
72
73 5
    private function rebuildProcessor()
74
    {
75 5
        $this->processor = new Processor($this->parser, $this->handlers);
76 5
        $this->processor = $this->processor->withEventContainer($this->events);
77 5
    }
78
79 2
    public function process($text)
80
    {
81 2
        return $this->processor->process($text);
82
    }
83
84 1
    public function parse($text)
85
    {
86 1
        return $this->parser->parse($text);
87
    }
88
89 1
    public function setParser(ParserInterface $parser)
90
    {
91 1
        $this->parser = $parser;
92 1
        $this->rebuildProcessor();
93
94 1
        return $this;
95
    }
96
97 2
    public function addHandler($name, $handler)
98
    {
99 2
        $this->handlers->add($name, $handler);
100
101 2
        return $this;
102
    }
103
104 1
    public function addHandlerAlias($alias, $name)
105
    {
106 1
        $this->handlers->addAlias($alias, $name);
107 1
    }
108
109 1
    public function addEventHandler($name, $handler)
110
    {
111 1
        $this->events->addListener($name, $handler);
112
113 1
        return $this;
114
    }
115
116 2 View Code Duplication
    public function serialize(ShortcodeInterface $shortcode, $format)
0 ignored issues
show
Duplication introduced by
This method 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...
117
    {
118
        switch($format) {
119 2
            case 'text': return $this->textSerializer->serialize($shortcode);
120 2
            case 'json': return $this->jsonSerializer->serialize($shortcode);
121 2
            case 'yaml': return $this->yamlSerializer->serialize($shortcode);
122 2
            case 'xml': return $this->xmlSerializer->serialize($shortcode);
123 1
            default: throw new \InvalidArgumentException(sprintf('Invalid serialization format %s!', $format));
124 1
        }
125
    }
126
127 2 View Code Duplication
    public function unserialize($text, $format)
0 ignored issues
show
Duplication introduced by
This method 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...
128
    {
129
        switch($format) {
130 2
            case 'text': return $this->textSerializer->unserialize($text);
131 2
            case 'json': return $this->jsonSerializer->unserialize($text);
132 2
            case 'yaml': return $this->yamlSerializer->unserialize($text);
133 2
            case 'xml': return $this->xmlSerializer->unserialize($text);
134 1
            default: throw new \InvalidArgumentException(sprintf('Invalid unserialization format %s!', $format));
135 1
        }
136
    }
137
138
    /** @deprecated use serialize() */
139
    public function serializeToText(ShortcodeInterface $s) { return $this->serialize($s, 'text'); }
140
    /** @deprecated use serialize() */
141
    public function serializeToJson(ShortcodeInterface $s) { return $this->serialize($s, 'json'); }
142
    /** @deprecated use unserialize() */
143
    public function unserializeFromText($text) { return $this->unserialize($text, 'text'); }
144
    /** @deprecated use unserialize() */
145
    public function unserializeFromJson($text) { return $this->unserialize($text, 'json'); }
146
}
147