1
|
|
|
<?php |
2
|
|
|
namespace gossi\formatter\entities; |
3
|
|
|
|
4
|
|
|
use phootwork\tokenizer\Token; |
5
|
|
|
|
6
|
|
|
class Block { |
7
|
|
|
|
8
|
|
|
// struct types |
9
|
|
|
const TYPE_CLASS = 'class'; |
10
|
|
|
const TYPE_TRAIT = 'trait'; |
11
|
|
|
const TYPE_INTERFACE = 'interface'; |
12
|
|
|
|
13
|
|
|
// routine types |
14
|
|
|
const TYPE_FUNCTION = 'function'; |
15
|
|
|
const TYPE_METHOD = 'method'; |
16
|
|
|
|
17
|
|
|
// block types |
18
|
|
|
const TYPE_IF = 'if'; |
19
|
|
|
const TYPE_ELSEIF = 'elseif'; |
20
|
|
|
const TYPE_ELSE = 'else'; |
21
|
|
|
const TYPE_DO = 'do'; |
22
|
|
|
const TYPE_WHILE = 'while'; |
23
|
|
|
const TYPE_FOR = 'for'; |
24
|
|
|
const TYPE_FOREACH = 'foreach'; |
25
|
|
|
const TYPE_SWITCH = 'switch'; |
26
|
|
|
const TYPE_USE = 'use'; |
27
|
|
|
const TYPE_NAMESPACE = 'namespace'; |
28
|
|
|
const TYPE_TRY = 'try'; |
29
|
|
|
const TYPE_CATCH = 'catch'; |
30
|
|
|
const TYPE_FINALLY = 'finally'; |
31
|
|
|
|
32
|
|
|
private static $typeMap = [ |
33
|
|
|
T_NAMESPACE => self::TYPE_NAMESPACE, |
34
|
|
|
T_CLASS => self::TYPE_CLASS, |
35
|
|
|
T_TRAIT => self::TYPE_TRAIT, |
36
|
|
|
T_INTERFACE => self::TYPE_INTERFACE, |
37
|
|
|
T_FUNCTION => self::TYPE_FUNCTION, |
38
|
|
|
T_IF => self::TYPE_IF, |
39
|
|
|
T_ELSEIF => self::TYPE_ELSEIF, |
40
|
|
|
T_ELSE => self::TYPE_ELSE, |
41
|
|
|
T_DO => self::TYPE_DO, |
42
|
|
|
T_WHILE => self::TYPE_WHILE, |
43
|
|
|
T_FOR => self::TYPE_FOR, |
44
|
|
|
T_FOREACH => self::TYPE_FOREACH, |
45
|
|
|
T_SWITCH => self::TYPE_SWITCH, |
46
|
|
|
T_USE => self::TYPE_USE, |
47
|
|
|
T_TRY => self::TYPE_TRY, |
48
|
|
|
T_CATCH => self::TYPE_CATCH, |
49
|
|
|
]; |
50
|
|
|
|
51
|
|
|
private static $STRUCTS = [self::TYPE_CLASS, self::TYPE_TRAIT, self::TYPE_INTERFACE]; |
52
|
|
|
private static $ROUTINE = [self::TYPE_FUNCTION, self::TYPE_METHOD]; |
53
|
|
|
private static $BLOCKS = [self::TYPE_IF, self::TYPE_ELSEIF, self::TYPE_ELSE, self::TYPE_DO, |
54
|
|
|
self::TYPE_WHILE, self::TYPE_FOR, self::TYPE_FOREACH, self::TYPE_SWITCH, self::TYPE_USE, |
55
|
|
|
self::TYPE_NAMESPACE]; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* The opening curly brace |
59
|
|
|
* |
60
|
|
|
* @var Token |
61
|
|
|
*/ |
62
|
|
|
public $open = null; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* The closing curly brace |
66
|
|
|
* |
67
|
|
|
* @var Token |
68
|
|
|
*/ |
69
|
|
|
public $close = null; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Start is the initial token of that block |
73
|
|
|
* |
74
|
|
|
* @var Token |
75
|
|
|
*/ |
76
|
|
|
public $start = null; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Start is the last token of that block |
80
|
|
|
* |
81
|
|
|
* @var Token |
82
|
|
|
*/ |
83
|
|
|
public $end = null; |
84
|
|
|
|
85
|
|
|
public $type = ''; |
86
|
|
|
|
87
|
10 |
|
public function __construct($type) { |
88
|
10 |
|
$this->type = $type; |
89
|
10 |
|
} |
90
|
|
|
|
91
|
10 |
|
public function isStruct() { |
92
|
10 |
|
return in_array($this->type, self::$STRUCTS); |
93
|
|
|
} |
94
|
|
|
|
95
|
10 |
|
public function isRoutine() { |
96
|
10 |
|
return in_array($this->type, self::$ROUTINE); |
97
|
|
|
} |
98
|
|
|
|
99
|
2 |
|
public function isBlock() { |
100
|
2 |
|
return in_array($this->type, self::$BLOCKS); |
101
|
|
|
} |
102
|
|
|
|
103
|
10 |
|
public static function getType(Token $token) { |
|
|
|
|
104
|
10 |
|
if (isset(self::$typeMap[$token->type])) { |
105
|
10 |
|
return self::$typeMap[$token->type]; |
106
|
|
|
} else if ($token->contents == 'finally') { |
107
|
|
|
return self::TYPE_FINALLY; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.