Completed
Push — master ( ba7f7c...c7ee1c )
by Eugene
10:02
created

ErrorExtension   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 38
rs 10
c 0
b 0
f 0
wmc 5
lcom 0
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getType() 0 4 1
A pack() 0 17 3
A unpackExt() 0 4 1
1
<?php
2
3
/**
4
 * This file is part of the Tarantool Client package.
5
 *
6
 * (c) Eugene Leonovich <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Tarantool\Client\Packer\Extension;
15
16
use MessagePack\BufferUnpacker;
17
use MessagePack\Packer;
18
use MessagePack\TypeTransformer\Extension;
19
use Tarantool\Client\Error;
20
use Tarantool\Client\Keys;
21
22
final class ErrorExtension implements Extension
23
{
24
    private const TYPE = 3;
25
26
    public function getType() : int
27
    {
28
        return self::TYPE;
29
    }
30
31
    /**
32
     * @param object $value
33
     */
34
    public function pack(Packer $packer, $value) : ?string
35
    {
36
        if (!$value instanceof Error) {
37
            return null;
38
        }
39
40
        [Keys::ERROR_STACK => $errorStack] = $value->toMap();
0 ignored issues
show
Bug introduced by
The variable $errorStack does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
41
42
        $packedError = $packer->packMapHeader(1);
43
        $packedError .= $packer->packInt(Keys::ERROR_STACK);
44
        $packedError .= $packer->packArrayHeader(\count($errorStack));
45
        foreach ($errorStack as $error) {
46
            $packedError .= $packer->packMap($error);
47
        }
48
49
        return $packer->packExt(self::TYPE, $packedError);
50
    }
51
52
    /**
53
     * @return Error
54
     */
55
    public function unpackExt(BufferUnpacker $unpacker, int $extLength)
56
    {
57
        return Error::fromMap($unpacker->unpackMap());
58
    }
59
}
60