Completed
Push — master ( 00fe51...4ef654 )
by ARCANEDEV
15s queued 11s
created

StoreManager::registerStore()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
cc 1
nc 1
nop 2
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php namespace Arcanedev\Notify;
2
3
use Illuminate\Support\Manager;
4
use Arcanedev\Notify\Contracts\StoreManager as StoreManagerContract;
5
6
/**
7
 * Class     StoreManager
8
 *
9
 * @package  Arcanedev\Notify
10
 * @author   ARCANEDEV <[email protected]>
11
 */
12
class StoreManager extends Manager implements StoreManagerContract
13
{
14
    /* -----------------------------------------------------------------
15
     |  Main Methods
16
     | -----------------------------------------------------------------
17
     */
18
19
    /**
20
     * Get the default driver name.
21
     *
22
     * @return string
23
     */
24 24
    public function getDefaultDriver(): string
25
    {
26 24
        return $this->config->get('notify.default', 'session');
27
    }
28
29
    /**
30
     * Register multiple stores.
31
     *
32
     * @param  array  $stores
33
     *
34
     * @return $this
35
     */
36 24
    public function registerStores(array $stores): StoreManagerContract
37
    {
38 24
        foreach ($stores as $driver => $store) {
39 24
            $this->registerStore($driver, $store);
40
        }
41
42 24
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Arcanedev\Notify\StoreManager) is incompatible with the return type declared by the interface Arcanedev\Notify\Contrac...Manager::registerStores of type self.

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...
43
    }
44
45
    /**
46
     * Register a store.
47
     *
48
     * @param  string  $driver
49
     * @param  array   $store
50
     *
51
     * @return self
52
     */
53 24
    public function registerStore(string $driver, array $store): StoreManagerContract
54
    {
55
        return $this->extend($driver, function () use ($store) {
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->extend($dr...ons'] ?? array())); }); (Arcanedev\Notify\StoreManager) is incompatible with the return type declared by the interface Arcanedev\Notify\Contrac...eManager::registerStore of type self.

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...
56 24
            return $this->container->make($store['class'], [
57 24
                'options' => $store['options'] ?? [],
58
            ]);
59 24
        });
60
    }
61
}
62