Completed
Pull Request — master (#147)
by Joel
06:25 queued 03:27
created

ContainerManager   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 30
Bugs 4 Features 17
Metric Value
wmc 7
c 30
b 4
f 17
lcom 1
cbo 7
dl 0
loc 63
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A attach() 0 12 4
B attachWebsocket() 0 35 3
1
<?php
2
3
namespace Docker\Manager;
4
5
use Docker\API\Resource\ContainerResource;
6
use Docker\Stream\AttachWebsocketStream;
7
use Docker\Stream\DockerRawStream;
8
use Joli\Jane\Swagger\Client\QueryParam;
9
10
class ContainerManager extends ContainerResource
11
{
12
    const FETCH_STREAM = 'stream';
13
14
    /**
15
     * {@inheritdoc}
16
     *
17
     * @return \Psr\Http\Message\ResponseInterface|DockerRawStream
18
     */
19
    public function attach($id, $parameters = [], $fetch = self::FETCH_OBJECT)
20
    {
21
        $response = parent::attach($id, $parameters, $fetch);
22
23
        if ($response->getStatusCode() == 200 && DockerRawStream::HEADER == $response->getHeaderLine('Content-Type')) {
24
            if ($fetch == self::FETCH_STREAM) {
25
                return new DockerRawStream($response->getBody());
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Docker\Strea...($response->getBody()); (Docker\Stream\DockerRawStream) is incompatible with the return type of the parent method Docker\API\Resource\ContainerResource::attach of type Psr\Http\Message\ResponseInterface.

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
29
        return $response;
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     *
35
     * @return \Psr\Http\Message\ResponseInterface|AttachWebsocketStream
36
     */
37
    public function attachWebsocket($id, $parameters = [], $fetch = self::FETCH_OBJECT)
38
    {
39
        $queryParam = new QueryParam();
40
        $queryParam->setDefault('logs', null);
41
        $queryParam->setDefault('stream', null);
42
        $queryParam->setDefault('stdin', null);
43
        $queryParam->setDefault('stdout', null);
44
        $queryParam->setDefault('stderr', null);
45
46
        $url      = '/containers/{id}/attach/ws';
47
        $url      = str_replace('{id}', $id, $url);
48
        $url      = $url . ('?' . $queryParam->buildQueryString($parameters));
49
50
        $headers  = array_merge([
51
            'Host' => 'localhost',
52
            'Origin' => 'php://docker-php',
53
            'Upgrade' => 'websocket',
54
            'Connection' => 'Upgrade',
55
            'Sec-WebSocket-Version' => '13',
56
            'Sec-WebSocket-Key' => base64_encode(uniqid()),
57
        ], $queryParam->buildHeaders($parameters));
58
59
        $body     = $queryParam->buildFormDataString($parameters);
60
61
        $request  = $this->messageFactory->createRequest('GET', $url, $headers, $body);
62
        $response = $this->httpClient->sendRequest($request);
63
64
        if ($response->getStatusCode() == 101) {
65
            if ($fetch == self::FETCH_STREAM) {
66
                return new AttachWebsocketStream($response->getBody());
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Docker\Strea...($response->getBody()); (Docker\Stream\AttachWebsocketStream) is incompatible with the return type of the parent method Docker\API\Resource\Cont...source::attachWebsocket of type Psr\Http\Message\ResponseInterface.

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...
67
            }
68
        }
69
70
        return $response;
71
    }
72
}
73