Completed
Pull Request — master (#33)
by Joao
02:50
created

SwaggerSchema   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 96
Duplicated Lines 19.79 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 5
dl 19
loc 96
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 8 8 2
A getHttpSchema() 0 4 2
A getHost() 0 4 2
A getBasePath() 0 4 2
A validateArguments() 5 10 5
A getDefintion() 6 14 4
A getRequestParameters() 0 9 2
A setAllowNullValues() 0 4 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
2
3
namespace ByJG\ApiTools\Swagger;
4
5
use ByJG\ApiTools\Base\Schema;
6
use ByJG\ApiTools\Exception\DefinitionNotFoundException;
7
use ByJG\ApiTools\Exception\HttpMethodNotFoundException;
8
use ByJG\ApiTools\Exception\InvalidDefinitionException;
9
use ByJG\ApiTools\Exception\NotMatchedException;
10
use ByJG\ApiTools\Exception\PathNotFoundException;
11
12
class SwaggerSchema extends Schema
13
{
14 View Code Duplication
    public function __construct($jsonFile, $allowNullValues = false)
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...
15
    {
16
        if (!is_array($jsonFile)) {
17
            $jsonFile = json_decode($jsonFile, true);
18
        }
19
        $this->jsonFile = $jsonFile;
20
        $this->allowNullValues = (bool) $allowNullValues;
21
    }
22
23
    public function getHttpSchema()
24
    {
25
        return isset($this->jsonFile['schemes']) ? $this->jsonFile['schemes'][0] : '';
26
    }
27
28
    public function getHost()
29
    {
30
        return isset($this->jsonFile['host']) ? $this->jsonFile['host'] : '';
31
    }
32
33
    public function getBasePath()
34
    {
35
        return isset($this->jsonFile['basePath']) ? $this->jsonFile['basePath'] : '';
36
    }
37
38
39
    /**
40
     * @param $parameterIn
41
     * @param $parameters
42
     * @param $arguments
43
     * @throws NotMatchedException
44
     */
45
    protected function validateArguments($parameterIn, $parameters, $arguments)
46
    {
47
        foreach ($parameters as $parameter) {
48 View Code Duplication
            if ($parameter['in'] === $parameterIn
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
                && $parameter['type'] === "integer"
50
                && filter_var($arguments[$parameter['name']], FILTER_VALIDATE_INT) === false) {
51
                throw new NotMatchedException('Path expected an integer value');
52
            }
53
        }
54
    }
55
56
    /**
57
     * @param $name
58
     * @return mixed
59
     * @throws DefinitionNotFoundException
60
     * @throws InvalidDefinitionException
61
     */
62
    public function getDefintion($name)
63
    {
64
        $nameParts = explode('/', $name);
65
66 View Code Duplication
        if (count($nameParts) < 3 || $nameParts[0] !== '#') {
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...
67
            throw new InvalidDefinitionException('Invalid Definition');
68
        }
69
70 View Code Duplication
        if (!isset($this->jsonFile[$nameParts[1]][$nameParts[2]])) {
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...
71
            throw new DefinitionNotFoundException("Definition '$name' not found");
72
        }
73
74
        return $this->jsonFile[$nameParts[1]][$nameParts[2]];
75
    }
76
77
    /**
78
     * @param $path
79
     * @param $method
80
     * @return SwaggerRequestBody
81
     * @throws DefinitionNotFoundException
82
     * @throws HttpMethodNotFoundException
83
     * @throws InvalidDefinitionException
84
     * @throws NotMatchedException
85
     * @throws PathNotFoundException
86
     */
87
    public function getRequestParameters($path, $method)
88
    {
89
        $structure = $this->getPathDefinition($path, $method);
90
91
        if (!isset($structure[self::SWAGGER_PARAMETERS])) {
92
            return new SwaggerRequestBody($this, "$method $path", []);
93
        }
94
        return new SwaggerRequestBody($this, "$method $path", $structure[self::SWAGGER_PARAMETERS]);
95
    }
96
97
    /**
98
     * OpenApi 2.0 doesn't describe null values, so this flag defines,
99
     * if match is ok when one of property
100
     *
101
     * @param $value
102
     */
103
    public function setAllowNullValues($value)
104
    {
105
        $this->allowNullValues = (bool) $value;
106
    }
107
}
108