tarsana /
command
| 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
Loading history...
|
|||||||
| 30 | switch ($type) { |
||||||
| 31 | case 'object': |
||||||
| 32 | return implode( |
||||||
| 33 | $syntax->separator(), |
||||||
|
0 ignored issues
–
show
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
Loading history...
|
|||||||
| 34 | array_keys($syntax->fields()) |
||||||
|
0 ignored issues
–
show
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
Loading history...
|
|||||||
| 35 | ); |
||||||
| 36 | break; |
||||||
|
0 ignored issues
–
show
break is not strictly necessary here and could be removed.
The 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 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 |