Passed
Push — master ( 2cf866...4f18eb )
by Doug
06:30 queued 03:02
created

RemoteXdebug::nameAndVersion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
/**
5
 * Code Coverage Driver.
6
 *
7
 * @copyright 2013 Anthon Pang
8
 *
9
 * @license BSD-2-Clause
10
 */
11
12
namespace DVDoug\Behat\CodeCoverage\Driver;
13
14
use GuzzleHttp\Client;
15
use Psr\Http\Message\ResponseInterface;
16
use SebastianBergmann\CodeCoverage\Driver\Driver;
17
use SebastianBergmann\CodeCoverage\RawCodeCoverageData;
18
19
/**
20
 * Remote xdebug driver.
21
 *
22
 * @author Anthon Pang <[email protected]>
23
 */
24
class RemoteXdebug extends Driver
25
{
26
    /**
27
     * @var array
28
     */
29
    private $config;
30
31
    /**
32
     * @var GuzzleHttp\Client
0 ignored issues
show
Bug introduced by
The type DVDoug\Behat\CodeCoverage\Driver\GuzzleHttp\Client was not found. Did you mean GuzzleHttp\Client? If so, make sure to prefix the type with \.
Loading history...
33
     */
34
    private $client;
35
36
    /**
37
     * Constructor.
38
     *
39
     * [
40
     *     'base_uri' => 'http://api.example.com/1.0/coverage',
41
     *     'auth'     => [
42
     *                       'user'     => 'user name',
43
     *                       'password' => 'password',
44
     *                   ],
45
     *     'create'   => [
46
     *                       'method' => 'POST',
47
     *                       'path'   => '/',
48
     *                   ],
49
     *     'read'     => [
50
     *                       'method' => 'GET',
51
     *                       'path'   => '/',
52
     *                   ],
53
     *     'delete'   => [
54
     *                       'method' => 'DELETE',
55
     *                       'path'   => '/',
56
     *                   ],
57
     * ]
58
     *
59
     * @param array             $config Configuration
60
     * @param GuzzleHttp\Client $client HTTP client
61
     */
62 10
    public function __construct(array $config, Client $client)
63
    {
64 10
        $this->config = $config;
65
66 10
        $this->client = $client;
0 ignored issues
show
Documentation Bug introduced by
It seems like $client of type GuzzleHttp\Client is incompatible with the declared type DVDoug\Behat\CodeCoverage\Driver\GuzzleHttp\Client 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...
67
        //$this->client->setBaseUrl($config['base_url']);
68 5
    }
69
70 6
    public function start(bool $determineUnusedAndDead = true): void
71
    {
72 6
        $response = $this->sendRequest('create');
73
74 4
        if ($response->getStatusCode() !== 200) {
75 4
            throw new \Exception(sprintf('remote driver start failed: %s', $response->getReasonPhrase()));
76
        }
77
    }
78
79 4
    public function stop(): RawCodeCoverageData
80
    {
81 4
        $response = $this->sendRequest('read', ['headers' => ['Accept' => 'application/json']]);
82
83 4
        if ($response->getStatusCode() !== 200) {
84 4
            throw new \Exception(sprintf('remote driver fetch failed: %s', $response->getReasonPhrase()));
85
        }
86
87
        $this->sendRequest('delete');
88
89
        return RawCodeCoverageData::fromXdebugWithoutPathCoverage(json_decode($response->getBody(true), true));
0 ignored issues
show
Unused Code introduced by
The call to Psr\Http\Message\MessageInterface::getBody() has too many arguments starting with true. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

89
        return RawCodeCoverageData::fromXdebugWithoutPathCoverage(json_decode($response->/** @scrutinizer ignore-call */ getBody(true), true));

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
90
    }
91
92 10
    private function sendRequest(string $endpoint, array $headers = []): ResponseInterface
93
    {
94 10
        $method = strtolower($this->config[$endpoint]['method']);
95
96 10
        if (!in_array($method, ['get', 'post', 'put', 'delete'])) {
97 2
            throw new \Exception(sprintf('%s method must be GET, POST, PUT, or DELETE', $endpoint));
98
        }
99
100 8
        if (isset($this->config['auth'])) {
101 8
            $response = $this->client->request(
102 8
                $method,
103 8
                $this->config[$endpoint]['path'],
104
                [
105 8
                    'auth' => [$this->config['auth']['user'], $this->config['auth']['password']],
106 8
                    'headers' => $headers,
107
                ]
108
            );
109
        } else {
110
            $response = $this->client->request(
111
                $method,
112
                $this->config[$endpoint]['path'],
113
                [
114
                    'headers' => $headers,
115
                ]
116
            );
117
        }
118
119 8
        return $response;
120
    }
121
122
    public function nameAndVersion(): string
123
    {
124
        return 'Remote Xdebug';
125
    }
126
}
127