Completed
Push — master ( 56b616...185f8b )
by Dmitry
02:45
created

Client::resetDisabled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Tarantool\Mapper;
4
5
use Exception;
6
use Tarantool\Client\Connection\Connection;
7
use Tarantool\Client\Client as TarantoolClient;
8
use Tarantool\Client\Request\Request;
9
10
class Client extends TarantoolClient
11
{
12
    private $logging = false;
13
    private $log = [];
14 64
15
    private $disabledRequests = [];
16 64
17
    public function connect()
18 64
    {
19 64
        $start = microtime(1);
20
        $result = parent::connect();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $result is correct as parent::connect() (which targets Tarantool\Client\Client::connect()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
21 64
        $this->log($start, Connection::class);
22
        return $result;
23
    }
24 64
25
    public function disableRequest($class)
26 64
    {
27 64
        if (!is_subclass_of($class, Request::class)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \Tarantool\Client\Request\Request::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
28
            throw new Exception("Invalid request type: " . $class);
29 64
        }
30
        $this->disabledRequests[] = $class;
31 64
    }
32
33
    public function resetDisabled()
34 64
    {
35
        $this->disabledRequests = [];
36 64
    }
37 64
38
    public function sendRequest(Request $request)
39 64
    {
40
        if (in_array(get_class($request), $this->disabledRequests)) {
41 1
            throw new Exception(get_class($request) . ' is disabled');
42
        }
43 1
        $start = microtime(1);
44 1
        $response = parent::sendRequest($request);
45
        $this->log($start, get_class($request), $request->getBody(), $response->getData());
0 ignored issues
show
Bug introduced by
It seems like $response->getData() targeting Tarantool\Client\Response::getData() can also be of type null; however, Tarantool\Mapper\Client::log() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
46 4
        return $response;
47
    }
48 4
49
    private function log($start, $class, $request = [], $response = [])
50
    {
51
        if ($this->logging) {
52
            $this->log[] = [microtime(1) - $start, $class, $request, $response];
53
        }
54
    }
55
56
    public function getTimeSummary()
57
    {
58
        $summary = 0;
59
        foreach ($this->log as $request) {
60
            $summary += $request[0];
61
        }
62
        return $summary;
63
    }
64
65
    public function setLogging($logging)
66
    {
67
        $this->logging = $logging;
68
    }
69
70
    public function getLog()
71
    {
72
        return $this->log;
73
    }
74
}
75