Passed
Pull Request — master (#3)
by Viacheslav
02:25 queued 58s
created

GenGo::performAction()   C

Complexity

Conditions 13
Paths 49

Size

Total Lines 62
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 40
CRAP Score 13.0574

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 13
eloc 43
c 1
b 0
f 0
nc 49
nop 0
dl 0
loc 62
ccs 40
cts 43
cp 0.9302
crap 13.0574
rs 6.6166

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Swaggest\JsonCli;
4
5
use Swaggest\GoCodeBuilder\JsonSchema\GoBuilder;
6
use Swaggest\GoCodeBuilder\JsonSchema\StructHookCallback;
7
use Swaggest\GoCodeBuilder\Templates\GoFile;
8
use Swaggest\GoCodeBuilder\Templates\Struct\StructDef;
9
use Swaggest\JsonSchema\Context;
10
use Swaggest\JsonSchema\RemoteRef\Preloaded;
11
use Swaggest\JsonSchema\Schema;
12
use Yaoi\Command;
13
14
class GenGo extends Command
15
{
16
    /** @var string */
17
    public $schema;
18
    /** @var string */
19
    public $packageName = 'entities';
20
    /** @var string */
21
    public $rootName = 'Structure';
22
    /** @var bool */
23
    public $showConstProperties = false;
24
    /** @var bool */
25
    public $keepParentInPropertyNames = false;
26
    /** @var []string */
0 ignored issues
show
Documentation Bug introduced by
The doc comment []string at position 0 could not be parsed: Unknown type name '[' at position 0 in []string.
Loading history...
27
    public $defPtr = ['#/definitions'];
28
    /** @var []string */
0 ignored issues
show
Documentation Bug introduced by
The doc comment []string at position 0 could not be parsed: Unknown type name '[' at position 0 in []string.
Loading history...
29
    public $ptrInSchema;
30
31
    public $output;
32
33
34
    /**
35
     * @param Command\Definition $definition
36
     * @param \stdClass|static $options
37
     */
38 1
    public static function setUpDefinition(Command\Definition $definition, $options)
39
    {
40 1
        $definition->description = 'Generate Go code from JSON schema';
41 1
        $options->schema = Command\Option::create()
0 ignored issues
show
Documentation Bug introduced by
It seems like Yaoi\Command\Option::cre...amed()->setIsRequired() of type Yaoi\Command\Option is incompatible with the declared type string of property $schema.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
42 1
            ->setDescription('Path to JSON schema file')->setIsUnnamed()->setIsRequired();
43
44 1
        $options->output = Command\Option::create()
45 1
            ->setDescription('Path to output .go file, STDOUT is used by default')->setType();
46
47 1
        $options->ptrInSchema = Command\Option::create()->setType()->setIsVariadic()
48 1
            ->setDescription('JSON pointers to structure in in root schema, default #');
49
50 1
        $options->packageName = Command\Option::create()->setType()
0 ignored issues
show
Documentation Bug introduced by
It seems like Yaoi\Command\Option::cre...e, default "entities"') of type Yaoi\Command\Option is incompatible with the declared type string of property $packageName.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
51 1
            ->setDescription('Go package name, default "entities"');
52
53 1
        $options->rootName = Command\Option::create()->setType()
0 ignored issues
show
Documentation Bug introduced by
It seems like Yaoi\Command\Option::cre...ly used for # pointer') of type Yaoi\Command\Option is incompatible with the declared type string of property $rootName.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
54 1
            ->setDescription('Go root struct name, default "Structure", only used for # pointer');
55
56 1
        $options->defPtr = Command\Option::create()->setType()->setIsVariadic()
57 1
            ->setDescription('Definitions pointers to strip from symbol names, default #/definitions');
58
59 1
        $options->showConstProperties = Command\Option::create()
0 ignored issues
show
Documentation Bug introduced by
It seems like Yaoi\Command\Option::cre...es, hidden by default') of type Yaoi\Command\Option is incompatible with the declared type boolean of property $showConstProperties.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
60 1
            ->setDescription('Show properties with constant values, hidden by default');
61
62 1
        $options->keepParentInPropertyNames = Command\Option::create()
0 ignored issues
show
Documentation Bug introduced by
It seems like Yaoi\Command\Option::cre...e, removed by default') of type Yaoi\Command\Option is incompatible with the declared type boolean of property $keepParentInPropertyNames.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
63 1
            ->setDescription('Keep parent prefix in property name, removed by default');
64
65 1
    }
66
67
68 3
    public function performAction()
69
    {
70 3
        $dataValue = Base::readJsonOrYaml($this->schema, $this->response);
71 3
        if (!$dataValue) {
72
            $this->response->error('Unable to find schema in ' . $this->schema);
73
            die(1);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
74
        }
75
76 3
        $skipRoot = false;
77 3
        if (empty($this->ptrInSchema)) {
78 1
            $schema = Schema::import($dataValue);
79
        } else {
80 2
            $baseName = basename($this->schema);
81 2
            $skipRoot = true;
82 2
            $preloaded = new Preloaded();
83 2
            $preloaded->setSchemaData($baseName, $dataValue);
84 2
            $data = new \stdClass();
85 2
            foreach ($this->ptrInSchema as $i => $ptr) {
86 2
                $data->oneOf[$i] = (object)[Schema::PROP_REF => $baseName . $ptr];
87
            }
88 2
            $schema = Schema::import($data, new Context($preloaded));
89
        }
90
91 3
        $builder = new GoBuilder();
92 3
        $builder->options->hideConstProperties = !$this->showConstProperties;
93 3
        $builder->options->trimParentFromPropertyNames = !$this->keepParentInPropertyNames;
94 3
        if (!empty($this->defPtr)) {
95 3
            $builder->pathToNameHook->prefixes = [];
96 3
            foreach ($this->defPtr as $defPtr) {
97 3
                if (isset($baseName)) {
98 2
                    $builder->pathToNameHook->prefixes[] = $baseName . $defPtr;
99
                }
100 3
                $builder->pathToNameHook->prefixes[] = $defPtr;
101
            }
102
        }
103
104 3
        if (isset($baseName)) {
105 2
            $builder->pathToNameHook->prefixes[] = $baseName;
106
        }
107
108 3
        $builder->structCreatedHook = new StructHookCallback(function (StructDef $structDef, $path, $schema) use ($builder) {
0 ignored issues
show
Unused Code introduced by
The import $builder is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
Unused Code introduced by
The parameter $schema is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

108
        $builder->structCreatedHook = new StructHookCallback(function (StructDef $structDef, $path, /** @scrutinizer ignore-unused */ $schema) use ($builder) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
109 3
            if ('#' === $path) {
110 2
                $structDef->setName($this->rootName);
111
            }
112 3
        });
113 3
        $builder->getType($schema);
114
115 3
        $goFile = new GoFile($this->packageName);
116 3
        $goFile->fileComment = 'Code generated by github.com/swaggest/json-cli, DO NOT EDIT.';
117 3
        $goFile->setComment('Package ' . $this->packageName . ' contains JSON mapping structures.');
118 3
        foreach ($builder->getGeneratedStructs() as $generatedStruct) {
119 3
            if ($skipRoot && $generatedStruct->path === '#') {
120 1
                continue;
121
            }
122 3
            $goFile->getCode()->addSnippet($generatedStruct->structDef);
123
        }
124 3
        $goFile->getCode()->addSnippet($builder->getCode());
125
126 3
        if ($this->output) {
127
            file_put_contents($this->output, $goFile->render());
128
        } else {
129 3
            echo $goFile->render();
130
        }
131
    }
132
}