ImageManager   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 153
Duplicated Lines 15.69 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 93.1%

Importance

Changes 26
Bugs 4 Features 11
Metric Value
wmc 16
c 26
b 4
f 11
lcom 0
cbo 5
dl 24
loc 153
ccs 81
cts 87
cp 0.931
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B build() 0 62 6
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 Docker\Stream\TarStream;
14
use Joli\Jane\OpenApi\Client\QueryParam;
15
use Psr\Http\Message\StreamInterface;
16
17
class ImageManager extends ImageResource
18
{
19
    const FETCH_STREAM = 'stream';
20
21
    /**
22
     * {@inheritdoc}
23
     *
24
     * @param resource|StreamInterface|string $inputStream The input stream (encoded with tar) containing the Dockerfile
25
     *                                                     and other files for the image.
26
     *
27
     * @return \Psr\Http\Message\ResponseInterface|BuildInfo[]|BuildStream
28
     */
29 4
    public function build($inputStream, $parameters = [], $fetch = self::FETCH_OBJECT)
30
    {
31 4
        if (is_resource($inputStream)) {
32 4
            $inputStream = new TarStream($inputStream);
33 4
        }
34
35 4
        $queryParam = new QueryParam();
36 4
        $queryParam->setDefault('dockerfile', null);
37 4
        $queryParam->setDefault('t', null);
38 4
        $queryParam->setDefault('remote', null);
39 4
        $queryParam->setDefault('q', false);
40 4
        $queryParam->setDefault('nocache', false);
41 4
        $queryParam->setDefault('pull', null);
42 4
        $queryParam->setDefault('rm', true);
43 4
        $queryParam->setDefault('forcerm', false);
44 4
        $queryParam->setDefault('memory', null);
45 4
        $queryParam->setDefault('memswap', null);
46 4
        $queryParam->setDefault('cpushares', null);
47 4
        $queryParam->setDefault('cpusetcpus', null);
48 4
        $queryParam->setDefault('cpuperiod', null);
49 4
        $queryParam->setDefault('cpuquota', null);
50 4
        $queryParam->setDefault('buildargs', null);
51 4
        $queryParam->setDefault('Content-type', 'application/tar');
52 4
        $queryParam->setHeaderParameters(['Content-type']);
53 4
        $queryParam->setDefault('X-Registry-Config', null);
54 4
        $queryParam->setHeaderParameters(['X-Registry-Config']);
55
56 4
        $url      = '/build';
57 4
        $url      = $url . ('?' . $queryParam->buildQueryString($parameters));
58
59 4
        $headers  = array_merge(['Host' => 'localhost'], $queryParam->buildHeaders($parameters));
60
61 4
        $body     = $inputStream;
62
63 4
        $request  = $this->messageFactory->createRequest('POST', $url, $headers, $body);
64
65 4
        if ($inputStream instanceof StreamInterface) {
66 4
            $request = $request->withHeader('Transfer-Encoding', 'chunked');
67 4
        }
68
69 4
        $response = $this->httpClient->sendRequest($request);
70
71 4
        if (200 === $response->getStatusCode()) {
72 4
            if (self::FETCH_STREAM === $fetch) {
73 1
                return new BuildStream($response->getBody(), $this->serializer);
74
            }
75
76 3
            if (self::FETCH_OBJECT === $fetch) {
77 3
                $buildInfoList = [];
78
79 3
                $stream = new BuildStream($response->getBody(), $this->serializer);
80
                $stream->onFrame(function (BuildInfo $buildInfo) use (&$buildInfoList) {
81 3
                    $buildInfoList[] = $buildInfo;
82 3
                });
83 3
                $stream->wait();
84
85 3
                return $buildInfoList;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $buildInfoList; (array) is incompatible with the return type documented by Docker\Manager\ImageManager::build of type Psr\Http\Message\Respons...cker\Stream\BuildStream.

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...
86
            }
87
        }
88
89
        return $response;
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     *
95
     * @return \Psr\Http\Message\ResponseInterface|CreateImageInfo[]|CreateImageStream
96
     */
97 3 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...
98
    {
99 3
        $response = parent::create($parameters, self::FETCH_RESPONSE);
100
101 3
        if (200 === $response->getStatusCode()) {
102 3
            if (self::FETCH_STREAM === $fetch) {
103 1
                return new CreateImageStream($response->getBody(), $this->serializer);
104
            }
105
106 2
            if (self::FETCH_OBJECT === $fetch) {
107 2
                $createImageInfoList = [];
108
109 2
                $stream = new CreateImageStream($response->getBody(), $this->serializer);
110
                $stream->onFrame(function (CreateImageInfo $createImageInfo) use (&$createImageInfoList) {
111 2
                    $createImageInfoList[] = $createImageInfo;
112 2
                });
113 2
                $stream->wait();
114
115 2
                return $createImageInfoList;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $createImageInfoList; (array) is incompatible with the return type documented by Docker\Manager\ImageManager::create of type Psr\Http\Message\Respons...tream\CreateImageStream.

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...
116
            }
117
        }
118
119
        return $response;
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     *
125
     * @return \Psr\Http\Message\ResponseInterface|PushImageInfo[]|CreateImageStream
126
     */
127 2
    public function push($name, $parameters = [], $fetch = self::FETCH_OBJECT)
128
    {
129 2
        if (isset($parameters['X-Registry-Auth']) && $parameters['X-Registry-Auth'] instanceof AuthConfig) {
0 ignored issues
show
Bug introduced by
The class Docker\API\Model\AuthConfig does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
130 2
            $parameters['X-Registry-Auth'] = base64_encode($this->serializer->serialize($parameters['X-Registry-Auth'], 'json'));
131 2
        }
132
133 2
        $queryParam = new QueryParam();
134 2
        $queryParam->setDefault('tag', null);
135 2
        $queryParam->setDefault('X-Registry-Auth', null);
136 2
        $queryParam->setHeaderParameters(['X-Registry-Auth']);
137
138 2
        $url      = 'http://localhost/images/{name}/push';
139 2
        $url      = str_replace('{name}', $name, $url);
140 2
        $url      = $url . ('?' . $queryParam->buildQueryString($parameters));
141
142 2
        $headers  = array_merge(['Host' => 'localhost'], $queryParam->buildHeaders($parameters));
143
144 2
        $body     = $queryParam->buildFormDataString($parameters);
145
146 2
        $request  = $this->messageFactory->createRequest('POST', $url, $headers, $body);
147 2
        $response = $this->httpClient->sendRequest($request);
148
149 2
        if (200 === $response->getStatusCode()) {
150 2
            if (self::FETCH_STREAM === $fetch) {
151 1
                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 documented by Docker\Manager\ImageManager::push of type Psr\Http\Message\Respons...tream\CreateImageStream.

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...
152
            }
153
154 1
            if (self::FETCH_OBJECT === $fetch) {
155 1
                $pushImageInfoList = [];
156
157 1
                $stream = new PushStream($response->getBody(), $this->serializer);
158 1
                $stream->onFrame(function (PushImageInfo $pushImageInfo) use (&$pushImageInfoList) {
159 1
                    $pushImageInfoList[] = $pushImageInfo;
160 1
                });
161 1
                $stream->wait();
162
163 1
                return $pushImageInfoList;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $pushImageInfoList; (array) is incompatible with the return type documented by Docker\Manager\ImageManager::push of type Psr\Http\Message\Respons...tream\CreateImageStream.

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...
164
            }
165
        }
166
167
        return $response;
168
    }
169
}
170