GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

WebTranslateItRepository   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 97
Duplicated Lines 16.49 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 16
loc 97
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A pullProject() 0 9 1
A pullFile() 0 8 1
A getPullProjectUrl() 8 8 1
A getPullFileUrl() 8 8 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
namespace Ozean12\WebTranslateItBundle\Service;
4
5
use GuzzleHttp\Client;
6
use JMS\Serializer\SerializerInterface;
7
use Ozean12\WebTranslateItBundle\DTO\PullProjectResponseDTO;
8
9
/**
10
 * Class WebTranslateItRepository.
11
 */
12
class WebTranslateItRepository implements TranslationRepositoryInterface
13
{
14
    const FORMAT_JSON = 'json';
15
    const PULL_PROJECT_URL_PATTERN = '{base_url}projects/{token}.{format}';
16
    const PULL_FILE_URL_PATTERN = '{base_url}projects/{token}/files/...?file_path={name}';
17
18
    /**
19
     * @var string
20
     */
21
    private $readKey;
22
23
    /**
24
     * @var string
25
     */
26
    private $baseUrl;
27
28
    /**
29
     * @var SerializerInterface
30
     */
31
    private $serializer;
32
33
    /**
34
     * @var Client
35
     */
36
    private $client;
37
38
    /**
39
     * WebTranslateItRepository constructor.
40
     *
41
     * @param string              $readKey
42
     * @param string              $baseUrl
43
     * @param Client              $client
44
     * @param SerializerInterface $serializer
45
     */
46
    public function __construct(
47
        $readKey,
48
        $baseUrl,
49
        Client $client,
50
        SerializerInterface $serializer
51
    ) {
52
        $this->readKey = $readKey;
53
        $this->baseUrl = $baseUrl;
54
        $this->serializer = $serializer;
55
        $this->client = $client;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function pullProject()
62
    {
63
        $url = $this->getPullProjectUrl();
64
65
        $response = $this->client->get($url);
66
        $content = (string) $response->getBody();
67
68
        return $this->serializer->deserialize($content, PullProjectResponseDTO::class, self::FORMAT_JSON);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->serializer...ss, self::FORMAT_JSON); (object|array|integer|double|string|boolean) is incompatible with the return type declared by the interface Ozean12\WebTranslateItBu...yInterface::pullProject of type Ozean12\WebTranslateItBu...\PullProjectResponseDTO.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function pullFile($name)
75
    {
76
        $url = $this->getPullFileUrl($name);
77
78
        $response = $this->client->get($url);
79
80
        return (string) $response->getBody();
81
    }
82
83
    /**
84
     * @return string
85
     */
86 View Code Duplication
    private function getPullProjectUrl()
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...
87
    {
88
        return str_replace(
89
            ['{base_url}', '{token}', '{format}'],
90
            [$this->baseUrl, $this->readKey, self::FORMAT_JSON],
91
            self::PULL_PROJECT_URL_PATTERN
92
        );
93
    }
94
95
    /**
96
     * @param string $name
97
     *
98
     * @return string
99
     */
100 View Code Duplication
    private function getPullFileUrl($name)
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...
101
    {
102
        return str_replace(
103
            ['{base_url}', '{token}', '{name}'],
104
            [$this->baseUrl, $this->readKey, $name],
105
            self::PULL_FILE_URL_PATTERN
106
        );
107
    }
108
}
109