Completed
Push — master ( be855c...5f2aa4 )
by Nils
10:52
created

Retriever::getOccuredExceptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace whm\Smoke\Extensions\SmokeResponseRetriever\Retriever\Koalamon;
4
5
use GuzzleHttp\Client;
6
use phm\HttpWebdriverClient\Http\HttpClient;
7
use Psr\Http\Message\UriInterface;
8
use whm\Crawler\Http\RequestFactory;
9
use whm\Html\Uri;
10
use whm\Smoke\Extensions\SmokeResponseRetriever\Retriever\Retriever as SmokeRetriever;
11
use whm\Smoke\Scanner\SessionContainer;
12
13
class Retriever implements SmokeRetriever
14
{
15
    private $apiKey;
16
    private $systems;
17
    private $project;
18
19
    /**
20
     * @var HttpAdapterInterface
21
     */
22
    private $client;
23
24
    const ENDPOINT_SYSTEMS = 'http://www.koalamon.com/rest/#project#/systems/?api_key=#api_key#';
25
26
    public function init($apiKey, $project)
27
    {
28
        $this->apiKey = $apiKey;
29
        $this->project = $project;
30
    }
31
32
    public function setHttpClient(Client $httpClient)
33
    {
34
        $this->client = $httpClient;
0 ignored issues
show
Documentation Bug introduced by
It seems like $httpClient of type object<GuzzleHttp\Client> is incompatible with the declared type object<whm\Smoke\Extensi...n\HttpAdapterInterface> of property $client.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
35
        $this->systems = $this->getSystems($httpClient);
0 ignored issues
show
Documentation introduced by
$httpClient is of type object<GuzzleHttp\Client>, but the function expects a object<phm\HttpWebdriverClient\Http\HttpClient>.

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...
36
    }
37
38
    public function getSystems(HttpClient $httpClient)
39
    {
40
        $url = $this->prepareUrl(self::ENDPOINT_SYSTEMS);
41
42
        $systems = $httpClient->get(new Uri($url));
43
44
        return json_decode($systems->getBody(), true);
45
    }
46
47
    private function prepareUrl($url)
48
    {
49
        $preparedUrl = str_replace('#project#', $this->project, $url);
50
        $preparedUrl = str_replace('#api_key#', $this->apiKey, $preparedUrl);
51
52
        return $preparedUrl;
53
    }
54
55
    public function next()
56
    {
57
        if (empty($this->systems)) {
58
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type declared by the interface whm\Smoke\Extensions\Smo...triever\Retriever::next of type Psr\Http\Message\ResponseInterface.

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...
59
        }
60
61
        $system = array_pop($this->systems);
62
63
        $request = RequestFactory::getRequest(new Uri($system['url']), 'GET', 'php://memory', ['Accept-Encoding' => 'gzip', 'Connection' => 'keep-alive']);
64
        $responses = $this->client->sendRequests(array($request));
65
66
        return $responses[0];
67
    }
68
69
    public function getComingFrom(UriInterface $uri)
70
    {
71
        return new Uri('http://www.koalamon.com');
72
    }
73
74
    public function getOriginUri(UriInterface $uri)
75
    {
76
        return $uri;
77
    }
78
79
    public function setSessionContainer(SessionContainer $sessionContainer)
80
    {
81
    }
82
83
    public function getOccuredExceptions()
84
    {
85
        return [];
86
    }
87
}
88