GenJSDoc::performAction()   A
last analyzed

Complexity

Conditions 5
Paths 11

Size

Total Lines 30
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 20
c 1
b 0
f 0
nc 11
nop 0
dl 0
loc 30
rs 9.2888
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
            $this->response->error($e->getMessage());
54
            throw new ExitCode('', 1);
55
        }
56
    }
57
}