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

RamlParameter::__construct()   B

Complexity

Conditions 7
Paths 48

Size

Total Lines 18
Code Lines 13

Duplication

Lines 9
Ratio 50 %

Importance

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