Skip to content

Commit 530ee56

Browse files
committed
Sync changes with Phan 1.0.0
1 parent b5aadf7 commit 530ee56

File tree

6 files changed

+217
-64
lines changed

6 files changed

+217
-64
lines changed

.phan/config.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@
6363
// to make sense of.
6464
'dead_code_detection' => false,
6565

66+
// Set to true in order to attempt to detect unused variables.
67+
// dead_code_detection will also enable unused variable detection.
68+
'unused_variable_detection' => true,
69+
6670
// Run a quick version of checks that takes less
6771
// time
6872
"quick_mode" => false,
@@ -226,13 +230,13 @@
226230
],
227231

228232
// A list of plugin files to execute
229-
'plugins' => array_merge([
233+
'plugins' => [
230234
'AlwaysReturnPlugin',
231235
'DollarDollarPlugin',
232236
'UnreachableCodePlugin',
233237
// NOTE: src/Phan/Language/Internal/FunctionSignatureMap.php mixes value without keys (as return type) with values having keys deliberately.
234238
'DuplicateArrayKeyPlugin',
235239
'PregRegexCheckerPlugin',
236240
'PrintfCheckerPlugin',
237-
], file_exists(__DIR__ . '/plugins/UnusedVariablePlugin.php') ? [__DIR__ . '/plugins/UnusedVariablePlugin.php'] : []),
241+
],
238242
];

ruleset.xml

+2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
<rule ref="PSR1" />
77
<rule ref="PSR2">
88
<exclude name="Generic.Files.LineLength.TooLong" />
9+
<!-- We put class initialization in the same file as methods -->
910
<exclude name="PSR1.Files.SideEffects.FoundWithSymbols" />
1011
</rule>
12+
<rule ref="PSR12" />
1113
<!-- Arbitrary increase of line length limit -->
1214
<rule ref="Generic.Files.LineLength">
1315
<properties>

src/TolerantASTConverter/String_.php renamed to src/TolerantASTConverter/StringUtil.php

+24-18
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
4747
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4848
*/
49-
final class String_
49+
final class StringUtil
5050
{
5151
const REPLACEMENTS = [
5252
'\\' => '\\',
@@ -65,25 +65,28 @@ final class String_
6565
* Parses a string token.
6666
*
6767
* @param string $str String token content
68-
* @param bool $parseUnicodeEscape Whether to parse PHP 7 \u escapes
68+
* @param bool $parse_unicode_escape Whether to parse PHP 7 \u escapes
6969
*
7070
* @return string The parsed string
7171
*/
72-
public static function parse(string $str, bool $parseUnicodeEscape = true) : string {
73-
$bLength = 0;
72+
public static function parse(string $str, bool $parse_unicode_escape = true) : string
73+
{
74+
$binary_length = 0;
7475
if ('b' === $str[0] || 'B' === $str[0]) {
75-
$bLength = 1;
76+
$binary_length = 1;
7677
}
7778

78-
if ('\'' === $str[$bLength]) {
79+
if ('\'' === $str[$binary_length]) {
7980
return \str_replace(
8081
['\\\\', '\\\''],
8182
['\\', '\''],
82-
\substr($str, $bLength + 1, -1)
83+
\substr($str, $binary_length + 1, -1)
8384
);
8485
} else {
8586
return self::parseEscapeSequences(
86-
substr($str, $bLength + 1, -1), '"', $parseUnicodeEscape
87+
substr($str, $binary_length + 1, -1),
88+
'"',
89+
$parse_unicode_escape
8790
);
8891
}
8992
}
@@ -95,23 +98,24 @@ public static function parse(string $str, bool $parseUnicodeEscape = true) : str
9598
*
9699
* @param string $str String without quotes
97100
* @param null|string $quote Quote type
98-
* @param bool $parseUnicodeEscape Whether to parse PHP 7 \u escapes
101+
* @param bool $parse_unicode_escape Whether to parse PHP 7 \u escapes
99102
*
100103
* @return string String with escape sequences parsed
101104
*/
102-
public static function parseEscapeSequences(string $str, $quote, bool $parseUnicodeEscape = true) : string {
105+
public static function parseEscapeSequences(string $str, $quote, bool $parse_unicode_escape = true) : string
106+
{
103107
if (null !== $quote) {
104108
$str = \str_replace('\\' . $quote, $quote, $str);
105109
}
106110

107111
$extra = '';
108-
if ($parseUnicodeEscape) {
112+
if ($parse_unicode_escape) {
109113
$extra = '|u\{([0-9a-fA-F]+)\}';
110114
}
111115

112116
return \preg_replace_callback(
113117
'~\\\\([\\\\$nrtfve]|[xX][0-9a-fA-F]{1,2}|[0-7]{1,3}' . $extra . ')~',
114-
function($matches) {
118+
function ($matches) {
115119
$str = $matches[1];
116120

117121
if (isset(self::REPLACEMENTS[$str])) {
@@ -134,22 +138,24 @@ function($matches) {
134138
* @param int $num Code point
135139
*
136140
* @return string UTF-8 representation of code point
141+
*
142+
* @throws \Error for invalid code points
137143
*/
138-
private static function codePointToUtf8(int $num) : string {
144+
private static function codePointToUtf8(int $num) : string
145+
{
139146
if ($num <= 0x7F) {
140147
return chr($num);
141148
}
142149
if ($num <= 0x7FF) {
143-
return chr(($num>>6) + 0xC0) . chr(($num&0x3F) + 0x80);
150+
return chr(($num >> 6) + 0xC0) . chr(($num & 0x3F) + 0x80);
144151
}
145152
if ($num <= 0xFFFF) {
146-
return chr(($num>>12) + 0xE0) . chr((($num>>6)&0x3F) + 0x80) . chr(($num&0x3F) + 0x80);
153+
return chr(($num >> 12) + 0xE0) . chr((($num >> 6) & 0x3F) + 0x80) . chr(($num & 0x3F) + 0x80);
147154
}
148155
if ($num <= 0x1FFFFF) {
149-
return chr(($num>>18) + 0xF0) . chr((($num>>12)&0x3F) + 0x80)
150-
. chr((($num>>6)&0x3F) + 0x80) . chr(($num&0x3F) + 0x80);
156+
return chr(($num >> 18) + 0xF0) . chr((($num >> 12) & 0x3F) + 0x80)
157+
. chr((($num >> 6) & 0x3F) + 0x80) . chr(($num & 0x3F) + 0x80);
151158
}
152159
throw new Error('Invalid UTF-8 codepoint escape sequence: Codepoint too large');
153160
}
154-
155161
}

src/TolerantASTConverter/TolerantASTConverter.php

+40-24
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,11 @@ public function parseCodeAsPHPAST(string $file_contents, int $version, array &$e
173173
*/
174174
public static function phpParserParse(string $file_contents, array &$errors = null) : PhpParser\Node
175175
{
176+
if (PHP_VERSION_ID >= 70300) {
177+
// TODO: Remove after upgrading to tolerant-php-parser 0.0.13 (https://github.com/Microsoft/tolerant-php-parser/pull/250)
178+
@class_exists(\Microsoft\PhpParser\PhpTokenizer::class);
179+
@class_exists(\Microsoft\PhpParser\Parser::class);
180+
}
176181
$parser = new Parser(); // TODO: In php 7.3, we might need to provide a version, due to small changes in lexing?
177182
$result = $parser->parseSourceFile($file_contents);
178183
$errors = DiagnosticsProvider::getDiagnostics($result);
@@ -270,7 +275,7 @@ private static function phpParserStmtlistToAstNode($parser_nodes, $lineno, bool
270275
foreach ($parser_nodes as $parser_node) {
271276
try {
272277
$child_node = static::phpParserNodeToAstNode($parser_node);
273-
} catch (InvalidNodeException $e) {
278+
} catch (InvalidNodeException $_) {
274279
continue;
275280
}
276281
if (\is_array($child_node)) {
@@ -372,7 +377,7 @@ protected static function phpParserNodeToAstNodeOrPlaceholderExpr($n)
372377

373378
/**
374379
* @param PhpParser\Node|Token $n - The node from PHP-Parser
375-
* @return ast\Node|ast\Node[]|string|int|float|bool|null - whatever ast\parse_code would return as the equivalent.
380+
* @return ast\Node|ast\Node[]|string|int|float|null - whatever ast\parse_code would return as the equivalent.
376381
*/
377382
protected static function phpParserNodeToAstNode($n)
378383
{
@@ -461,7 +466,7 @@ protected static function initHandleMap() : array
461466
'Microsoft\PhpParser\Node\Expression\AssignmentExpression' => function (PhpParser\Node\Expression\AssignmentExpression $n, int $start_line) {
462467
try {
463468
$var_node = static::phpParserNodeToAstNode($n->leftOperand);
464-
} catch (InvalidNodeException $e) {
469+
} catch (InvalidNodeException $_) {
465470
if (self::$should_add_placeholders) {
466471
$var_node = new ast\Node(ast\AST_VAR, 0, ['name' => '__INCOMPLETE_VARIABLE__'], $start_line);
467472
} else {
@@ -886,13 +891,14 @@ protected static function initHandleMap() : array
886891
},
887892
'Microsoft\PhpParser\Node\Parameter' => function (PhpParser\Node\Parameter $n, int $start_line) : ast\Node {
888893
$type_line = static::getEndLine($n->typeDeclaration) ?: $start_line;
894+
$default_node = $n->default !== null ? static::phpParserNodeToAstNode($n->default) : null;
889895
return static::astNodeParam(
890896
$n->questionToken !== null,
891897
$n->byRefToken !== null,
892898
$n->dotDotDotToken !== null,
893899
static::phpParserTypeToAstNode($n->typeDeclaration, $type_line),
894900
static::variableTokenToString($n->variableName),
895-
$n->default !== null ? static::phpParserNodeToAstNode($n->default) : null,
901+
$default_node,
896902
$start_line
897903
);
898904
},
@@ -932,7 +938,7 @@ protected static function initHandleMap() : array
932938
$raw_string = static::tokenToRawString($part);
933939

934940
// Pass in '"\\n"' and get "\n" (somewhat inefficient)
935-
$represented_string = String_::parse($start_quote_text . $raw_string . $end_quote_text);
941+
$represented_string = StringUtil::parse($start_quote_text . $raw_string . $end_quote_text);
936942
$inner_node_parts[] = $represented_string;
937943
}
938944
}
@@ -976,14 +982,14 @@ protected static function initHandleMap() : array
976982
'Microsoft\PhpParser\Node\Statement\BreakOrContinueStatement' => function (PhpParser\Node\Statement\BreakOrContinueStatement $n, int $start_line) : ast\Node {
977983
$kind = $n->breakOrContinueKeyword->kind === TokenKind::ContinueKeyword ? ast\AST_CONTINUE : ast\AST_BREAK;
978984
if ($n->breakoutLevel !== null) {
979-
$breakoutLevel = static::phpParserNodeToAstNode($n->breakoutLevel);
980-
if (!\is_int($breakoutLevel)) {
981-
$breakoutLevel = null;
985+
$breakout_level = static::phpParserNodeToAstNode($n->breakoutLevel);
986+
if (!\is_int($breakout_level)) {
987+
$breakout_level = null;
982988
}
983989
} else {
984-
$breakoutLevel = null;
990+
$breakout_level = null;
985991
}
986-
return new ast\Node($kind, 0, ['depth' => $breakoutLevel], $start_line);
992+
return new ast\Node($kind, 0, ['depth' => $breakout_level], $start_line);
987993
},
988994
'Microsoft\PhpParser\Node\CatchClause' => function (PhpParser\Node\CatchClause $n, int $start_line) : ast\Node {
989995
$qualified_name = $n->qualifiedName;
@@ -1054,12 +1060,22 @@ protected static function initHandleMap() : array
10541060
// This node type is generated for something that isn't a function/constant/property. e.g. "public example();"
10551061
return null;
10561062
},
1063+
/**
1064+
* @throws InvalidNodeException
1065+
*/
10571066
'Microsoft\PhpParser\Node\MethodDeclaration' => function (PhpParser\Node\MethodDeclaration $n, int $start_line) : ast\Node {
10581067
$statements = $n->compoundStatementOrSemicolon;
10591068
$ast_return_type = static::phpParserTypeToAstNode($n->returnType, static::getEndLine($n->returnType) ?: $start_line);
10601069
if (($ast_return_type->children['name'] ?? null) === '') {
10611070
$ast_return_type = null;
10621071
}
1072+
$original_method_name = $n->name;
1073+
if (($original_method_name->kind ?? null) === TokenKind::Name) {
1074+
$method_name = static::tokenToString($original_method_name);
1075+
} else {
1076+
$method_name = 'placeholder_' . $original_method_name->fullStart;
1077+
}
1078+
10631079
if ($n->questionToken !== null && $ast_return_type !== null) {
10641080
$ast_return_type = new ast\Node(ast\AST_NULLABLE_TYPE, 0, ['type' => $ast_return_type], $start_line);
10651081
}
@@ -1074,7 +1090,7 @@ protected static function initHandleMap() : array
10741090
],
10751091
$start_line,
10761092
$n->getDocCommentText(),
1077-
static::variableTokenToString($n->name),
1093+
$method_name,
10781094
static::getEndLine($n),
10791095
self::nextDeclId()
10801096
);
@@ -2157,7 +2173,7 @@ private static function astNodeBinaryop(int $flags, PhpParser\Node\Expression\Bi
21572173
{
21582174
try {
21592175
$left_node = static::phpParserNodeToAstNode($n->leftOperand);
2160-
} catch (InvalidNodeException $e) {
2176+
} catch (InvalidNodeException $_) {
21612177
if (self::$should_add_placeholders) {
21622178
$left_node = static::newPlaceholderExpression($n->leftOperand);
21632179
} else {
@@ -2167,7 +2183,7 @@ private static function astNodeBinaryop(int $flags, PhpParser\Node\Expression\Bi
21672183
}
21682184
try {
21692185
$right_node = static::phpParserNodeToAstNode($n->rightOperand);
2170-
} catch (InvalidNodeException $e) {
2186+
} catch (InvalidNodeException $_) {
21712187
if (self::$should_add_placeholders) {
21722188
$right_node = static::newPlaceholderExpression($n->rightOperand);
21732189
} else {
@@ -2192,7 +2208,7 @@ private static function astNodeAssignop(int $flags, PhpParser\Node\Expression\Bi
21922208
{
21932209
try {
21942210
$var_node = static::phpParserNodeToAstNode($n->leftOperand);
2195-
} catch (InvalidNodeException $e) {
2211+
} catch (InvalidNodeException $_) {
21962212
if (self::$should_add_placeholders) {
21972213
$var_node = new ast\Node(ast\AST_VAR, 0, ['name' => '__INCOMPLETE_VARIABLE__'], $start_line);
21982214
} else {
@@ -2546,14 +2562,14 @@ private static function tokenToScalar(Token $n)
25462562
return $float;
25472563
}
25482564

2549-
return String_::parse($str);
2565+
return StringUtil::parse($str);
25502566
}
25512567

25522568
private static function parseQuotedString(PhpParser\Node\StringLiteral $n) : string
25532569
{
25542570
$start = $n->getStart();
25552571
$text = \substr(self::$file_contents, $start, $n->getEndPosition() - $start);
2556-
return String_::parse($text);
2572+
return StringUtil::parse($text);
25572573
}
25582574

25592575
/**
@@ -2615,10 +2631,10 @@ private static function phpParserClassconstfetchToAstClassconstfetch($scope_reso
26152631
*/
26162632
private static function phpParserNameToString(PhpParser\Node\QualifiedName $name) : string
26172633
{
2618-
$nameParts = $name->nameParts;
2634+
$name_parts = $name->nameParts;
26192635
// TODO: Handle error case (can there be missing parts?)
26202636
$result = '';
2621-
foreach ($nameParts as $part) {
2637+
foreach ($name_parts as $part) {
26222638
$part_as_string = static::tokenToString($part);
26232639
if ($part_as_string !== '') {
26242640
$result .= \trim($part_as_string);
@@ -2639,14 +2655,14 @@ private static function phpParserNameToString(PhpParser\Node\QualifiedName $name
26392655
*/
26402656
private static function newAstDecl(int $kind, int $flags, array $children, int $lineno, string $doc_comment = null, string $name = null, int $end_lineno = 0, int $decl_id = -1) : ast\Node
26412657
{
2642-
$children50 = [];
2643-
$children50['name'] = $name;
2644-
$children50['docComment'] = $doc_comment;
2645-
$children50 += $children;
2658+
$decl_children = [];
2659+
$decl_children['name'] = $name;
2660+
$decl_children['docComment'] = $doc_comment;
2661+
$decl_children += $children;
26462662
if ($decl_id >= 0) {
2647-
$children50['__declId'] = $decl_id;
2663+
$decl_children['__declId'] = $decl_id;
26482664
}
2649-
$node = new ast\Node($kind, $flags, $children50, $lineno);
2665+
$node = new ast\Node($kind, $flags, $decl_children, $lineno);
26502666
if (\is_int($end_lineno)) {
26512667
$node->endLineno = $end_lineno;
26522668
}

0 commit comments

Comments
 (0)