DecoderProvider   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 45
loc 45
ccs 0
cts 15
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 1
A getDecoder() 8 8 2
A supports() 4 4 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
3
/*
4
 * This file is part of the AMFWebServicesClientBundle package.
5
 *
6
 * (c) Amine Fattouch <http://github.com/fattouchsquall>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace AMF\WebServicesClientBundle\Rest\Decoder;
13
14
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
15
16
/**
17
 * This is the container provider for the decoder.
18
 *
19
 * @author Mohamed Amine Fattouch <[email protected]>
20
 */
21 View Code Duplication
class DecoderProvider extends ContainerAwareTrait implements DecoderProviderInterface
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...
22
{
23
    /**
24
     * @var array
25
     */
26
    protected $decoders;
27
28
    /**
29
     * The constructor class.
30
     *
31
     * @param array $decoders List of decoders (default empty).
32
     */
33
    public function __construct(array $decoders = [])
34
    {
35
        $this->decoders = $decoders;
36
    }
37
38
    /**
39
     * @param string $format The format of decoder.
40
     *
41
     * @throws \InvalidArgumentException
42
     *
43
     * @return DecoderInterface
44
     */
45
    public function getDecoder($format)
46
    {
47
        if ($this->supports($format) === false) {
48
            throw new \InvalidArgumentException(sprintf("Format '%s' is not supported.", $format));
49
        }
50
51
        return $this->container->get($this->decoders[$format]);
0 ignored issues
show
Bug introduced by
The property container does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
52
    }
53
54
    /**
55
     * Tests whether a decoder for a given format exists.
56
     *
57
     * @param string $format The format.
58
     *
59
     * @return boolean
60
     */
61
    protected function supports($format)
62
    {
63
        return isset($this->decoders[$format]);
64
    }
65
}
66