Passed
Push — master ( 67aa47...d37a32 )
by vincent
02:27
created

Genres   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 89
Duplicated Lines 24.72 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 22
loc 89
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getMovieList() 10 10 2
A getTVList() 10 10 2
A getList() 0 19 3
A genreItemGenerator() 0 7 2

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\Catalogs;
4
5
use vfalies\tmdb\Interfaces\GenresInterface;
6
use vfalies\tmdb\Tmdb;
7
use vfalies\tmdb\lib\CurlRequest;
8
9
class Genres implements GenresInterface
10
{
11
12
    protected $tmdb = null;
13
14
    /**
15
     * Constructor
16
     * @param \vfalies\tmdb\Tmdb $tmdb
17
     * @throws Exception
18
     */
19 5
    public function __construct(Tmdb $tmdb)
20
    {
21 5
        $this->tmdb = $tmdb;
22 5
    }
23
24
    /**
25
     * Get movie genres list
26
     * @param array $options
27
     * @return \Generator
28
     * @throws \Exception
29
     */
30 3 View Code Duplication
    public function getMovieList(array $options = array()): \Generator
31
    {
32
        try
33
        {            
34 3
            return $this->getList('genre/movie/list', $options);
35 1
        } catch (\Exception $ex)
36
        {
37 1
            throw new \Exception($ex->getMessage(), $ex->getCode(), $ex);
38
        }
39
    }
40
41
    /**
42
     * Get TV genres list
43
     * @param array $options
44
     * @return \Generator
45
     * @throws \Exception
46
     */
47 2 View Code Duplication
    public function getTVList(array $options = array()): \Generator
48
    {
49
        try
50
        {
51 2
            return $this->getList('genre/tv/list', $options);
52 1
        } catch (\Exception $ex)
53
        {
54 1
            throw new \Exception($ex->getMessage(), $ex->getCode(), $ex);
55
        }
56
    }
57
58
    /**
59
     * Generic getter list
60
     * @param string $type
61
     * @param array $options
62
     * @return \Generator
63
     * @throws \Exception
64
     */
65 5
    private function getList(string $type, array $options): \Generator
66
    {
67
        try
68
        {
69 5
            $params   = $this->tmdb->checkOptions($options);
70 5
            $response = $this->tmdb->sendRequest(new CurlRequest(), $type, null, $params);
71
72 3
            $genres = [];
73 3
            if (isset($response->genres))
74
            {
75 2
                $genres = $response->genres;
76
            }
77
78 3
            return $this->genreItemGenerator($genres);
79 2
        } catch (\Exception $ex)
80
        {
81 2
            throw new \Exception($ex->getMessage(), $ex->getCode(), $ex);
82
        }
83
    }
84
85
    /**
86
     * Genre Item generator method
87
     * @param array $results
88
     */
89 3
    private function genreItemGenerator(array $results): \Generator
90
    {
91 3
        foreach ($results as $result)
92
        {
93 2
            yield $result;
94
        }
95 1
    }
96
97
}
98