ListTrait   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 54
Duplicated Lines 35.19 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A listById() 9 9 1
A getById() 0 9 2
A listBy() 10 10 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 a part of nekland youtube api package
5
 *
6
 * (c) Nekland <[email protected]>
7
 *
8
 * For the full license, take a look to the LICENSE file
9
 * on the root directory of this project
10
 */
11
12
namespace Nekland\YoutubeApi\Api\Behavior;
13
14
use Nekland\YoutubeApi\Exception\NotFoundItemException;
15
16
trait ListTrait
17
{
18
    /**
19
     * @param  string $id
20
     * @param  array  $parts
21
     * @param  array  $otherParameters
22
     * @return array
23
     */
24 View Code Duplication
    public function listById($id, array $parts = ['snippet'], array $otherParameters = [])
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...
25
    {
26
        $parameters = array_merge(
27
            ['part' => implode(',', $parts), 'id' => $id],
28
            $otherParameters
29
        );
30
31
        return $this->get(static::URL, $parameters);
0 ignored issues
show
Bug introduced by
It seems like get() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
32
    }
33
34
    /**
35
     * Get automatically only the video data array
36
     *
37
     * @param  string $id
38
     * @param  array  $parts
39
     * @param  array  $otherParameters
40
     * @return array
41
     * @throws NotFoundItemException
42
     */
43
    public function getById($id, array $parts = ['snippet'], array $otherParameters = [])
44
    {
45
        $data = $this->listById($id, $parts, $otherParameters);
46
        if (empty($data['items'])) {
47
            throw new NotFoundItemException();
48
        }
49
50
        return $data['items'][0];
51
    }
52
53
    /**
54
     * @param  array $filters
55
     * @param  array $parts
56
     * @param  array $otherParameters
57
     * @return array
58
     */
59 View Code Duplication
    public function listBy(array $filters, array $parts = ['snippet'], array $otherParameters = [])
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...
60
    {
61
        $parameters = array_merge(
62
            ['part' => implode(',', $parts)],
63
            $filters,
64
            $otherParameters
65
        );
66
67
        return $this->get(static::URL, $parameters);
0 ignored issues
show
Bug introduced by
It seems like get() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
68
    }
69
}
70