Episode   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 57
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 57
loc 57
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A characters() 6 6 1
A getCharacters() 4 4 1
A extractIdsFromResponseBasedOnKey() 26 26 5

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 Zmaglica\RickAndMortyApiWrapper\Model;
4
5 View Code Duplication
class Episode extends AbstractModel
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...
6
{
7
    /**
8
     * Get all characters without running API call. Useful if you want to perform additional filtering.
9
     * @param bool $removeDuplicates
10
     * @return \Zmaglica\RickAndMortyApiWrapper\Api\AbstractApi
11
     */
12
    public function characters($removeDuplicates = false)
13
    {
14
        $ids = $this->extractIdsFromResponseBasedOnKey('characters', $removeDuplicates);
15
16
        return $this->api->character()->whereId($ids);
17
    }
18
19
    /**
20
     * Get all episode characters
21
     * @param bool $removeDuplicates
22
     * @return mixed
23
     */
24
    public function getCharacters($removeDuplicates = false)
25
    {
26
        return $this->characters($removeDuplicates)->get();
27
    }
28
29
    /**
30
     * This helper function will extract all URL ids by key. Key can be location, residence or episode
31
     * @param $key
32
     * @param $removeDuplicates
33
     * @return array
34
     */
35
    private function extractIdsFromResponseBasedOnKey($key, $removeDuplicates)
36
    {
37
        $ids = [];
38
        if ($this->info) {
39
            $urls = array_column($this->data['results'], $key);
40
            // flatten array to single one because there can be multiple residents per location
41
            $urls = array_merge(...$urls);
42
        } elseif (isset($this->data[0])) {
43
            $urls = array_column($this->data, $key);
44
            // flatten array to single one because there can be multiple residents per location
45
            $urls = array_merge(...$urls);
46
        } else {
47
            $urls = $this->data[$key];
48
        }
49
50
        if ($removeDuplicates) {
51
            $urls = array_unique($urls);
52
        }
53
54
        foreach ($urls as $url) {
55
            $ids[] = substr(strrchr($url, '/'), 1);
56
            ;
57
        }
58
59
        return $ids;
60
    }
61
}
62