Completed
Pull Request — master (#48)
by Sullivan
05:32
created

BaseUriPlugin::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Http\Client\Common\Plugin;
4
5
use Http\Client\Common\Plugin;
6
use Psr\Http\Message\RequestInterface;
7
use Psr\Http\Message\UriInterface;
8
9
/**
10
 * This plugin combines the host and path plugins.
11
 *
12
 * @author Sullivan Senechal <[email protected]>
13
 */
14
final class BaseUriPlugin implements Plugin
15
{
16
    /**
17
     * @var AddHostPlugin
18
     */
19
    private $addHostPlugin;
20
21
    /**
22
     * @var AddPathPlugin
23
     */
24
    private $addPathPlugin = null;
25
26
    /**
27
     * @param UriInterface $host
28
     * @param array        $hostConfig Config for AddHostPlugin. @see AddHostPlugin::configureOptions
29
     */
30 4
    public function __construct(UriInterface $host, array $hostConfig = [])
31
    {
32 4
        $this->addHostPlugin = new AddHostPlugin($host, $hostConfig);
33
34 4
        if ($host->getPath()) {
35 4
            $this->addPathPlugin = new AddPathPlugin($host);
36 4
        }
37 4
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function handleRequest(RequestInterface $request, callable $next, callable $first)
43
    {
44 2
        $addHostNext = function (RequestInterface $request) use ($next, $first) {
45 2
            return $this->addHostPlugin->handleRequest($request, $next, $first);
46 2
        };
47
48 2
        if ($this->addPathPlugin) {
49 2
            return $this->addPathPlugin->handleRequest($request, $addHostNext, $first);
50
        }
51
52
        return $addHostNext;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $addHostNext; (Closure) is incompatible with the return type declared by the interface Http\Client\Common\Plugin::handleRequest of type Http\Promise\Promise.

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...
53
    }
54
}
55