Completed
Pull Request — master (#59)
by Tobias
10:05 queued 01:51
created

CurlFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 2
1
<?php
2
3
namespace Http\HttplugBundle\ClientFactory;
4
5
use Http\Client\Curl\Client;
6
use Http\Message\MessageFactory;
7
use Http\Message\StreamFactory;
8
9
/**
10
 * @author Tobias Nyholm <[email protected]>
11
 */
12
class CurlFactory implements ClientFactory
13
{
14
    /**
15
     * @var MessageFactory
16
     */
17
    private $messageFactory;
18
19
    /**
20
     * @var StreamFactory
21
     */
22
    private $streamFactory;
23
24
    /**
25
     * @param MessageFactory $messageFactory
26
     * @param StreamFactory  $streamFactory
27
     */
28
    public function __construct(MessageFactory $messageFactory, StreamFactory $streamFactory)
29
    {
30
        $this->messageFactory = $messageFactory;
31
        $this->streamFactory = $streamFactory;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function createClient(array $config = [])
38
    {
39
        if (!class_exists('Http\Client\Curl\Client')) {
40
            throw new \LogicException('To use the Curl client you need to install the "php-http/curl-client" package.');
41
        }
42
43
        return new Client($this->messageFactory, $this->streamFactory, $config);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Http\Client\...treamFactory, $config); (Http\Client\Curl\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...
44
    }
45
}
46