Completed
Push — master ( 9cb95e...d5860f )
by Rémi
06:09
created

ApplicationUserEventSourcedRepository::save()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace MessageApp\User\Repository\EventSourced;
4
5
use Broadway\EventSourcing\EventSourcingRepository;
6
use MessageApp\User\ApplicationUser;
7
use MessageApp\User\ApplicationUserId;
8
use MessageApp\User\Exception\AppUserException;
9
use MessageApp\User\Repository\ApplicationUserRepository;
10
11
class ApplicationUserEventSourcedRepository implements ApplicationUserRepository
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  ApplicationUser $user
32
     * @return void
33
     */
34
    public function save(ApplicationUser $user)
35
    {
36
        $this->repository->save($user);
0 ignored issues
show
Documentation introduced by
$user is of type object<MessageApp\User\ApplicationUser>, 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 user corresponding to the id
41
     *
42
     * @param  ApplicationUserId $id
43
     * @throws AppUserException
44
     * @return ApplicationUser
45
     */
46
    public function load(ApplicationUserId $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 MessageApp\User\Reposito...ionUserRepository::load of type MessageApp\User\ApplicationUser.

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