Completed
Push — master ( b846df...63f83e )
by Freek
03:08
created

NewsletterListCollection::getDefault()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 2
eloc 6
nc 2
nop 0
1
<?php
2
3
namespace Spatie\Newsletter;
4
5
use Illuminate\Support\Collection;
6
use Spatie\Newsletter\Exceptions\InvalidNewsletterList;
7
8
class NewsletterListCollection extends Collection
9
{
10
    /** @var string */
11
    public $defaultListName = '';
12
13
    /**
14
     * @param array $config
15
     *
16
     * @return static
17
     */
18
    public static function createFromConfig(array $config)
19
    {
20
        $collection = new static();
21
22
        foreach ($config['lists'] as $name => $listProperties) {
23
            $collection->push(new NewsletterList($name, $listProperties));
24
        }
25
26
        $collection->defaultListName = $config['defaultListName'];
27
28
        return $collection;
29
    }
30
31
    /**
32
     * @param string $name
33
     *
34
     * @return \Spatie\Newsletter\NewsletterList
35
     * 
36
     * @throws \Spatie\Newsletter\Exceptions\InvalidNewsletterList
37
     */
38
    public function findByName($name)
39
    {
40
        if ((string)$name === '') {
41
            return $this->getDefault();
42
        }
43
44
        $list = $this->first(function ($index, NewsletterList $newletterList) use ($name) {
45
            return $newletterList->getName() === $name;
46
        });
47
48
        if (is_null($list)) {
49
            throw InvalidNewsletterList::noListWithName($name);
50
        }
51
52
        return $list;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $list; (object|integer|double|string|array|boolean) is incompatible with the return type documented by Spatie\Newsletter\Newsle...tCollection::findByName of type Spatie\Newsletter\NewsletterList.

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...
53
    }
54
55
    /**
56
     * @return \Spatie\Newsletter\NewsletterList
57
     *
58
     * @throws \Spatie\Newsletter\Exceptions\InvalidNewsletterList
59
     */
60
    public function getDefault()
61
    {
62
        $defaultList = $this->first(function ($index, NewsletterList $newletterList) {
63
            return $newletterList->getName() === $this->defaultListName;
64
        });
65
66
        if (is_null($defaultList)) {
67
            throw InvalidNewsletterList::defaultListDoesNotExist($this->defaultListName);
68
        }
69
70
        return $defaultList;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $defaultList; (object|integer|double|string|array|boolean) is incompatible with the return type documented by Spatie\Newsletter\Newsle...tCollection::getDefault of type Spatie\Newsletter\NewsletterList.

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...
71
    }
72
}
73