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
Push — master ( f252bb...862be0 )
by Sergey
10:05
created

JsonApiRegistry   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 103
Duplicated Lines 43.69 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 14
c 1
b 0
f 1
lcom 2
cbo 0
dl 45
loc 103
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A registerDecoder() 0 13 3
B getDecoder() 23 23 4
A registerEncoder() 0 13 3
B getEncoder() 22 22 4

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
/*
3
 * This file is part of the reva2/jsonapi.
4
 *
5
 * (c) OrbitScripts LLC <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
12
namespace Reva2\JsonApi\Services;
13
14
use Neomerx\JsonApi\Contracts\Decoder\DecoderInterface;
15
use Neomerx\JsonApi\Contracts\Encoder\EncoderInterface;
16
use Reva2\JsonApi\Contracts\Services\JsonApiRegistryInterface;
17
18
/**
19
 * JSON API decoders/encoders registry
20
 *
21
 * @package Reva2\JsonApi\Services
22
 * @author Sergey Revenko <[email protected]>
23
 */
24
class JsonApiRegistry implements JsonApiRegistryInterface
25
{
26
    /**
27
     * Decoders map
28
     *
29
     * @var array
30
     */
31
    protected $decoders = [];
32
33
    /**
34
     * Encoders map
35
     *
36
     * @var array
37
     */
38
    protected $encoders = [];
39
40
    /**
41
     * @inheritdoc
42
     */
43
    public function registerDecoder($name, $decoder)
44
    {
45
        if ((!$decoder instanceof DecoderInterface) && (!$decoder instanceof \Closure)) {
46
            throw new \InvalidArgumentException(sprintf(
47
                "Decoder must be a %s instance or closure",
48
                DecoderInterface::class
49
            ));
50
        }
51
52
        $this->decoders[$name] = $decoder;
53
54
        return $this;
55
    }
56
57
    /**
58
     * @inheritdoc
59
     */
60 View Code Duplication
    public function getDecoder($name)
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...
61
    {
62
        if (!array_key_exists($name, $this->decoders)) {
63
            throw new \RuntimeException(sprintf("Decoder with name '%s' is not registered", $name));
64
        }
65
66
        if ($this->decoders[$name] instanceof \Closure) {
67
            $factory = $this->decoders[$name];
68
            $decoder = $factory();
69
            if (!$decoder instanceof DecoderInterface) {
70
                throw new \LogicException(sprintf(
71
                    "Decoder '%s' should implement %s interface",
72
                    $name,
73
                    DecoderInterface::class
74
                ));
75
            }
76
77
            $this->decoders[$name] = $decoder;
78
79
        }
80
81
        return $this->decoders[$name];
82
    }
83
84
    /**
85
     * @inheritdoc
86
     */
87
    public function registerEncoder($name, $encoder)
88
    {
89
        if ((!$encoder instanceof EncoderInterface) && (!$encoder instanceof \Closure)) {
90
            throw new \InvalidArgumentException(sprintf(
91
                "Encoder must be a %s instance or closure",
92
                EncoderInterface::class
93
            ));
94
        }
95
96
        $this->encoders[$name] = $encoder;
97
98
        return $this;
99
    }
100
101
    /**
102
     * @inheritdoc
103
     */
104 View Code Duplication
    public function getEncoder($name)
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...
105
    {
106
        if (!array_key_exists($name, $this->encoders)) {
107
            throw new \RuntimeException(sprintf("Encoder with name '%s' is not registered", $name));
108
        }
109
110
        if ($this->encoders[$name] instanceof \Closure) {
111
            $factory = $this->encoders[$name];
112
            $encoder = $factory();
113
            if (!$encoder instanceof EncoderInterface) {
114
                throw new \LogicException(sprintf(
115
                    "Encoder '%s' should implement %s interface",
116
                    $name,
117
                    EncoderInterface::class
118
                ));
119
            }
120
121
            $this->encoders[$name] = $encoder;
122
        }
123
124
        return $this->encoders[$name];
125
    }
126
}