Completed
Push — master ( 3431c4...f44a02 )
by Eugene
07:11
created

PurePacker::unpack()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 21
ccs 8
cts 8
cp 1
rs 8.7624
cc 5
eloc 13
nc 6
nop 1
crap 5
1
<?php
2
3
namespace Tarantool\Client\Packer;
4
5
use MessagePack\BufferUnpacker;
6
use MessagePack\Packer;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Tarantool\Client\Packer\Packer.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
7
use Tarantool\Client\Exception\Exception;
8
use Tarantool\Client\IProto;
9
use Tarantool\Client\Packer\Packer as ClientPacker;
10
use Tarantool\Client\Request\Request;
11
use Tarantool\Client\Response;
12
13
class PurePacker implements ClientPacker
14
{
15
    private $packer;
16
    private $unpacker;
17 59
18
    public function __construct(Packer $packer = null, BufferUnpacker $unpacker = null)
19 59
    {
20 59
        $this->packer = $packer ?: new Packer();
21 59
        $this->unpacker = $unpacker ?: new BufferUnpacker();
22
    }
23 113
24
    public function pack(Request $request, $sync = null)
25 113
    {
26 113
        $content = $this->packer->packMapHeader(2).
27 113
            $this->packer->packInt(IProto::CODE).
28
            $this->packer->packInt($request->getType()).
29
            $this->packer->packInt(IProto::SYNC).
30 113
            $this->packer->packInt($sync);
31 100
32
        if (null !== $body = $request->getBody()) {
33
            $content .= $this->packer->packMap($body);
34 113
        }
35
36
        return PackUtils::packLength(strlen($content)).$content;
37 108
    }
38
39 108
    public function unpack($data)
40
    {
41 108
        $this->unpacker->reset($data);
42 2
43
        try {
44
            $header = $this->unpacker->unpackMap();
45 106
            $body = $this->unpacker->unpackMap();
46
        } catch (\Exception $e) {
47 106
            throw new Exception('Unable to unpack data.', 0, $e);
48
        } catch (\Throwable $e) {
0 ignored issues
show
Bug introduced by
The class Throwable does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
49 106
            throw new Exception('Unable to unpack data.', 0, $e);
50 23
        }
51
52
        $code = $header[IProto::CODE];
53 86
54
        if ($code >= Response::TYPE_ERROR) {
55
            throw new Exception($body[IProto::ERROR], $code & (Response::TYPE_ERROR - 1));
56
        }
57
58
        return new Response($header[IProto::SYNC], $body ? $body[IProto::DATA] : null);
59
    }
60
}
61