MultiPartLocation   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 64
Duplicated Lines 18.75 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 12
loc 64
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A visit() 12 12 1
A after() 0 19 2

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
namespace GuzzleHttp\Command\Guzzle\RequestLocation;
3
4
use GuzzleHttp\Command\CommandInterface;
5
use GuzzleHttp\Command\Guzzle\Operation;
6
use GuzzleHttp\Command\Guzzle\Parameter;
7
use GuzzleHttp\Psr7;
8
use Psr\Http\Message\RequestInterface;
9
10
/**
11
 * Adds POST files to a request
12
 */
13
class MultiPartLocation extends AbstractLocation
14
{
15
    /** @var string $contentType */
16
    protected $contentType = 'multipart/form-data; boundary=';
17
18
    /** @var array $formParamsData */
19
    protected $multipartData = [];
20
21
    /**
22
     * Set the name of the location
23
     *
24
     * @param string $locationName
25
     */
26 1
    public function __construct($locationName = 'multipart')
27
    {
28 1
        parent::__construct($locationName);
29 1
    }
30
31
    /**
32
     * @param CommandInterface $command
33
     * @param RequestInterface $request
34
     * @param Parameter $param
35
     * @return RequestInterface
36
     */
37 1 View Code Duplication
    public function visit(
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...
38
        CommandInterface $command,
39
        RequestInterface $request,
40
        Parameter $param
41
    ) {
42 1
        $this->multipartData[] = [
43 1
            'name' => $param->getWireName(),
44 1
            'contents' => $this->prepareValue($command[$param->getName()], $param)
0 ignored issues
show
Bug introduced by
Consider using $param->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
45 1
        ];
46
47 1
        return $request;
48
    }
49
50
51
    /**
52
     * @param CommandInterface $command
53
     * @param RequestInterface $request
54
     * @param Operation $operation
55
     * @return RequestInterface
56
     */
57 1
    public function after(
58
        CommandInterface $command,
59
        RequestInterface $request,
60
        Operation $operation
61
    ) {
62 1
        $data = $this->multipartData;
63 1
        $this->multipartData = [];
64 1
        $modify = [];
65
66 1
        $body = new Psr7\MultipartStream($data);
67 1
        $modify['body'] = Psr7\stream_for($body);
68 1
        $request = Psr7\modify_request($request, $modify);
69 1
        if ($request->getBody() instanceof Psr7\MultipartStream) {
0 ignored issues
show
Bug introduced by
The class GuzzleHttp\Psr7\MultipartStream 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...
70
            // Use a multipart/form-data POST if a Content-Type is not set.
71 1
            $request->withHeader('Content-Type', $this->contentType . $request->getBody()->getBoundary());
72 1
        }
73
74 1
        return $request;
75
    }
76
}
77