Completed
Push — master ( d56647...5f1d83 )
by Kirill
11s
created

RoomManager   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 53
rs 10
wmc 5
lcom 0
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A register() 0 6 1
A get() 0 10 2
A all() 0 4 1
1
<?php
2
/**
3
 * This file is part of GitterBot package.
4
 *
5
 * @author Serafim <[email protected]>
6
 * @author butschster <[email protected]>
7
 * @date 09.10.2015 17:08
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
namespace Domains;
13
14
use Domains\Room\RoomInterface;
15
use Illuminate\Support\Collection;
16
use Interfaces\Gitter\StandartGitterRoom;
17
18
/**
19
 * Class RoomManager
20
 * @package Domains
21
 */
22
class RoomManager
23
{
24
    /**
25
     * @var Collection
26
     */
27
    protected $rooms;
28
29
    /**
30
     * RoomManager constructor.
31
     *
32
     * @param array $rooms
33
     */
34
    public function __construct(array $rooms = [])
0 ignored issues
show
Unused Code introduced by
The parameter $rooms is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
35
    {
36
        $this->rooms = new Collection();
37
    }
38
39
    /**
40
     * @param RoomInterface $room
41
     *
42
     * @return $this
43
     */
44
    public function register(RoomInterface $room)
45
    {
46
        $this->rooms->put($room->id(), $room);
47
48
        return $this;
49
    }
50
51
    /**
52
     * @param string $id
53
     *
54
     * @return RoomInterface|null
55
     */
56
    public function get($id)
57
    {
58
        if (is_null($room = $this->rooms->get($id))) {
59
            return $this->rooms->filter(function(RoomInterface $room) use($id) {
60
                return $room->alias() == $id;
61
            })->first();
62
        }
63
64
        return $room;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $room; (object|integer|double|string|array|boolean) is incompatible with the return type documented by Domains\RoomManager::get of type Domains\Room\RoomInterface|null.

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...
65
    }
66
67
    /**
68
     * @return array
69
     */
70
    public function all()
71
    {
72
        return $this->rooms->all();
73
    }
74
}
75