OutputGeneratorTrait::resolve()   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;
4
5
use Joli\Jane\Generator\Context\Context;
6
use Joli\Jane\Runtime\Reference;
7
use Joli\Jane\Reference\Resolver;
8
use Joli\Jane\OpenApi\Model\Schema;
9
use PhpParser\Node\Arg;
10
use PhpParser\Node\Expr;
11
use PhpParser\Node\Name;
12
use PhpParser\Node\Stmt;
13
use PhpParser\Node\Scalar;
14
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
15
16
trait OutputGeneratorTrait
17
{
18
    /**
19
     * @return DenormalizerInterface
20
     */
21
    abstract protected function getDenormalizer();
22
23
    /**
24
     * @param         $status
25
     * @param         $schema
26
     * @param Context $context
27
     *
28
     * @return [string, null|Stmt\If_]
0 ignored issues
show
Documentation introduced by
The doc-type string,">[string, could not be parsed: Unknown type name "[" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
29
     */
30 11
    protected function createResponseDenormalizationStatement($status, $schema, Context $context, $reference)
31
    {
32 11
        $jsonReference  = $reference;
33 11
        $array          = false;
34
35 11
        if ($schema instanceof Reference) {
36 5
            list($jsonReference, $schema) = $this->resolve($schema, Schema::class);
37
        }
38
39 11 View Code Duplication
        if ($schema instanceof Schema && $schema->getType() == "array") {
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...
40 4
            $array = true;
41 4
            $jsonReference .= '/items';
42
43 4
            if ($schema->getItems() instanceof Reference) {
44 3
                list($jsonReference, ) = $this->resolve($schema->getItems(), Schema::class);
45
            }
46
        }
47
48 11
        $class = $context->getRegistry()->getClass($jsonReference);
49
50
        // Happens when reference resolve to a none object
51 11
        if ($class === null) {
52 7
            $returnType = 'null';
53 7
            $returnStmt = new Stmt\Return_(new Expr\ConstFetch(new Name('null')));
54
        } else {
55 6
            $class = $context->getRegistry()->getSchema($jsonReference)->getNamespace() . "\\Model\\" . $class->getName();
56
57 6
            if ($array) {
58 4
                $class .= "[]";
59
            }
60
61 6
            $returnType = "\\" . $class;
62 6
            $returnStmt = new Stmt\Return_(new Expr\MethodCall(
63 6
                new Expr\PropertyFetch(new Expr\Variable('this'), 'serializer'),
64 6
                'deserialize',
65
                [
66 6
                    new Arg(new Expr\Cast\String_(new Expr\MethodCall(new Expr\Variable('response'), 'getBody'))),
67 6
                    new Arg(new Scalar\String_($class)),
68 6
                    new Arg(new Scalar\String_('json'))
69
                ]
70
            ));
71
        }
72
73 11
        if ($status === 'default') {
74 1
            return [$returnType, $returnStmt];
75
        }
76
77 11
        return [$returnType, new Stmt\If_(
78 11
            new Expr\BinaryOp\Equal(
79 11
                new Scalar\String_($status),
80 11
                new Expr\MethodCall(new Expr\Variable('response'), 'getStatusCode')
81
            ),
82
            [
83 11
                'stmts' => [$returnStmt]
84
            ]
85
        )];
86
    }
87
88
    /**
89
     * @param Reference $reference
90
     * @param $class
91
     *
92
     * @return mixed
93
     */
94 6 View Code Duplication
    private function resolve(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...
95
    {
96 6
        $result    = $reference;
97
98
        do {
99 6
            $refString = (string) $reference->getMergedUri();
100 6
            $result = $result->resolve(function ($data) use($result, $class) {
101 6
                return $this->getDenormalizer()->denormalize($data, $class, 'json', [
102 6
                    'document-origin' => (string) $result->getMergedUri()->withFragment('')
103
                ]);
104 6
            });
105 6
        } while ($result instanceof Reference);
106
107 6
        return [$refString, $result];
108
    }
109
}
110