Completed
Push — master ( 90ac5a...649bdf )
by Amine
11s
created

ExceptionPrinter::printParseException()   B

Complexity

Conditions 6
Paths 18

Size

Total Lines 23
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 8.5906
c 0
b 0
f 0
cc 6
eloc 18
nc 18
nop 1
1
<?php namespace Tarsana\Command\Console;
2
3
use Tarsana\Syntax\ArraySyntax;
4
use Tarsana\Syntax\Exceptions\ParseException;
5
use Tarsana\Syntax\ObjectSyntax;
6
use Tarsana\Syntax\Syntax;
7
8
/**
9
 * Transforms an exception to a string to be shown on the console.
10
 */
11
class ExceptionPrinter {
12
13
    /**
14
     * Converts the given exception to a string.
15
     *
16
     * @param  \Exception $e
17
     * @return string
18
     */
19
    public function print(\Exception $e) : string
20
    {
21
        if ($e instanceof ParseException)
22
            return $this->printParseException($e, true);
0 ignored issues
show
Unused Code introduced by
The call to ExceptionPrinter::printParseException() has too many arguments starting with true.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
23
24
        return "<error>{$e}</error>";
25
    }
26
27
    /**
28
     * Converts a parse exception to a string.
29
     *
30
     * @param  Tarsana\Syntax\Exceptions\ParseException $e
31
     * @return string
32
     */
33
    public function printParseException(ParseException $e) : string
34
    {
35
        $syntax = $e->syntax();
36
        $error  = '';
37
        if ($syntax instanceof ObjectSyntax) {
38
            $i = $e->extra();
39
            if ($i['type'] == 'invalid-field')
40
                $error = "{$i['field']} is invalid!";
41
            if ($i['type'] == 'missing-field')
42
                $error = "{$i['field']} is missing!";
43
            if ($i['type'] == 'additional-items') {
44
                $items = implode(', ', $i['items']);
45
                $error = "additional items {$items}";
46
            }
47
        }
48
        $syntax = $this->printSyntax($e->syntax());
49
        $output = "<reset>Failed to parse <warn>'{$e->input()}'</warn> as <info>{$syntax}</info> <error>{$error}</error>";
50
        $previous = $e->previous();
51
        if ($previous) {
52
            $output .= PHP_EOL . $this->printParseException($previous);
53
        }
54
        return $output;
55
    }
56
57
    protected function printSyntax(Syntax $s, bool $short = false) : string
58
    {
59
        if ($s instanceof ObjectSyntax) {
60
            if ($short) return 'object';
61
            return implode($s->separator(), array_keys($s->fields()));
62
        }
63
        if ($s instanceof ArraySyntax) {
64
            if ($short) return 'array';
65
            return $this->printSyntax($s->syntax()).$s->separator().'...';
66
        }
67
        return (string) $s;
68
    }
69
}
70