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

OpenApiParameter   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 53
Duplicated Lines 54.72 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 11
lcom 0
cbo 2
dl 29
loc 53
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 9 19 8
A createSchema() 20 20 3

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
namespace KleijnWeb\ApiDescriptions\Description\Standard\OpenApi;
9
10
use KleijnWeb\ApiDescriptions\Description\Parameter;
11
use KleijnWeb\ApiDescriptions\Description\Schema;
12
13
/**
14
 * @author John Kleijn <[email protected]>
15
 */
16
class OpenApiParameter extends Parameter
17
{
18
    /**
19
     * OpenApiParameter constructor.
20
     *
21
     * @param \stdClass $definition
22
     */
23
    public function __construct(\stdClass $definition)
24
    {
25
        $this->name             = $definition->name;
26
        $this->in               = $definition->in;
27
        $this->collectionFormat = isset($definition->collectionFormat) ? $definition->collectionFormat : null;
28
        $this->required         = isset($definition->required) && $definition->required;
29
        $this->enum             = isset($definition->enum) ? $definition->enum : null;
30
        $this->pattern          = isset($definition->pattern) ? $definition->pattern : null;
31
32 View Code Duplication
        if ($this->isIn(self::IN_BODY)) {
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...
33
            $definition->schema       = isset($definition->schema) ? $definition->schema : (object)[];
34
            $definition->schema->type = 'object';
35
        }
36 View Code Duplication
        if (isset($definition->schema)) {
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...
37
            $this->schema = Schema::get($definition->schema);
38
        } else {
39
            $this->schema = $this->createSchema($definition);
40
        }
41
    }
42
43
    /**
44
     * @param \stdClass $definition
45
     *
46
     * @return Schema
47
     */
48 View Code Duplication
    protected function createSchema(\stdClass $definition): Schema
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...
49
    {
50
        // Remove non-JSON-Schema properties
51
        $schemaDefinition     = clone $definition;
52
        $swaggerPropertyNames = [
53
            'name',
54
            'in',
55
            'description',
56
            'required',
57
            'allowEmptyValue',
58
            'collectionFormat'
59
        ];
60
        foreach ($swaggerPropertyNames as $propertyName) {
61
            if (property_exists($schemaDefinition, $propertyName)) {
62
                unset($schemaDefinition->$propertyName);
63
            }
64
        }
65
66
        return Schema::get($schemaDefinition);
67
    }
68
}
69