ErrorPlugin   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 38
Duplicated Lines 15.79 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 3
dl 6
loc 38
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handleRequest() 0 8 1
A transformResponseToException() 6 12 5

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 Http\Client\Plugin;
4
5 1
@trigger_error('The '.__NAMESPACE__.'\ErrorPlugin class is deprecated since version 1.1 and will be removed in 2.0. Use Http\Client\Common\Plugin\ErrorPlugin instead.', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
6
7
use Http\Client\Plugin\Exception\ClientErrorException;
8
use Http\Client\Plugin\Exception\ServerErrorException;
9
use Psr\Http\Message\RequestInterface;
10
use Psr\Http\Message\ResponseInterface;
11
12
/**
13
 * Throw exception when the response of a request is not acceptable.
14
 *
15
 * By default an exception will be thrown for all status codes from 400 to 599.
16
 *
17
 * @author Joel Wurtz <[email protected]>
18
 *
19
 * @deprecated since since version 1.1, and will be removed in 2.0. Use {@link \Http\Client\Common\Plugin\ErrorPlugin} instead.
20
 */
21
class ErrorPlugin implements Plugin
0 ignored issues
show
Deprecated Code introduced by
The interface Http\Client\Plugin\Plugin has been deprecated with message: since since version 1.1, and will be removed in 2.0. Use {@link \Http\Client\Common\Plugin} instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26 3
    public function handleRequest(RequestInterface $request, callable $next, callable $first)
27
    {
28 3
        $promise = $next($request);
29
30 3
        return $promise->then(function (ResponseInterface $response) use ($request) {
31 3
            return $this->transformResponseToException($request, $response);
32 3
        });
33
    }
34
35
    /**
36
     * Transform response to an error if possible.
37
     *
38
     * @param RequestInterface  $request  Request of the call
39
     * @param ResponseInterface $response Response of the call
40
     *
41
     * @throws ClientErrorException If response status code is a 4xx
42
     * @throws ServerErrorException If response status code is a 5xx
43
     *
44
     * @return ResponseInterface If status code is not in 4xx or 5xx return response
45
     */
46 3
    protected function transformResponseToException(RequestInterface $request, ResponseInterface $response)
47
    {
48 3 View Code Duplication
        if ($response->getStatusCode() >= 400 && $response->getStatusCode() < 500) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
49 1
            throw new ClientErrorException($response->getReasonPhrase(), $request, $response);
0 ignored issues
show
Deprecated Code introduced by
The class Http\Client\Plugin\Exception\ClientErrorException has been deprecated with message: since since version 1.1, and will be removed in 2.0. Use {@link \Http\Client\Common\Exception\ClientErrorException} instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
50
        }
51
52 2 View Code Duplication
        if ($response->getStatusCode() >= 500 && $response->getStatusCode() < 600) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
53 1
            throw new ServerErrorException($response->getReasonPhrase(), $request, $response);
0 ignored issues
show
Deprecated Code introduced by
The class Http\Client\Plugin\Exception\ServerErrorException has been deprecated with message: since since version 1.1, and will be removed in 2.0. Use {@link \Http\Client\Common\Exception\ServerErrorException} instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
54
        }
55
56 1
        return $response;
57
    }
58
}
59