Completed
Push — master ( 255087...05b7b6 )
by Rémi
05:55
created

MiniGameEventSourcedRepository::load()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace MiniGameApp\Repository\EventSourced;
4
5
use Broadway\EventSourcing\EventSourcingRepository;
6
use MiniGame\Entity\MiniGame;
7
use MiniGame\Entity\MiniGameId;
8
use MiniGameApp\Exception\GameNotFoundException;
9
use MiniGameApp\Repository\GameRepository;
10
11
class MiniGameEventSourcedRepository implements GameRepository
12
{
13
    /**
14
     * @var EventSourcingRepository
15
     */
16
    private $repository;
17
18
    /**
19
     * Constructor
20
     *
21
     * @param EventSourcingRepository $repository
22
     */
23
    public function __construct(EventSourcingRepository $repository)
24
    {
25
        $this->repository = $repository;
26
    }
27
28
    /**
29
     * Saves a mini-game
30
     *
31
     * @param  MiniGame $game
32
     * @return MiniGame
33
     */
34
    public function save(MiniGame $game)
35
    {
36
        $this->repository->save($game);
0 ignored issues
show
Documentation introduced by
$game is of type object<MiniGame\Entity\MiniGame>, but the function expects a object<Broadway\Domain\AggregateRoot>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
37
    }
38
39
    /**
40
     * Get the mini-game corresponding to the id
41
     *
42
     * @param  MiniGameId $id
43
     * @throws GameNotFoundException
44
     * @return MiniGame
45
     */
46
    public function load(MiniGameId $id)
47
    {
48
        return $this->repository->load($id);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->repository->load($id); (Broadway\EventSourcing\EventSourcedAggregateRoot) is incompatible with the return type declared by the interface MiniGameApp\Repository\GameRepository::load of type MiniGame\Entity\MiniGame.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
49
    }
50
}
51