Passed
Pull Request — master (#7)
by vincent
02:21
created

Item   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 76
Duplicated Lines 42.11 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 32
loc 76
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 2
A getPoster() 16 16 4
A getBackdrop() 16 16 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
namespace vfalies\tmdb\Items;
4
5
abstract class Item
6
{
7
    protected $data = null;
8
    protected $conf = null;
9
    protected $id   = null;
10
    protected $tmdb = null;
11
12
    /**
13
     * Constructor
14
     * @param \vfalies\tmdb\Tmdb $tmdb
15
     * @param int $item_id
16
     * @param array $options
17
     * @param string $item_name
18
     * @throws \Exception
19
     */
20 71
    public function __construct(\vfalies\tmdb\Tmdb $tmdb, int $item_id, array $options, string $item_name)
21
    {
22
        try
23
        {
24 71
            $this->id   = (int) $item_id;
25 71
            $this->tmdb = $tmdb;
26 71
            $this->conf = $this->tmdb->getConfiguration();
27 71
            $params     = $this->tmdb->checkOptions($options);
28 71
            $this->data = $this->tmdb->sendRequest(new \vfalies\tmdb\CurlRequest(), $item_name . '/'.(int) $item_id, null, $params);
29
        }
30 3
        catch (\Exception $ex)
31
        {
32 3
            throw new \Exception($ex->getMessage(), $ex->getCode(), $ex);
33
        }
34 68
    }
35
36
    /**
37
     * Get item poster
38
     * @param string $size
39
     * @return string
40
     */
41 12 View Code Duplication
    public function getPoster(string $size = 'w185'): string
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...
42
    {
43 12
        if (isset($this->data->poster_path))
44
        {
45 9
            if (!isset($this->conf->images->base_url))
46
            {
47 3
                throw new \Exception('base_url configuration not found');
48
            }
49 6
            if (!in_array($size, $this->conf->images->poster_sizes))
50
            {
51 3
                throw new \Exception('Incorrect poster size : ' . $size);
52
            }
53 3
            return $this->conf->images->base_url . $size . $this->data->poster_path;
54
        }
55 3
        return '';
56
    }
57
58
    /**
59
     * Get item backdrop
60
     * @param string $size
61
     * @return string|null
62
     */
63 12 View Code Duplication
    public function getBackdrop(string $size = 'w780'): string
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...
64
    {
65 12
        if (isset($this->data->backdrop_path))
66
        {
67 9
            if (!isset($this->conf->images->base_url))
68
            {
69 3
                throw new \Exception('base_url configuration not found');
70
            }
71 6
            if (!in_array($size, $this->conf->images->backdrop_sizes))
72
            {
73 3
                throw new \Exception('Incorrect backdrop size : ' . $size);
74
            }
75 3
            return $this->conf->images->base_url . $size . $this->data->backdrop_path;
76
        }
77 3
        return '';
78
    }
79
80
}
81