Completed
Push — master ( d4566f...31a5e9 )
by Zaahid
02:27
created

MimeLiteralPartFactory   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 13
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 1
c 1
b 0
f 1
lcom 0
cbo 1
dl 0
loc 13
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A newInstance() 0 4 1
1
<?php
2
/**
3
 * This file is part of the ZBateson\MailMimeParser project.
4
 *
5
 * @license http://opensource.org/licenses/bsd-license.php BSD
6
 */
7
namespace ZBateson\MailMimeParser\Header\Part;
8
9
/**
10
 * Extends HeaderPartFactory to instantiate MimeLiteralParts for its newInstance
11
 * function.
12
 *
13
 * @author Zaahid Bateson <[email protected]>
14
 */
15
class MimeLiteralPartFactory extends HeaderPartFactory
16
{
17
    /**
18
     * Creates and returns a MimeLiteralPart.
19
     * 
20
     * @param string $value
21
     * @return MimeLiteralPart
22
     */
23
    public function newInstance($value)
24
    {
25
        return $this->newMimeLiteralPart($value);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->newMimeLiteralPart($value); (ZBateson\MailMimeParser\...er\Part\MimeLiteralPart) is incompatible with the return type of the parent method ZBateson\MailMimeParser\...artFactory::newInstance of type ZBateson\MailMimeParser\Header\Part\Token.

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...
26
    }
27
}
28