Code Duplication    Length = 22-26 lines in 2 locations

src/Commands/Check.php 1 location

@@ 10-31 (lines=22) @@
7
use Symfony\Component\Console\Output\OutputInterface;
8
use Yuloh\JsonGuardCli\Util;
9
10
class Check
11
{
12
    public function __invoke($schema, OutputInterface $output)
13
    {
14
        if (Util::isLoaderPath($schema)) {
15
            $schema = Util::load($schema);
16
        } else {
17
            $schema = Util::normalizeJsonArgument($schema);
18
        }
19
20
        $metaSchema = (new Dereferencer())
21
            ->dereference('file://' . Util::schemaPath('draft4.json'));
22
        $validator  = new Validator($schema, $metaSchema);
23
24
        if ($validator->passes()) {
25
            $output->writeln('<info>✓ Valid draft-04 JSON Schema</info>');
26
        } else {
27
            $output->writeln('<error>✗ Invalid draft-04 JSON Schema</error>');
28
            Util::renderErrorTable($output, $validator->errors());
29
        }
30
    }
31
}
32

src/Commands/Validate.php 1 location

@@ 10-35 (lines=26) @@
7
use Symfony\Component\Console\Output\OutputInterface;
8
use Yuloh\JsonGuardCli\Util;
9
10
class Validate
11
{
12
    public function __invoke($data, $schema, OutputInterface $output)
13
    {
14
        // If it's a loader path we don't normalize it.
15
        if (!Util::isLoaderPath($schema)) {
16
            $schema = Util::normalizeJsonArgument($schema);
17
        }
18
19
        if (Util::isLoaderPath($data)) {
20
            throw new \InvalidArgumentException(
21
                'Data can not be loaded from a loader path.'
22
            );
23
        }
24
25
        $schema    = (new Dereferencer())->dereference($schema);
26
        $validator = new Validator(Util::normalizeJsonArgument($data), $schema);
27
28
        if ($validator->passes()) {
29
            $output->writeln('<info>✓ Validation passed</info>');
30
        } else {
31
            $output->writeln('<error>✗ Validation failed</error>');
32
            Util::renderErrorTable($output, $validator->errors());
33
        }
34
    }
35
}
36