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.

ShortcodeFacade::serialize()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 10

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 5

Importance

Changes 0
Metric Value
dl 10
loc 10
ccs 7
cts 7
cp 1
rs 9.6111
c 0
b 0
f 0
cc 5
nc 5
nop 2
crap 5
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\ParsedShortcodeInterface;
17
use Thunder\Shortcode\Shortcode\ShortcodeInterface;
18
use Thunder\Shortcode\Syntax\CommonSyntax;
19
use Thunder\Shortcode\Syntax\SyntaxInterface;
20
21
/**
22
 * @author Tomasz Kowalczyk <[email protected]>
23
 */
24
class ShortcodeFacade
25
{
26
    /** @var ProcessorInterface */
27
    private $processor;
28
    /** @var ParserInterface */
29
    private $parser;
30
    /** @var SyntaxInterface */
31
    private $syntax;
32
33
    /** @var HandlerContainer */
34
    private $handlers;
35
    /** @var EventContainer */
36
    private $events;
37
38
    /** @var SerializerInterface */
39
    private $textSerializer;
40
    /** @var SerializerInterface */
41
    private $jsonSerializer;
42
    /** @var SerializerInterface */
43
    private $xmlSerializer;
44
    /** @var SerializerInterface */
45
    private $yamlSerializer;
46
47 5
    public function __construct()
48
    {
49 5
        $this->syntax = new CommonSyntax();
50 5
        $this->handlers = new HandlerContainer();
51 5
        $this->events = new EventContainer();
52
53 5
        $this->parser = new RegularParser($this->syntax);
54 5
        $this->rebuildProcessor();
55
56 5
        $this->textSerializer = new TextSerializer();
57 5
        $this->jsonSerializer = new JsonSerializer();
58 5
        $this->yamlSerializer = new YamlSerializer();
59 5
        $this->xmlSerializer = new XmlSerializer();
60 5
    }
61
62
    /**
63
     * @deprecated use constructor and customize using exposed methods
64
     * @return self
65
     */
66 1
    public static function create(HandlerContainerInterface $handlers, SyntaxInterface $syntax)
67
    {
68 1
        $self = new self();
69
70
        /** @psalm-suppress PropertyTypeCoercion */
71 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...
72 1
        $self->syntax = $syntax;
73 1
        $self->rebuildProcessor();
74
75 1
        return $self;
76
    }
77
78
    /** @return void */
79 5
    private function rebuildProcessor()
80
    {
81 5
        $this->processor = new Processor($this->parser, $this->handlers);
82 5
        $this->processor = $this->processor->withEventContainer($this->events);
83 5
    }
84
85
    /**
86
     * @param string $text
87
     *
88
     * @return string
89
     */
90 2
    public function process($text)
91
    {
92 2
        return $this->processor->process($text);
93
    }
94
95
    /**
96
     * @param string $text
97
     *
98
     * @return ParsedShortcodeInterface[]
99
     */
100 1
    public function parse($text)
101
    {
102 1
        return $this->parser->parse($text);
103
    }
104
105
    /** @return $this */
106 1
    public function setParser(ParserInterface $parser)
107
    {
108 1
        $this->parser = $parser;
109 1
        $this->rebuildProcessor();
110
111 1
        return $this;
112
    }
113
114
    /**
115
     * @param string $name
116
     * @psalm-param callable(ShortcodeInterface):string $handler
117
     *
118
     * @return $this
119
     */
120 2
    public function addHandler($name, $handler)
121
    {
122 2
        $this->handlers->add($name, $handler);
123
124 2
        return $this;
125
    }
126
127
    /**
128
     * @param string $alias
129
     * @param string $name
130
     *
131
     * @return $this
132
     */
133 1
    public function addHandlerAlias($alias, $name)
134
    {
135 1
        $this->handlers->addAlias($alias, $name);
136
137 1
        return $this;
138
    }
139
140
    /**
141
     * @param string $name
142
     * @param callable $handler
143
     *
144
     * @return $this
145
     */
146 1
    public function addEventHandler($name, $handler)
147
    {
148 1
        $this->events->addListener($name, $handler);
149
150 1
        return $this;
151
    }
152
153
    /* --- SERIALIZATION --------------------------------------------------- */
154
155
    /**
156
     * @param string $format
157
     *
158
     * @return string
159
     */
160 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...
161
    {
162
        switch($format) {
163 2
            case 'text': return $this->textSerializer->serialize($shortcode);
164 2
            case 'json': return $this->jsonSerializer->serialize($shortcode);
165 2
            case 'yaml': return $this->yamlSerializer->serialize($shortcode);
166 2
            case 'xml': return $this->xmlSerializer->serialize($shortcode);
167 1
            default: throw new \InvalidArgumentException(sprintf('Invalid serialization format %s!', $format));
168 1
        }
169
    }
170
171
    /**
172
     * @param string $text
173
     * @param string $format
174
     *
175
     * @return ShortcodeInterface
176
     */
177 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...
178
    {
179
        switch($format) {
180 2
            case 'text': return $this->textSerializer->unserialize($text);
181 2
            case 'json': return $this->jsonSerializer->unserialize($text);
182 2
            case 'yaml': return $this->yamlSerializer->unserialize($text);
183 2
            case 'xml': return $this->xmlSerializer->unserialize($text);
184 1
            default: throw new \InvalidArgumentException(sprintf('Invalid unserialization format %s!', $format));
185 1
        }
186
    }
187
188
    /**
189
     * @deprecated use serialize($shortcode, $format)
190
     * @return string
191
     */
192
    public function serializeToText(ShortcodeInterface $s) { return $this->serialize($s, 'text'); }
193
194
    /**
195
     * @deprecated use serialize($shortcode, $format)
196
     * @return string
197
     */
198
    public function serializeToJson(ShortcodeInterface $s) { return $this->serialize($s, 'json'); }
199
200
    /**
201
     * @deprecated use serialize($shortcode, $format)
202
     * @param string $text
203
     *
204
     * @return ShortcodeInterface
205
     */
206
    public function unserializeFromText($text) { return $this->unserialize($text, 'text'); }
207
208
    /**
209
     * @deprecated use serialize($shortcode, $format)
210
     * @param string $text
211
     *
212
     * @return ShortcodeInterface
213
     */
214
    public function unserializeFromJson($text) { return $this->unserialize($text, 'json'); }
215
}
216