AbstractColorRule   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 2
cbo 0
dl 0
loc 63
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A passes() 0 4 1
A message() 0 4 1
A matchAll() 0 4 1
1
<?php namespace Arcanedev\Color\Rules;
2
3
use Illuminate\Contracts\Validation\Rule;
4
5
/**
6
 * Class     AbstractColorRule
7
 *
8
 * @package  Arcanedev\Color\Rules
9
 * @author   ARCANEDEV <[email protected]>
10
 */
11
abstract class AbstractColorRule implements Rule
12
{
13
    /* -----------------------------------------------------------------
14
     |  Properties
15
     | -----------------------------------------------------------------
16
     */
17
18
    /** @var string */
19
    protected $key     = '';
20
21
    /**
22
     * @var string
23
     *
24
     * @link  http://stackoverflow.com/questions/12385500/regex-pattern-for-rgb-rgba-hsl-hsla-color-coding
25
     */
26
    protected $pattern = '';
27
28
    /* -----------------------------------------------------------------
29
     |  Main Methods
30
     | -----------------------------------------------------------------
31
     */
32
33
    /**
34
     * Determine if the validation rule passes.
35
     *
36
     * @param  string  $attribute
37
     * @param  mixed   $value
38
     *
39
     * @return bool
40
     */
41 45
    public function passes($attribute, $value)
42
    {
43 45
        return $this->matchAll($this->pattern, $value);
44
    }
45
46
    /**
47
     * Get the validation error message.
48
     *
49
     * @return string
50
     */
51 30
    public function message()
52
    {
53 30
        return trans("color::validation.{$this->key}.invalid");
0 ignored issues
show
Bug Best Practice introduced by
The return type of return trans("color::val...{$this->key}.invalid"); (Illuminate\Contracts\Tra...lator|string|array|null) is incompatible with the return type declared by the interface Illuminate\Contracts\Validation\Rule::message of type string.

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...
54
    }
55
56
    /* -----------------------------------------------------------------
57
     |  Other Methods
58
     | -----------------------------------------------------------------
59
     */
60
61
    /**
62
     * Check if match all.
63
     *
64
     * @param  string  $pattern
65
     * @param  string  $value
66
     *
67
     * @return bool
68
     */
69 45
    protected function matchAll($pattern, $value)
70
    {
71 45
        return preg_match_all($pattern, $value) !== 0;
72
    }
73
}
74