Completed
Pull Request — master (#9)
by Jean-Baptiste
02:39
created

Guzzle6::setDefaultAuth()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
dl 9
loc 9
rs 9.6666
c 0
b 0
f 0
nc 2
cc 2
eloc 5
nop 3
1
<?php
2
3
namespace CanalTP\AbstractGuzzle\Version;
4
5
use GuzzleHttp\Psr7\Request;
6
use GuzzleHttp\Client;
7
use CanalTP\AbstractGuzzle\Guzzle;
8
9
class Guzzle6 extends Guzzle
10
{
11
    /**
12
     * @var Client
13
     */
14
    private $client;
15
16
    /**
17
     * {@InheritDoc}
18
     */
19 View Code Duplication
    public function __construct($baseUri, $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...
20
    {
21
        $this->defaultOptions = array_merge([
22
            'base_uri' => $baseUri,
23
            'http_errors' => false
24
        ], $options);
25
26
        $this->client = new Client($this->defaultOptions);
27
    }
28
29
    public function setBaseUri($baseUri)
30
    {
31
        $this->setDefaultOptions(array_merge($this->defaultOptions, ['base_uri' => $baseUri]));
32
    }
33
34
    public function getBaseUri()
35
    {
36
        return $this->client->getConfig('base_uri');
0 ignored issues
show
Bug introduced by
The method getConfig() does not seem to exist on object<GuzzleHttp\Client>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
37
    }
38
39
    public function setDefaultOptions($options = [])
40
    {
41
        $this->__construct($this->getBaseUri(), $options);
42
    }
43
44
    public function getDefaultOptions()
45
    {
46
        return $this->client->getConfig();
0 ignored issues
show
Bug introduced by
The method getConfig() does not seem to exist on object<GuzzleHttp\Client>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
47
    }
48
49 View Code Duplication
    public function setDefaultAuth($username, $password, $type = 'basic')
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...
50
    {
51
        $auth = [$username, $password];
52
        if ($type !== 'basic') {
53
            $auth[] = $type;
54
        }
55
56
        $this->setDefaultAuth('auth', $auth);
0 ignored issues
show
Documentation introduced by
$auth is of type array<integer,string>, but the function expects a string.

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...
57
    }
58
59
60
    /**
61
     * @return Client
62
     */
63
    public function getClient()
64
    {
65
        return $this->client;
66
    }
67
68
    /**
69
     * @param Client $client
70
     *
71
     * @return self
72
     */
73
    public function setClient(Client $client)
74
    {
75
        $this->client = $client;
76
77
        return $this;
78
    }
79
80
    /**
81
     * {@InheritDoc}
82
     */
83
    public function send(Request $request)
84
    {
85
        return $this->client->send($request);
0 ignored issues
show
Documentation introduced by
$request is of type object<GuzzleHttp\Psr7\Request>, but the function expects a object<GuzzleHttp\Message\RequestInterface>.

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...
Bug Best Practice introduced by
The return type of return $this->client->send($request); (GuzzleHttp\Message\ResponseInterface) is incompatible with the return type declared by the abstract method CanalTP\AbstractGuzzle\Guzzle::send of type GuzzleHttp\Psr7\Response.

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...
86
    }
87
}
88