Completed
Pull Request — master (#25)
by Viacheslav
10:07
created

GenJSDoc   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
dl 0
loc 46
rs 10
c 1
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setUpDefinition() 0 4 1
A performAction() 0 31 5
1
<?php
2
3
namespace Swaggest\JsonCli;
4
5
use Swaggest\JsonCli\GenPhp\BuilderOptions;
6
use Swaggest\JsonSchema\Schema;
7
use Swaggest\PhpCodeBuilder\JSDoc\TypeBuilder;
8
use Yaoi\Command;
9
10
class GenJSDoc extends Base
11
{
12
    use BuilderOptions;
13
14
    /**
15
     * @param Command\Definition $definition
16
     * @param \stdClass|static $options
17
     */
18
    public static function setUpDefinition(Command\Definition $definition, $options)
19
    {
20
        $definition->description = 'Generate JSDoc code from JSON schema';
21
        Base::setupGenOptions($definition, $options);
22
    }
23
24
25
    public function performAction()
26
    {
27
        try {
28
            $skipRoot = false;
29
            $baseName = null;
30
            $schema = $this->loadSchema($skipRoot, $baseName);
31
32
            $jb = new TypeBuilder();
33
            $jb->trimNamePrefix = $this->defPtr;
34
35
            if (!$schema instanceof Schema) {
36
                $this->response->error('failed to assert Schema type, ' . get_class($schema) . ' received');
37
                throw new ExitCode('', 1);
38
            }
39
40
            $jb->getTypeString($schema);
41
42
            if ($this->output) {
43
                if (!file_exists(dirname($this->output))) {
44
                    $this->response->error('Destination directory does not exist, please create: ' . dirname($this->output));
45
                    throw new ExitCode('', 1);
46
                }
47
                file_put_contents($this->output, $jb->file);
48
49
            } else {
50
                echo $jb->file;
51
            }
52
        } catch (\Exception $e) {
53
            throw $e;
54
            $this->response->error($e->getMessage());
0 ignored issues
show
Unused Code introduced by
$this->response->error($e->getMessage()) is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
55
            throw new ExitCode('', 1);
56
        }
57
    }
58
}