Completed
Pull Request — master (#8)
by
unknown
02:39
created

ActivityRepository::findActivityById()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of the xAPI package.
5
 *
6
 * (c) Christian Flothmann <[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
namespace XApi\Repository\Doctrine\Repository;
13
14
use Xabbuh\XApi\Common\Exception\NotFoundException;
15
use Xabbuh\XApi\Model\IRI;
16
use XApi\Repository\Api\ActivityRepositoryInterface;
17
use XApi\Repository\Doctrine\Mapping\Object as MappedObject;
18
use XApi\Repository\Doctrine\Storage\ObjectStorage;
19
20
/**
21
 * Doctrine based {@link Activity} repository.
22
 *
23
 * @author Jérôme Parmentier <[email protected]>
24
 */
25
final class ActivityRepository implements ActivityRepositoryInterface
26
{
27
    private $storage;
28
29
    public function __construct(ObjectStorage $storage)
30
    {
31
        $this->storage = $storage;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function findActivityById(IRI $activityId)
38
    {
39
        $criteria = array(
40
            'type' => MappedObject::TYPE_ACTIVITY,
41
            'activityId' => $activityId->getValue(),
42
        );
43
44
        $activity = $this->storage->findObject($criteria);
45
46
        if (null === $activity) {
47
            throw new NotFoundException('No activity could be found matching the given criteria.');
48
        }
49
50
        return $activity->getModel();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $activity->getModel(); (Xabbuh\XApi\Model\Group|...buh\XApi\Model\Activity) is incompatible with the return type declared by the interface XApi\Repository\Api\Acti...rface::findActivityById of type Xabbuh\XApi\Model\Activity.

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...
51
    }
52
}
53