Issues (4)

src/Cli/ApiCompatCmd.php (3 issues)

Severity
1
<?php
2
3
namespace Swaggest\ApiCompat\Cli;
4
5
use Swaggest\ApiCompat\ApiCompat;
6
use Swaggest\ApiCompat\Change;
7
use Swaggest\ApiCompat\Path;
8
use Yaoi\Command;
9
use Yaoi\Command\Definition;
10
use Yaoi\Io\Response;
11
12
class ApiCompatCmd extends Command
13
{
14
    public $originalPath;
15
    public $newPath;
16
    public $verbose;
17
18
19
    /**
20
     * @param Definition $definition
21
     * @param \stdClass|static $options
22
     */
23
    static function setUpDefinition(Definition $definition, $options)
24
    {
25
        $definition->name = 'api-compat';
26
        $definition->version = 'v1.0.3';
27
        $definition->description = 'API compatibility checker for swagger.json, https://github.com/swaggest/api-compat';
28
29
        $options->originalPath = Command\Option::create()->setIsUnnamed()->setIsRequired()
30
            ->setDescription('Path to old (original) swagger.json file');
31
        $options->newPath = Command\Option::create()->setIsUnnamed()->setIsRequired()
32
            ->setDescription('Path to new swagger.json file');
33
34
        $options->verbose = Command\Option::create()->setDescription('Verbose output');
35
    }
36
37
    public function performAction()
38
    {
39
        $originalJson = file_get_contents($this->originalPath);
40
        if (!$originalJson) {
41
            $this->response->error('Unable to read ' . $this->originalPath);
42
            die(1);
0 ignored issues
show
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...
43
        }
44
45
        $newJson = file_get_contents($this->newPath);
46
        if (!$newJson) {
47
            $this->response->error('Unable to read ' . $this->newPath);
48
            die(1);
0 ignored issues
show
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...
49
        }
50
51
        $ac = new ApiCompat(json_decode($originalJson), json_decode($newJson));
52
        if ($ac->getBreakingChanges()) {
53
            if ($this->verbose) {
54
                $this->showMessages($ac->getBreakingChanges(), $this->response);
55
            }
56
            $this->response->error('Breaking changes detected in new swagger schema');
57
            die(1);
0 ignored issues
show
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...
58
        } else {
59
            $this->response->success('No breaking changes detected in new swagger schema');
60
        }
61
62
    }
63
64
    /**
65
     * @param Change[] $changes
66
     * @param Response $response
67
     */
68
    public function showMessages($changes, $response)
69
    {
70
        foreach ($changes as $breakingChange) {
71
            $response->error($breakingChange->message . ' at ' . Path::quoteUrldecode($breakingChange->path));
72
            if ($breakingChange->originalValue) {
73
                $response->addContent('original: ' . json_encode($breakingChange->originalValue, JSON_UNESCAPED_SLASHES));
74
            }
75
            if ($breakingChange->newValue) {
76
                $response->addContent('new: ' . json_encode($breakingChange->newValue, JSON_UNESCAPED_SLASHES));
77
            }
78
        }
79
80
    }
81
82
83
84
}