Completed
Pull Request — master (#95)
by Eduardo Gulias
02:00
created

RFCValidation::isValid()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 2
eloc 9
nc 2
nop 2
1
<?php
2
3
namespace Egulias\EmailValidator\Validation;
4
5
use Egulias\EmailValidator\EmailLexer;
6
use Egulias\EmailValidator\EmailParser;
7
use Egulias\EmailValidator\Exception\InvalidEmail;
8
9
class RFCValidation implements EmailValidation
10
{
11
    private $parser;
12
    private $warnings;
13
    private $error;
14
    
15
    public function isValid($email, EmailLexer $emailLexer)
16
    {
17
        $this->parser = new EmailParser($emailLexer);
18
        try {
19
            $this->parser->parse((string)$email);
20
        } catch (InvalidEmail $invalid) {
21
            $this->error = $invalid;
22
            return false;
23
        }
24
        
25
        $this->warnings = $this->parser->getWarnings();
26
        return true;
27
    }
28
29
    public function getError()
30
    {
31
        return $this->error;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->error; (Egulias\EmailValidator\Exception\InvalidEmail) is incompatible with the return type declared by the interface Egulias\EmailValidator\V...ailValidation::getError of type Egulias\EmailValidator\V...nvalidArgumentException.

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...
32
    }
33
34
    public function getWarnings()
35
    {
36
        return $this->warnings;
37
    }
38
}
39