JsonApiCallsTrait   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 97
Duplicated Lines 44.33 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 43
loc 97
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A postJsonApi() 11 11 1
A putJsonApi() 11 11 1
A patchJsonApi() 11 11 1
A deleteJsonApi() 10 10 1
A streamFromString() 0 7 1

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 declare(strict_types=1);
2
3
namespace Limoncello\Testing;
4
5
/**
6
 * Copyright 2015-2020 [email protected]
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 * http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
use Psr\Http\Message\ResponseInterface;
22
use Psr\Http\Message\StreamInterface;
23
use Zend\Diactoros\Stream;
24
25
/** @noinspection PhpTooManyParametersInspection
26
 * @package Limoncello\Testing
27
 *
28
 * @@codingStandardsIgnoreLine
29
 * @method ResponseInterface call(string $method, string $uri, array $queryParams = [], array $parsedBody = [], array $headers = [], array $cookies = [], array $files = [], array $server = [], string|StreamInterface $messageBody = 'php://input')
30
 */
31
trait JsonApiCallsTrait
32
{
33
    /**
34
     * @param string $uri
35
     * @param string $json
36
     * @param array  $headers
37
     * @param array  $cookies
38
     * @param array  $files
39
     *
40
     * @return ResponseInterface
41
     */
42 1 View Code Duplication
    protected function postJsonApi(
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...
43
        string $uri,
44
        string $json,
45
        array $headers = [],
46
        array $cookies = [],
47
        array $files = []
48
    ): ResponseInterface {
49 1
        $headers['CONTENT_TYPE'] = 'application/vnd.api+json';
50
51 1
        return $this->call('POST', $uri, [], [], $headers, $cookies, $files, [], $this->streamFromString($json));
52
    }
53
54
    /**
55
     * @param string $uri
56
     * @param string $json
57
     * @param array  $headers
58
     * @param array  $cookies
59
     * @param array  $files
60
     *
61
     * @return ResponseInterface
62
     */
63 1 View Code Duplication
    protected function putJsonApi(
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...
64
        string $uri,
65
        string $json,
66
        array $headers = [],
67
        array $cookies = [],
68
        array $files = []
69
    ): ResponseInterface {
70 1
        $headers['CONTENT_TYPE'] = 'application/vnd.api+json';
71
72 1
        return $this->call('PUT', $uri, [], [], $headers, $cookies, $files, [], $this->streamFromString($json));
73
    }
74
75
    /**
76
     * @param string $uri
77
     * @param string $json
78
     * @param array  $headers
79
     * @param array  $cookies
80
     * @param array  $files
81
     *
82
     * @return ResponseInterface
83
     */
84 1 View Code Duplication
    protected function patchJsonApi(
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...
85
        string $uri,
86
        string $json,
87
        array $headers = [],
88
        array $cookies = [],
89
        array $files = []
90
    ): ResponseInterface {
91 1
        $headers['CONTENT_TYPE'] = 'application/vnd.api+json';
92
93 1
        return $this->call('PATCH', $uri, [], [], $headers, $cookies, $files, [], $this->streamFromString($json));
94
    }
95
96
    /**
97
     * @param string $uri
98
     * @param string $json
99
     * @param array  $headers
100
     * @param array  $cookies
101
     *
102
     * @return ResponseInterface
103
     */
104 1 View Code Duplication
    protected function deleteJsonApi(
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...
105
        string $uri,
106
        string $json,
107
        array $headers = [],
108
        array $cookies = []
109
    ): ResponseInterface {
110 1
        $headers['CONTENT_TYPE'] = 'application/vnd.api+json';
111
112 1
        return $this->call('DELETE', $uri, [], [], $headers, $cookies, [], [], $this->streamFromString($json));
113
    }
114
115
    /**
116
     * @param string $content
117
     *
118
     * @return StreamInterface
119
     */
120 5
    protected function streamFromString(string $content): StreamInterface
121
    {
122 5
        $stream = new Stream('php://temp', 'wb+');
123 5
        $stream->write($content);
124
125 5
        return $stream;
126
    }
127
}
128