Completed
Pull Request — master (#9)
by ARCANEDEV
13:07
created

ChangeFrequency::keys()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 12
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 0
ccs 9
cts 9
cp 1
crap 1
rs 9.4285
1
<?php namespace Arcanedev\LaravelSitemap\Entities;
2
3
use Arcanedev\LaravelSitemap\Contracts\Entities\ChangeFrequency as ChangeFrequencyContract;
4
use Illuminate\Support\Collection;
5
6
/**
7
 * Class     ChangeFrequency
8
 *
9
 * @package  Arcanedev\LaravelSitemap\Entities
10
 * @author   ARCANEDEV <[email protected]>
11
 */
12
class ChangeFrequency implements ChangeFrequencyContract
13
{
14
    /* -----------------------------------------------------------------
15
     |  Main Methods
16
     | -----------------------------------------------------------------
17
     */
18
19
    /**
20
     * Get all the valid frequency keys.
21
     *
22
     * @return \Illuminate\Support\Collection
23
     */
24 10
    public static function keys()
25
    {
26 10
        return new Collection([
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Illuminate\S...EARLY, static::NEVER)); (Illuminate\Support\Collection) is incompatible with the return type declared by the interface Arcanedev\LaravelSitemap...s\ChangeFrequency::keys of type array.

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...
27 10
            static::ALWAYS,
28 10
            static::HOURLY,
29 10
            static::DAILY,
30 10
            static::WEEKLY,
31 10
            static::MONTHLY,
32 10
            static::YEARLY,
33 10
            static::NEVER,
34
        ]);
35
    }
36
37
    /**
38
     * Get all the valid frequency values.
39
     *
40
     * @param  string|null $locale
41
     *
42
     * @return \Illuminate\Support\Collection
43
     */
44
    public static function all($locale = null)
45
    {
46 6
        return static::keys()->mapWithKeys(function ($key) use ($locale) {
0 ignored issues
show
Bug Best Practice introduced by
The return type of return static::keys()->m...array(), $locale)); }); (Illuminate\Support\Collection) is incompatible with the return type declared by the interface Arcanedev\LaravelSitemap...es\ChangeFrequency::all of type array.

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...
47 6
            return [$key => trans("sitemap::frequencies.$key", [], $locale)];
48 6
        });
49
    }
50
51
    /**
52
     * Get the translated frequency name.
53
     *
54
     * @param  string       $key
55
     * @param  string|null  $default
56
     * @param  string|null  $locale
57
     *
58
     * @return string|null
59
     */
60 2
    public static function get($key, $default = null, $locale = null)
61
    {
62 2
        return static::all($locale)->get($key, $default);
63
    }
64
65
    /**
66
     * Check if the given frequency exists.
67
     *
68
     * @param  string $key
69
     *
70
     * @return bool
71
     */
72 2
    public static function has($key)
73
    {
74 2
        return static::keys()->flip()->has($key);
75
    }
76
}
77