ReactFactory   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 24
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 0
dl 24
loc 24
ccs 0
cts 7
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 1
A createClient() 8 8 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Http\HttplugBundle\ClientFactory;
6
7
use Http\Adapter\React\Client;
8
use Http\Message\MessageFactory;
9
10
/**
11
 * @author Tobias Nyholm <[email protected]>
12
 */
13 View Code Duplication
class ReactFactory implements ClientFactory
14
{
15
    /**
16
     * @var MessageFactory
17
     */
18
    private $messageFactory;
19
20
    public function __construct(MessageFactory $messageFactory)
21
    {
22
        $this->messageFactory = $messageFactory;
23
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function createClient(array $config = [])
29
    {
30
        if (!class_exists('Http\Adapter\React\Client')) {
31
            throw new \LogicException('To use the React adapter you need to install the "php-http/react-adapter" package.');
32
        }
33
34
        return new Client($this->messageFactory);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Http\Adapter...$this->messageFactory); (Http\Adapter\React\Client) is incompatible with the return type declared by the interface Http\HttplugBundle\Clien...ntFactory::createClient of type Http\Client\HttpClient.

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...
35
    }
36
}
37