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.

CoinbaseClient   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 117
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B createCheckout() 0 37 4
A getHeaders() 0 11 1
1
<?php namespace Yani\Coinbase;
2
3
use Yani\Coinbase\Exceptions\CoinbaseCheckoutException;
4
use GuzzleHttp\Client as Guzzle;
5
6
class CoinbaseClient {
7
8
	/**
9
	 * The coinbase API KEY
10
	 *
11
	 * @var string
12
	 */
13
	protected $apiKey;
14
15
	/**
16
	 * The coinbase API SECRET
17
	 *
18
	 * @var string
19
	 */
20
	protected $apiSecret;
21
22
	/**
23
	 * The Guzzle HTTP client
24
	 *
25
	 * @var \GuzzleHttp\Client
26
	 */
27
	protected $client = null;
28
29
	/**
30
	 * The Coinbase endpoint
31
	 *
32
	 * @var string
33
	 */
34
	protected $endpoint = '';
35
36
	/**
37
	 * Instantiate a new client
38
	 *
39
	 * @param \GuzzleHttp\Client $client
40
	 * @param string             $apiKey
41
	 * @param string             $apiSecret
42
	 * @param string             $endpoint
43
	 */
44
	public function __construct(Guzzle $client, $apiKey, $apiSecret, $endpoint)
45
	{
46
		$this->apiKey    = $apiKey;
47
		$this->apiSecret = $apiSecret;
48
		$this->client    = $client;
49
		$this->endpoint  = $endpoint;
50
	}
51
52
	/**
53
	 * Create order with Coinbase API
54
	 *
55
	 * @param float  $amount
56
	 * @param string $currency
57
	 * @param string $name
58
	 * @param string $description
0 ignored issues
show
Bug introduced by
There is no parameter named $description. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
59
	 * @param array  $metadata
0 ignored issues
show
Bug introduced by
There is no parameter named $metadata. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
60
	 *
61
	 * @return stdClass
62
	 */
63
	public function createCheckout($amount, $currency, $name, $data = [])
64
	{
65
		$payload = [
66
			'amount' => $amount,
67
			'currency' => $currency,
68
			'name'     => $name,
69
		];
70
		foreach ($data as $key => $value)
71
		{
72
			$payload[$key] = $value;
73
		}
74
		$payload = json_encode($payload);
75
		$path    = '/v2/checkouts';
76
		$headers = $this->getHeaders(time(), 'POST', $path, $payload);
0 ignored issues
show
Documentation introduced by
$payload is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
77
78
		try
79
		{
80
			$response = $this->client->post($this->endpoint . $path, [
81
				'body'    => $payload,
82
				'headers' => $headers
83
			]);
84
		}
85
		catch (\Exception $e)
86
		{
87
			echo $e->getResponse();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Exception as the method getResponse() does only exist in the following sub-classes of Exception: GuzzleHttp\Exception\BadResponseException, GuzzleHttp\Exception\ClientException, GuzzleHttp\Exception\ConnectException, GuzzleHttp\Exception\RequestException, GuzzleHttp\Exception\ServerException, GuzzleHttp\Exception\TooManyRedirectsException. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
88
			exit(0);
0 ignored issues
show
Coding Style Compatibility introduced by
The method createCheckout() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
89
		}
90
		if ((int) $response->getStatusCode() === 201)
91
		{
92
			return json_decode($response->getBody())->data;
93
		}
94
		else
95
		{
96
			throw new CoinbaseCheckoutException($response->getBody());
97
		}
98
99
	}
100
101
	/**
102
	 * Return headers with coinbase signature
103
	 *
104
	 * @param int    $timestamp
105
	 * @param string $method
106
	 * @param string $requestPath
107
	 * @param array  $body
108
	 *
109
	 * @return array
110
	 */
111
	public function getHeaders($timestamp, $method, $requestPath, $body)
112
	{
113
		$accessSign = hash_hmac('sha256', ($timestamp . $method . $requestPath . $body), $this->apiSecret);
114
115
		return [
116
			'CB-ACCESS-KEY'       => $this->apiKey,
117
			'CB-ACCESS-SIGN'      => $accessSign,
118
			'CB-ACCESS-TIMESTAMP' => $timestamp,
119
			'CB-VERSION'          => '2015-04-08',
120
		];
121
	}
122
}
123