BodyParameterGenerator::resolveSchema()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 15
Ratio 100 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 15
loc 15
ccs 9
cts 9
cp 1
rs 9.4285
cc 2
eloc 9
nc 1
nop 2
crap 2
1
<?php
2
3
namespace Joli\Jane\OpenApi\Generator\Parameter;
4
5
use Doctrine\Common\Inflector\Inflector;
6
use Joli\Jane\Generator\Context\Context;
7
use Joli\Jane\Runtime\Reference;
8
use Joli\Jane\Reference\Resolver;
9
use Joli\Jane\OpenApi\Model\BodyParameter;
10
use Joli\Jane\OpenApi\Model\Schema;
11
use PhpParser\Node;
12
use PhpParser\Parser;
13
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
14
15
class BodyParameterGenerator extends ParameterGenerator
16
{
17
    /**
18
     * @var DenormalizerInterface
19
     */
20
    private $denormalizer;
21
22 13
    public function __construct(Parser $parser, DenormalizerInterface $denormalizer)
23
    {
24 13
        parent::__construct($parser);
25
26 13
        $this->denormalizer = $denormalizer;
27 13
    }
28
29
    /**
30
     * {@inheritDoc}
31
     *
32
     * @param $parameter BodyParameter
33
     */
34 3
    public function generateMethodParameter($parameter, Context $context, $reference)
35
    {
36 3
        $name = Inflector::camelize($parameter->getName());
37
38 3
        list($class, $array) = $this->getClass($parameter, $context, $reference);
39
40 3
        if (null === $array || true === $array) {
41 2
            if ($class == "array") {
42 1
                return new Node\Param($name, null, "array");
43
            }
44
45 2
            return new Node\Param($name);
46
        }
47
48 2
        return new Node\Param($name, null, $class);
49
    }
50
51
    /**
52
     * {@inheritDoc}
53
     *
54
     * @param $parameter BodyParameter
55
     */
56 3
    public function generateDocParameter($parameter, Context $context, $reference)
57
    {
58 3
        list($class, $array) = $this->getClass($parameter, $context, $reference);
0 ignored issues
show
Unused Code introduced by
The assignment to $array is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
59
60 3
        if (null === $class) {
61
            return sprintf('%s $%s %s', 'mixed', Inflector::camelize($parameter->getName()), $parameter->getDescription() ?: '');
62
        }
63
64 3
        return sprintf('%s $%s %s', $class, Inflector::camelize($parameter->getName()), $parameter->getDescription() ?: '');
65
    }
66
67
    /**
68
     * @param BodyParameter $parameter
69
     * @param Context $context
70
     *
71
     * @return array
72
     */
73 3
    protected function getClass(BodyParameter $parameter, Context $context, $reference)
74
    {
75 3
        $resolvedSchema = null;
76 3
        $jsonReference  = null;
77 3
        $array          = false;
78 3
        $schema         = $parameter->getSchema();
79
80 3
        if ($schema instanceof Reference) {
81 1
            list($jsonReference, $resolvedSchema) = $this->resolveSchema($schema, Schema::class);
82
        }
83
84 3 View Code Duplication
        if ($schema instanceof Schema && $schema->getType() == "array" && $schema->getItems() instanceof Reference) {
0 ignored issues
show
Bug introduced by
The class Joli\Jane\OpenApi\Model\Schema does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
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...
85
            list($jsonReference, $resolvedSchema) = $this->resolveSchema($schema->getItems(), Schema::class);
86
            $array          = true;
87
        }
88
89 3
        if ($resolvedSchema === null) {
90 3
            if ($context->getRegistry()->hasClass($reference)) {
91 1
                return ["\\" . $context->getRegistry()->getSchema($reference)->getNamespace() . "\\Model\\" . $context->getRegistry()->getClass($reference)->getName(), false];
92
            }
93
94 2
            return [$schema->getType(), null];
95
        }
96
97 1
        $class = $context->getRegistry()->getClass($jsonReference);
98
99
        // Happens when reference resolve to a none object
100 1
        if ($class === null) {
101
            return [$schema->getType(), null];
102
        }
103
104 1
        $class = "\\" . $context->getRegistry()->getSchema($jsonReference)->getNamespace() . "\\Model\\" . $class->getName();
105
106 1
        if ($array) {
107
            $class .= "[]";
108
        }
109
110 1
        return [$class, $array];
111
    }
112
113
    /**
114
     * @param Reference $reference
115
     * @param $class
116
     *
117
     * @return mixed
118
     */
119 1 View Code Duplication
    private function resolveSchema(Reference $reference, $class)
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...
120
    {
121 1
        $result    = $reference;
122
123
        do {
124 1
            $refString = (string) $reference->getMergedUri();
125 1
            $result = $result->resolve(function ($data) use($result, $class) {
126 1
                return $this->denormalizer->denormalize($data, $class, 'json', [
127 1
                    'document-origin' => (string) $result->getMergedUri()->withFragment('')
128
                ]);
129 1
            });
130 1
        } while ($result instanceof Reference);
131
132 1
        return [$refString, $result];
133
    }
134
}
135