SyntaxHelper::fields()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 4
eloc 8
c 1
b 0
f 1
nc 4
nop 1
dl 0
loc 11
rs 10
1
<?php namespace Tarsana\Command\Helpers;
2
3
use Tarsana\Syntax\Syntax;
4
5
6
class SyntaxHelper {
7
8
    protected static $instance;
9
10
    public static function instance() : SyntaxHelper
11
    {
12
        if (null === self::$instance)
13
            self::$instance = new SyntaxHelper;
14
        return self::$instance;
15
    }
16
17
    private function __construct() {}
18
19
    public function type(Syntax $syntax) : string
20
    {
21
        $class = explode("\\", get_class($syntax));
22
        return strtolower(substr(array_pop($class), 0, -6));
23
    }
24
25
    public function asString(Syntax $syntax) : string
26
    {
27
        $type = $this->type($syntax);
28
        if ($type == 'optional')
29
            return $this->asString($syntax->syntax());
0 ignored issues
show
Bug introduced by
The method syntax() does not exist on Tarsana\Syntax\Syntax. It seems like you code against a sub-type of Tarsana\Syntax\Syntax such as Tarsana\Syntax\OptionalSyntax or Tarsana\Syntax\ArraySyntax or Debugger. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

29
            return $this->asString($syntax->/** @scrutinizer ignore-call */ syntax());
Loading history...
30
        switch ($type) {
31
            case 'object':
32
                return implode(
33
                    $syntax->separator(),
0 ignored issues
show
Bug introduced by
The method separator() does not exist on Tarsana\Syntax\Syntax. It seems like you code against a sub-type of Tarsana\Syntax\Syntax such as Tarsana\Syntax\ObjectSyntax or Tarsana\Syntax\ArraySyntax. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

33
                    $syntax->/** @scrutinizer ignore-call */ 
34
                             separator(),
Loading history...
34
                    array_keys($syntax->fields())
0 ignored issues
show
Bug introduced by
The method fields() does not exist on Tarsana\Syntax\Syntax. It seems like you code against a sub-type of Tarsana\Syntax\Syntax such as Tarsana\Syntax\ObjectSyntax. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

34
                    array_keys($syntax->/** @scrutinizer ignore-call */ fields())
Loading history...
35
                );
36
            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
37
            case 'array':
38
                $text = $this->asString($syntax->syntax());
39
                return "{$text}{$syntax->separator()}...";
40
            break;
41
            default:
42
                return $type;
43
        }
44
    }
45
46
    public function fields(Syntax $syntax) : array
47
    {
48
        $type = $this->type($syntax);
49
        switch ($type) {
50
            case 'object':
51
                return $syntax->fields();
52
            case 'array':
53
            case 'optional':
54
                return $this->fields($syntax->syntax());
55
        }
56
        return [];
57
    }
58
59
}
60