Completed
Push — master ( e54b6a...c762b0 )
by John
03:11
created

OpenApiOperation   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 57
Duplicated Lines 29.82 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
lcom 0
cbo 4
dl 17
loc 57
rs 10
c 1
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C __construct() 17 46 11

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
 * This file is part of the KleijnWeb\ApiDescriptions package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace KleijnWeb\ApiDescriptions\Description\Standard\OpenApi;
10
11
use KleijnWeb\ApiDescriptions\Description\Operation;
12
use KleijnWeb\ApiDescriptions\Description\Schema;
13
14
/**
15
 * @author John Kleijn <[email protected]>
16
 */
17
class OpenApiOperation extends Operation
18
{
19
    /**
20
     * Operation constructor.
21
     *
22
     * @param \stdClass $definition
23
     * @param string    $path
24
     * @param string    $method
25
     * @param array     $pathParameters
26
     */
27
    public function __construct(\stdClass $definition, string $path, string $method, array $pathParameters = [])
28
    {
29
        $this->path       = $path;
30
        $this->method     = $method;
31
        $this->parameters = $pathParameters;
32
33
        if (isset($definition->parameters)) {
34
            foreach ($definition->parameters as $parameterDefinition) {
35
                $this->parameters[] = new OpenApiParameter($parameterDefinition);
36
            }
37
        }
38
39
        if (isset($definition->responses)) {
40
            $hasOkResponse = false;
41
            foreach ($definition->responses as $code => $responseDefinition) {
42
                $code = (string)$code;
43
                if ($code === 'default' || substr((string)$code, 1) === '2') {
44
                    $hasOkResponse = true;
45
                }
46
                $code                   = (int)$code;
47
                $this->responses[$code] = new OpenApiResponse($code, $responseDefinition);
48
            }
49
            if (!$hasOkResponse) {
50
                $this->responses[200] = new OpenApiResponse(200, (object)[]);
51
            }
52
        }
53
54
        $schemaDefinition = (object)[];
55 View Code Duplication
        if (!isset($definition->parameters)) {
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...
56
            $schemaDefinition->type = 'null';
57
            $this->requestSchema    = Schema::get($schemaDefinition);
58
        } else {
59
            $schemaDefinition->type       = 'object';
60
            $schemaDefinition->required   = [];
61
            $schemaDefinition->properties = (object)[];
62
63
            foreach ($this->parameters as $parameter) {
64
                if ($parameter->isRequired()) {
65
                    $schemaDefinition->required[] = $parameter->getName();
66
                }
67
                $schemaDefinition->properties->{$parameter->getName()} = $parameter->getSchema()->getDefinition();
68
            }
69
70
            $this->requestSchema = Schema::get($schemaDefinition);
71
        }
72
    }
73
}
74