Completed
Pull Request — master (#147)
by Joel
05:37 queued 02:50
created

ImageManager   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 112
Duplicated Lines 42.86 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 27
Bugs 4 Features 12
Metric Value
wmc 14
c 27
b 4
f 12
lcom 1
cbo 9
dl 48
loc 112
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B build() 24 24 4
B create() 24 24 4
B push() 0 42 6

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
namespace Docker\Manager;
4
5
use Docker\API\Model\AuthConfig;
6
use Docker\API\Model\BuildInfo;
7
use Docker\API\Model\CreateImageInfo;
8
use Docker\API\Model\PushImageInfo;
9
use Docker\API\Resource\ImageResource;
10
use Docker\Stream\BuildStream;
11
use Docker\Stream\CreateImageStream;
12
use Docker\Stream\PushStream;
13
use Joli\Jane\Swagger\Client\QueryParam;
14
15
class ImageManager extends ImageResource
16
{
17
    const FETCH_STREAM = 'stream';
18
19
    /**
20
     * {@inheritdoc}
21
     *
22
     * @return \Psr\Http\Message\ResponseInterface|BuildInfo[]|BuildStream
23
     */
24 View Code Duplication
    public function build($inputStream, $parameters = [], $fetch = self::FETCH_OBJECT)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
25
    {
26
        $response = parent::build($inputStream, $parameters, self::FETCH_RESPONSE);
27
28
        if (200 === $response->getStatusCode()) {
29
            if (self::FETCH_STREAM === $fetch) {
30
                return new BuildStream($response->getBody(), $this->serializer);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Docker\Strea...(), $this->serializer); (Docker\Stream\BuildStream) is incompatible with the return type of the parent method Docker\API\Resource\ImageResource::build 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...
31
            }
32
33
            if (self::FETCH_OBJECT === $fetch) {
34
                $buildInfoList = [];
35
36
                $stream = new BuildStream($response->getBody(), $this->serializer);
37
                $stream->onFrame(function (BuildInfo $buildInfo) use (&$buildInfoList) {
38
                    $buildInfoList[] = $buildInfo;
39
                });
40
                $stream->wait();
41
42
                return $buildInfoList;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $buildInfoList; (array) is incompatible with the return type of the parent method Docker\API\Resource\ImageResource::build 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...
43
            }
44
        }
45
46
        return $response;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     *
52
     * @return \Psr\Http\Message\ResponseInterface|CreateImageInfo[]|CreateImageStream
53
     */
54 View Code Duplication
    public function create($parameters = [], $fetch = self::FETCH_OBJECT)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
55
    {
56
        $response = parent::create($parameters, self::FETCH_RESPONSE);
57
58
        if (200 === $response->getStatusCode()) {
59
            if (self::FETCH_STREAM === $fetch) {
60
                return new CreateImageStream($response->getBody(), $this->serializer);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Docker\Strea...(), $this->serializer); (Docker\Stream\CreateImageStream) is incompatible with the return type of the parent method Docker\API\Resource\ImageResource::create 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...
61
            }
62
63
            if (self::FETCH_OBJECT === $fetch) {
64
                $createImageInfoList = [];
65
66
                $stream = new CreateImageStream($response->getBody(), $this->serializer);
67
                $stream->onFrame(function (CreateImageInfo $createImageInfo) use (&$createImageInfoList) {
68
                    $createImageInfoList[] = $createImageInfo;
69
                });
70
                $stream->wait();
71
72
                return $createImageInfoList;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $createImageInfoList; (array) is incompatible with the return type of the parent method Docker\API\Resource\ImageResource::create 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...
73
            }
74
        }
75
76
        return $response;
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     *
82
     * @return \Psr\Http\Message\ResponseInterface|PushImageInfo[]|CreateImageStream
83
     */
84
    public function push($name, $parameters = [], $fetch = self::FETCH_OBJECT)
85
    {
86
        if (isset($parameters['X-Registry-Auth']) && $parameters['X-Registry-Auth'] instanceof AuthConfig) {
87
            $parameters['X-Registry-Auth'] = base64_encode($this->serializer->serialize($parameters['X-Registry-Auth'], 'json'));
88
        }
89
90
        $queryParam = new QueryParam();
91
        $queryParam->setDefault('tag', null);
92
        $queryParam->setDefault('X-Registry-Auth', null);
93
        $queryParam->setHeaderParameters(['X-Registry-Auth']);
94
95
        $url      = 'http://localhost/images/{name}/push';
96
        $url      = str_replace('{name}', $name, $url);
97
        $url      = $url . ('?' . $queryParam->buildQueryString($parameters));
98
99
        $headers  = array_merge(['Host' => 'localhost'], $queryParam->buildHeaders($parameters));
100
101
        $body     = $queryParam->buildFormDataString($parameters);
102
103
        $request  = $this->messageFactory->createRequest('POST', $url, $headers, $body);
104
        $response = $this->httpClient->sendRequest($request);
105
106
        if (200 === $response->getStatusCode()) {
107
            if (self::FETCH_STREAM === $fetch) {
108
                return new PushStream($response->getBody(), $this->serializer);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Docker\Strea...(), $this->serializer); (Docker\Stream\PushStream) is incompatible with the return type of the parent method Docker\API\Resource\ImageResource::push 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...
109
            }
110
111
            if (self::FETCH_OBJECT === $fetch) {
112
                $pushImageInfoList = [];
113
114
                $stream = new PushStream($response->getBody(), $this->serializer);
115
                $stream->onFrame(function (PushImageInfo $pushImageInfo) use (&$pushImageInfoList) {
116
                    $pushImageInfoList[] = $pushImageInfo;
117
                });
118
                $stream->wait();
119
120
                return $pushImageInfoList;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $pushImageInfoList; (array) is incompatible with the return type of the parent method Docker\API\Resource\ImageResource::push 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...
121
            }
122
        }
123
124
        return $response;
125
    }
126
}
127