Completed
Pull Request — master (#27)
by ARCANEDEV
03:58
created

OldPasswordRule::message()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php namespace Arcanesoft\Auth\Rules;
2
3
use Illuminate\Contracts\Auth\Authenticatable;
4
use Illuminate\Contracts\Validation\Rule;
5
use Illuminate\Support\Facades\Hash;
6
7
/**
8
 * Class     OldPasswordRule
9
 *
10
 * @package  Arcanesoft\Auth\Rules
11
 * @author   ARCANEDEV <[email protected]>
12
 */
13
class OldPasswordRule implements Rule
14
{
15
    /* -----------------------------------------------------------------
16
     |  Properties
17
     | -----------------------------------------------------------------
18
     */
19
20
    /** @var  \Illuminate\Contracts\Auth\Authenticatable */
21
    protected $user;
22
23
    /* -----------------------------------------------------------------
24
     |  Constructor
25
     | -----------------------------------------------------------------
26
     */
27
28
    /**
29
     * OldPasswordRule constructor.
30
     *
31
     * @param  \Illuminate\Contracts\Auth\Authenticatable  $user
32
     */
33
    public function __construct(Authenticatable $user)
34
    {
35
        $this->user = $user;
36
    }
37
38
    /* -----------------------------------------------------------------
39
     |  Main Methods
40
     | -----------------------------------------------------------------
41
     */
42
43
    /**
44
     * Determine if the validation rule passes.
45
     *
46
     * @param  string  $attribute
47
     * @param  mixed   $value
48
     *
49
     * @return bool
50
     */
51
    public function passes($attribute, $value)
52
    {
53
        return Hash::check($value, $this->user->getAuthPassword());
54
    }
55
56
    /**
57
     * Get the validation error message.
58
     *
59
     * @return string
60
     */
61
    public function message()
62
    {
63
        return trans('auth::validation.user_password');
0 ignored issues
show
Bug Best Practice introduced by
The return type of return trans('auth::validation.user_password'); (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...
64
    }
65
}
66