Skip to content

Commit dce177a

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents daa503d + 099d0b1 commit dce177a

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

src/Token.php

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function getText(string $document = null) {
3535
return substr($document, $this->start, $this->length - ($this->start - $this->fullStart));
3636
}
3737

38-
public function getFullText(string & $document) : string {
38+
public function getFullText(string $document) : string {
3939
return substr($document, $this->fullStart, $this->length);
4040
}
4141

@@ -59,15 +59,25 @@ public function getEndPosition() {
5959
return $this->fullStart + $this->length;
6060
}
6161

62-
public static function getTokenKindNameFromValue($kindName) {
63-
$constants = (new \ReflectionClass("Microsoft\\PhpParser\\TokenKind"))->getConstants();
64-
foreach ($constants as $name => $val) {
65-
if ($val == $kindName) {
66-
$kindName = $name;
67-
break;
68-
}
62+
/**
63+
* @return string[] - A hash map of the format [int $tokenKind => string $tokenName]
64+
*/
65+
private static function getTokenKindNameFromValueMap() {
66+
static $mapToKindName;
67+
if ($mapToKindName === null) {
68+
$constants = (new \ReflectionClass("Microsoft\\PhpParser\\TokenKind"))->getConstants();
69+
$mapToKindName = \array_flip($constants);
6970
}
70-
return $kindName;
71+
return $mapToKindName;
72+
}
73+
74+
/**
75+
* @param int $kind
76+
* @return string (Or int, if the kind name for $kind wasn't found)
77+
*/
78+
public static function getTokenKindNameFromValue($kind) {
79+
$mapToKindName = self::getTokenKindNameFromValueMap();
80+
return $mapToKindName[$kind] ?? $kind;
7181
}
7282

7383
public function jsonSerialize() {
@@ -76,7 +86,7 @@ public function jsonSerialize() {
7686
if (!isset($GLOBALS["SHORT_TOKEN_SERIALIZE"])) {
7787
$GLOBALS["SHORT_TOKEN_SERIALIZE"] = false;
7888
}
79-
89+
8090
if ($GLOBALS["SHORT_TOKEN_SERIALIZE"]) {
8191
return [
8292
"kind" => $kindName,

tests/ParserFrameworkValidationTests.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ public function frameworkErrorProvider() {
1818
$iterator = new RecursiveDirectoryIterator(__DIR__ . "/../validation/frameworks/" . $frameworkName);
1919

2020
foreach (new RecursiveIteratorIterator($iterator) as $file) {
21-
if (strpos($file, ".php") !== false) {
21+
$pathName = $file->getPathname();
22+
if (preg_match('/\.php$/', $pathName) > 0) {
23+
// Include files ending in ".php", but don't include XML(foo.phpunit.xml) or binary files (foo.php.gz)
2224
$totalSize += $file->getSize();
23-
$testProviderArray[$frameworkName . "::" . $file->getBasename()] = [$file->getPathname(), $frameworkName];
25+
$testProviderArray[$frameworkName . "::" . $file->getBasename()] = [$pathName, $frameworkName];
2426
}
2527
}
2628
}
@@ -33,7 +35,7 @@ public function frameworkErrorProvider() {
3335
/**
3436
* @dataProvider frameworkErrorProvider
3537
*/
36-
public function testFramworkErrors($testCaseFile, $frameworkName) {
38+
public function testFrameworkErrors($testCaseFile, $frameworkName) {
3739
$fileContents = file_get_contents($testCaseFile);
3840
$parser = new \Microsoft\PhpParser\Parser();
3941
$sourceFile = $parser->parseSourceFile($fileContents);
@@ -51,8 +53,8 @@ public function testFramworkErrors($testCaseFile, $frameworkName) {
5153
foreach ($sourceFile->getDescendantNodesAndTokens() as $child) {
5254
if ($child instanceof Token) {
5355
$this->assertNotEquals(\Microsoft\PhpParser\TokenKind::Unknown, $child->kind, "input: $testCaseFile\r\nexpected: ");
54-
$this->assertNotTrue($child instanceof \Microsoft\PhpParser\SkippedToken, "input: $testCaseFile\r\nexpected: ");
55-
$this->assertNotTrue($child instanceof \Microsoft\PhpParser\MissingToken, "input: $testCaseFile\r\nexpected: ");
56+
$this->assertNotInstanceOf(\Microsoft\PhpParser\SkippedToken::class, $child, "input: $testCaseFile\r\nexpected: ");
57+
$this->assertNotInstanceOf(\Microsoft\PhpParser\MissingToken::class, $child, "input: $testCaseFile\r\nexpected: ");
5658
}
5759
}
5860

0 commit comments

Comments
 (0)