Completed
Push — master ( c72839...dd8e38 )
by Thomas
02:47
created

TokenMatcher::isCast()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
namespace gossi\formatter\parser;
3
4
use phootwork\collection\Set;
5
use phootwork\tokenizer\Token;
6
7
class TokenMatcher {
0 ignored issues
show
Coding Style introduced by
The property $IMPORT_STATEMENTS is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style introduced by
The property $LINE_CONTEXT is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
8
9
	// Analyzer
10
	private static $PROPERTIES = [T_PRIVATE, T_PUBLIC, T_PROTECTED, T_STATIC, T_VAR, T_ABSTRACT];
0 ignored issues
show
Unused Code introduced by
The property $PROPERTIES is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
11
	private static $IDENTIFIER = [T_CONST, T_NAMESPACE, T_USE];
0 ignored issues
show
Unused Code introduced by
The property $IDENTIFIER is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
12
13
	// Tokenizer
14
	public static $IMPORT_STATEMENTS = [T_REQUIRE, T_REQUIRE_ONCE, T_INCLUDE, T_INCLUDE_ONCE];
15
16
	/**
17
	 * Keyswords that are followed by a single space
18
	*/
19
	public static $KEYWORDS = [T_ABSTRACT, T_CASE, T_CLASS, T_FUNCTION, T_CLONE, T_CONST, T_EXTENDS, T_FINAL, T_GLOBAL, T_IMPLEMENTS, T_INTERFACE, T_NAMESPACE, T_NEW, T_PRIVATE, T_PUBLIC, T_PROTECTED, T_THROW, T_TRAIT, T_USE];
20
	public static $BLOCKS = [T_IF, T_ELSEIF, T_ELSE, T_FOR, T_FOREACH, T_WHILE, T_DO, T_SWITCH, T_TRY, T_CATCH];
21
	public static $CASTS = [T_ARRAY_CAST, T_BOOL_CAST, T_DOUBLE_CAST, T_INT_CAST, T_OBJECT_CAST, T_STRING_CAST, T_UNSET_CAST];
22
	// 	public static $ASSIGNMENTS = [T_AND_EQUAL, T_CONCAT_EQUAL, T_DIV_EQUAL, T_MINUS_EQUAL, T_MOD_EQUAL, T_MUL_EQUAL, T_OR_EQUAL, T_PLUS_EQUAL, T_SL_EQUAL, T_SR_EQUAL, T_XOR_EQUAL];
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
23
	// 	public static $OPERATORS = [T_BOOLEAN_AND, T_BOOLEAN_OR, T_INSTANCEOF, T_IS_EQUAL, T_IS_GREATER_OR_EQUAL, T_IS_IDENTICAL, T_IS_NOT_EQUAL, T_IS_NOT_IDENTICAL, T_IS_SMALLER_OR_EQUAL, T_LOGICAL_AND, T_LOGICAL_OR, T_LOGICAL_XOR, T_SL, T_SR];
0 ignored issues
show
Unused Code Comprehensibility introduced by
37% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
24
	public static $ASSIGNMENTS = ['=', '&=', '.=', '/=', '-=', '%=', '*=', '|=', '+=', '**=', '<<=', '>>=', '^='];
25
	public static $OPERATORS = ['&', '&&', 'and', '|', '||', 'or', '^', 'xor', 'instanceof', '==', '>=', '>', '===', '!=', '<>', '!==', '<=', '<', '<<', '>>', '+', '-', '*', '/', '**', 'as'];
26
	public static $STRUCTURAL = [T_CLASS, T_INTERFACE, T_TRAIT, T_NAMESPACE, T_USE, T_FUNCTION];
27
	public static $STRUCTS = [T_CLASS, T_INTERFACE, T_TRAIT];
28
	public static $LINE_CONTEXT = ['echo', 'global', 'static', 'yield', 'case'];
29
30
	/** @var Set */
31
	private $keywords;
32
	/** @var Set */
33
	private $blocks;
34
	/** @var Set */
35
	private $casts;
36
	/** @var Set */
37
	private $assignments;
38
	/** @var Set */
39
	private $operators;
40
	/** @var Set */
41
	private $structural;
42
	/** @var Set */
43
	private $structs;
44
	/** @var Set */
45
	private $lineContext;
46
	/** @var Set */
47
	private $imports;
48
	/** @var Set */
49
	private $unitIdentifier;
50
	/** @var Set */
51
	private $modifier;
52
53 10
	public function __construct() {
54 10
		$this->keywords = new Set([T_ABSTRACT, T_CASE, T_CLASS, T_FUNCTION, T_CLONE, T_CONST,
55 10
				T_EXTENDS, T_FINAL, T_GLOBAL, T_IMPLEMENTS, T_INTERFACE, T_NAMESPACE, T_NEW,
56 10
				T_PRIVATE, T_PUBLIC, T_PROTECTED, T_THROW, T_TRAIT, T_USE]);
57
58 10
		$this->blocks = new Set([T_IF, T_ELSEIF, T_ELSE, T_FOR, T_FOREACH, T_WHILE,
59 10
				T_DO, T_SWITCH, T_TRY, T_CATCH, T_CLASS, T_INTERFACE, T_TRAIT,
60 10
				T_NAMESPACE, T_USE, T_FUNCTION]);
61
62 10
		$this->casts = new Set([T_ARRAY_CAST, T_BOOL_CAST, T_DOUBLE_CAST, T_INT_CAST,
63 10
				T_OBJECT_CAST, T_STRING_CAST, T_UNSET_CAST]);
64
65 10
		$this->assignments = new Set(['=', '&=', '.=', '/=', '-=', '%=', '*=', '|=', '+=',
66 10
				'**=', '<<=', '>>=', '^=']);
67
68 10
		$this->operators = new Set(['&', '&&', 'and', '|', '||', 'or', '^', 'xor',
69 10
				'instanceof', '==', '>=', '>', '===', '!=', '<>', '!==', '<=', '<', '<<',
70 10
				'>>', '+', '-', '*', '/', '**', 'as']);
71
72 10
		$this->structural = new Set([T_CLASS, T_INTERFACE, T_TRAIT, T_NAMESPACE, T_USE, T_FUNCTION]);
73
74 10
		$this->structs = new Set([T_CLASS, T_INTERFACE, T_TRAIT]);
75
76 10
		$this->lineContext = new Set(['echo', 'global', 'static', 'yield', 'case']);
77
78 10
		$this->imports = new Set([T_REQUIRE, T_REQUIRE_ONCE, T_INCLUDE, T_INCLUDE_ONCE]);
79
80 10
		$this->unitIdentifier = new Set([T_CONST, T_NAMESPACE, T_USE]);
81 10
		$this->unitIdentifier->addAll($this->imports);
82
83 10
		$this->modifier = new Set([T_PRIVATE, T_PUBLIC, T_PROTECTED, T_STATIC, T_VAR, T_ABSTRACT]);
84 10
	}
85
86 2
	public function isKeyword(Token $token) {
87 2
		return $this->keywords->contains($token->type);
88
	}
89
90 10
	public function isBlock(Token $token) {
91 10
		return $this->blocks->contains($token->type);
92
	}
93
94
	public function isCast(Token $token) {
95
		return $this->blocks->contains($token->type);
96
	}
97
98 2
	public function isAssignment(Token $token) {
99 2
		return $this->assignments->contains($token->contents);
100
	}
101
102 10
	public function isOperator(Token $token) {
103 10
		return $this->operators->contains($token->contents);
104
	}
105
106
	public function isStruct(Token $token) {
107
		return $this->operators->contains($token->type);
108
	}
109
110 10
	public function isLineContext(Token $token) {
111 10
		return $this->lineContext->contains($token->contents);
112
	}
113
114
	public function isImport(Token $token) {
115
		return $this->imports->contains($token->type);
116
	}
117
118 10
	public function isUnitIdentifier(Token $token) {
119 10
		return $this->unitIdentifier->contains($token->type);
120
	}
121
122 10
	public function isModifier(Token $token) {
123 10
		return $this->modifier->contains($token->type);
124
	}
125
126
}
127