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.

Transport   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 189
Duplicated Lines 16.93 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 17
c 5
b 0
f 0
lcom 1
cbo 5
dl 32
loc 189
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A get() 8 8 1
A post() 8 8 1
A put() 8 8 1
A delete() 8 8 1
C processResponse() 0 42 7
A getUri() 0 4 1
A getOptions() 0 11 1
A getToken() 0 4 1
A setToken() 0 6 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 prgTW\BaseCRM\Transport;
4
5
use GuzzleHttp\Exception\BadResponseException;
6
use GuzzleHttp\Exception\ClientException;
7
use GuzzleHttp\Exception\ServerException;
8
use GuzzleHttp\Message\ResponseInterface;
9
use prgTW\BaseCRM\Client\ClientInterface;
10
use prgTW\BaseCRM\Client\GuzzleClient;
11
use prgTW\BaseCRM\Exception\RepresentationErrorException;
12
use prgTW\BaseCRM\Utils\Convert;
13
14
class Transport
15
{
16
	const TOKEN_FUTUERSIMPLE_NAME = 'X-Futuresimple-Token';
17
	const TOKEN_PIPEJUMP_NAME     = 'X-Pipejump-Auth';
18
19
	/** @var ClientInterface */
20
	private $client;
21
22
	/** @var string */
23
	private $token;
24
25
	/** @var ResponseInterface */
26
	protected $lastResponse;
27
28
	/**
29
	 * @param string          $token
30
	 * @param ClientInterface $client
0 ignored issues
show
Documentation introduced by
Should the type for parameter $client not be null|ClientInterface?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
31
	 */
32
	public function __construct($token = '', ClientInterface $client = null)
33
	{
34
		$this->client       = null !== $client ? $client : new GuzzleClient;
0 ignored issues
show
Documentation Bug introduced by
It seems like null !== $client ? $clie...M\Client\GuzzleClient() can also be of type object<prgTW\BaseCRM\Client\GuzzleClient>. However, the property $client is declared as type object<prgTW\BaseCRM\Client\ClientInterface>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

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

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
35
		$this->token        = $token;
36
		$this->lastResponse = null;
37
	}
38
39
	/**
40
	 * @param string $uri
41
	 * @param string $key
0 ignored issues
show
Documentation introduced by
Should the type for parameter $key not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
42
	 * @param array  $options
43
	 *
44
	 * @return array|bool
45
	 */
46 View Code Duplication
	public function get($uri, $key = null, array $options = [])
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...
47
	{
48
		$options  = $this->getOptions($options);
49
		$response = $this->client->request('GET', $this->getUri($uri), $options);
50
		$decoded  = $this->processResponse($response, $key);
51
52
		return $decoded;
53
	}
54
55
	/**
56
	 * @param string $uri
57
	 * @param string $key
0 ignored issues
show
Documentation introduced by
Should the type for parameter $key not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
58
	 * @param array  $options
59
	 *
60
	 * @return array|bool
61
	 */
62 View Code Duplication
	public function post($uri, $key = null, array $options = [])
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...
63
	{
64
		$options  = $this->getOptions($options);
65
		$response = $this->client->request('POST', $this->getUri($uri), $options);
66
		$decoded  = $this->processResponse($response, $key);
67
68
		return $decoded;
69
	}
70
71
	/**
72
	 * @param string $uri
73
	 * @param string $key
0 ignored issues
show
Documentation introduced by
Should the type for parameter $key not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
74
	 * @param array  $options
75
	 *
76
	 * @return array|bool
77
	 */
78 View Code Duplication
	public function put($uri, $key = null, array $options = [])
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...
79
	{
80
		$options  = $this->getOptions($options);
81
		$response = $this->client->request('PUT', $this->getUri($uri), $options);
82
		$decoded  = $this->processResponse($response, $key);
83
84
		return $decoded;
85
	}
86
87
	/**
88
	 * @param string $uri
89
	 * @param string $key
0 ignored issues
show
Documentation introduced by
Should the type for parameter $key not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
90
	 * @param array  $options
91
	 *
92
	 * @return array|bool
93
	 */
94 View Code Duplication
	public function delete($uri, $key = null, array $options = [])
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...
95
	{
96
		$options  = $this->getOptions($options);
97
		$response = $this->client->request('DELETE', $this->getUri($uri), $options);
98
		$decoded  = $this->processResponse($response, $key);
99
100
		return $decoded;
101
	}
102
103
	/**
104
	 * @param ResponseInterface $response
105
	 * @param string            $key
0 ignored issues
show
Documentation introduced by
Should the type for parameter $key not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
106
	 *
107
	 * @throws RepresentationErrorException on 422 response
108
	 * @throws ClientException on 4xx errors
109
	 * @throws ServerException on 5xx errors
110
	 * @throws BadResponseException when key is not found in response data
111
	 * @return array|bool
112
	 */
113
	private function processResponse(ResponseInterface $response, $key = null)
114
	{
115
		$status = $response->getStatusCode();
116
		if (204 == $status)
117
		{
118
			return true;
119
		}
120
121
		$decoded = $response->json();
122
		$decoded = Convert::objectToArray($decoded);
123
124
		if (422 == $status)
125
		{
126
			//@codeCoverageIgnoreStart
127
			throw new RepresentationErrorException('', $this->client->getLastRequest(), $response);
0 ignored issues
show
Bug introduced by
The method getLastRequest() does not exist on prgTW\BaseCRM\Client\ClientInterface. Did you maybe mean request()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
128
			//@codeCoverageIgnoreEnd
129
		}
130
131
		if (200 <= $status && 300 > $status)
132
		{
133
			$this->lastResponse = $response;
134
135
			if (null === $key)
136
			{
137
				return $decoded;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $decoded; (array|integer|double|string|null|boolean|resource) is incompatible with the return type documented by prgTW\BaseCRM\Transport\Transport::processResponse of type array|boolean.

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...
138
			}
139
140
			if (false === array_key_exists($key, $decoded))
141
			{
142
				//@codeCoverageIgnoreStart
143
				$message = sprintf('Key "%s" not found in data', $key);
144
				throw new BadResponseException($message, $this->client->getLastRequest(), $response);
0 ignored issues
show
Bug introduced by
The method getLastRequest() does not exist on prgTW\BaseCRM\Client\ClientInterface. Did you maybe mean request()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
145
				//@codeCoverageIgnoreEnd
146
			}
147
148
			return $decoded[$key];
149
		}
150
151
		//@codeCoverageIgnoreStart
152
		throw BadResponseException::create($this->client->getLastRequest(), $response);
0 ignored issues
show
Bug introduced by
The method getLastRequest() does not exist on prgTW\BaseCRM\Client\ClientInterface. Did you maybe mean request()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
153
		//@codeCoverageIgnoreEnd
154
	}
155
156
	/**
157
	 * @param string $uri
158
	 *
159
	 * @return string
160
	 */
161
	protected function getUri($uri)
162
	{
163
		return $uri . '.json';
164
	}
165
166
	/**
167
	 * @param array $options
168
	 *
169
	 * @return array
170
	 */
171
	private function getOptions(array $options)
172
	{
173
		$options = array_merge_recursive([
174
			'headers' => [
175
				self::TOKEN_PIPEJUMP_NAME     => $this->token,
176
				self::TOKEN_FUTUERSIMPLE_NAME => $this->token,
177
			],
178
		], $options);
179
180
		return $options;
181
	}
182
183
	/**
184
	 * @return string
185
	 */
186
	public function getToken()
187
	{
188
		return $this->token;
189
	}
190
191
	/**
192
	 * @param string $token
193
	 *
194
	 * @return $this
195
	 */
196
	public function setToken($token)
197
	{
198
		$this->token = $token;
199
200
		return $this;
201
	}
202
}
203