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()); |
|
|
|
|
55
|
|
|
throw new ExitCode('', 1); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
} |
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
orexit
statements that have been added for debug purposes.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.