Issues (2)

src/Api/Command/TransformBoard/Command.php (1 issue)

1
<?php
2
/**
3
 *
4
 * This file is part of the Aggrego.
5
 * (c) Tomasz Kunicki <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 */
11
12
declare(strict_types = 1);
13
14
namespace Aggrego\Domain\Api\Command\TransformBoard;
15
16
use Aggrego\CommandConsumer\Command as ConsumerCommand;
17
use Aggrego\CommandConsumer\Name;
18
use Aggrego\CommandConsumer\Uuid;
19
use Aggrego\Domain\Board\Key;
20
use Aggrego\Domain\Board\Uuid as BoardUuid;
21
use Assert\Assertion;
22
23
class Command implements ConsumerCommand
24
{
25
    public const NAME = 'Aggrego/Domain/TransformBoard';
26
27
    /**
28
     * @var Uuid
29
     */
30
    private $uuid;
31
32
    /**
33
     * @var BoardUuid
34
     */
35
    private $boardUuid;
36
    /**
37
     * @var Key
38
     */
39
    private $key;
40
41
    public function __construct(string $uuid, string $boardUuid, array $key)
42
    {
43
        $this->uuid = new Uuid($uuid);
44
        $this->boardUuid = new BoardUuid($boardUuid);
45
        $this->key = new Key($key);
46
    }
47
48
    public function getBoardUuid(): BoardUuid
49
    {
50
        return $this->boardUuid;
51
    }
52
53
    public function getKey(): Key
54
    {
55
        return $this->key;
56
    }
57
58
    public function getName(): Name
59
    {
60
        return new Name(self::NAME);
61
    }
62
63
    public function getUuid(): Uuid
64
    {
65
        return $this->uuid;
66
    }
67
68
    public function serialize()
69
    {
70
        return json_encode([
71
            'uuid' => $this->getUuid()->getValue(),
72
            'name' => $this->getName()->getValue(),
73
            'key' => $this->key->getValue(),
74
            'board_uuid' => $this->boardUuid->getValue(),
75
        ]);
76
    }
77
78
    public function unserialize($serialized): self
79
    {
80
        $json = json_decode($serialized, true);
81
        Assertion::keyExists($json, 'uuid');
82
        Assertion::keyExists($json, 'name');
83
        Assertion::eq($json['name'], self::NAME);
84
        Assertion::keyExists($json, 'key');
85
        Assertion::keyExists($json, 'board_uuid');
86
87
        $this->uuid = new Uuid($json['uuid']);
88
        $this->boardUuid = new BoardUuid($json['board_uuid']);
89
        $this->key = new Key($json['key']);
90
91
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Aggrego\Domain\Api\Command\TransformBoard\Command which is incompatible with the return type mandated by Serializable::unserialize() of void.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
92
    }
93
}
94