Skip to content

Commit

Permalink
PHP7: support return types and add more reserved keywords
Browse files Browse the repository at this point in the history
  • Loading branch information
Bastian Hofmann committed May 30, 2016
1 parent 6740e4d commit 7c245a1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
16 changes: 15 additions & 1 deletion src/rg/tools/phpnsc/ClassScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,9 @@ public function getUseStatements($file) {

public function parseUsedEntities($file, $namespace, $fileContent, $originalFileContent) {
$reservedClassKeywords = array(
'parent', 'self', '__class__', 'static', 'array', 'new', 'clone', 'callable'
'parent', 'self', '__class__', 'static', 'array', 'new', 'clone',
'callable', 'string', 'int', 'float', 'bool', 'resource', 'false', 'true',
'null', 'numeric', 'mixed', 'object'
);
// new operator
$this->parseFileWithRegexForUsedEntities($file, $namespace, $fileContent, $originalFileContent, '/\Wnew\s+([a-zA-Z0-9_\\\]+)/i', $this->usedEntities, $reservedClassKeywords);
Expand All @@ -173,6 +175,18 @@ public function parseUsedEntities($file, $namespace, $fileContent, $originalFile
$this->parseFileWithRegexForUsedEntities($file, $namespace, $fileContent, $originalFileContent, '/\W([\$a-zA-Z0-9_\\\]+)::/i', $this->usedEntities, $reservedClassKeywords);
// Typehints
$this->parseFileWithRegexForUsedEntities($file, $namespace, $fileContent, $originalFileContent, '/[\,\(]\s*([a-zA-Z0-9_\\\]+)\s+\$[a-zA-Z0-9_]+/i', $this->usedEntities, $reservedClassKeywords);

// Return Typehints
$this->parseFileWithRegexForUsedEntities(
$file,
$namespace,
$fileContent,
$originalFileContent,
'/\)\s*:\s*([a-zA-Z0-9_\\\]+)\s*\{/i',
$this->usedEntities,
$reservedClassKeywords
);

// implements
$this->parseFileWithRegexForUsedEntities($file, $namespace, $fileContent, $originalFileContent, '/\simplements\s+([\s,a-zA-Z0-9_\\\]+)\W/i', $this->usedEntities, array(), 1, ',');
// Instanceof
Expand Down
5 changes: 4 additions & 1 deletion test/rg/tools/phpnsc/ClassScannerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,10 @@ abstract class ClassTwo implements Bar
$foo = new ClassOne(ClassThree::CONSTANT, TypeHintClass $variable);
$bar = new ClassTwo;
$foo = new __CLASS__;
$xyz = new $variable(NotClassCONSTANT, parent::FOO, self::FOO, static::FOO, array $bar);
$xyz = new $variable(NotClassCONSTANT, parent::FOO, self::FOO, static::FOO, array $bar, string $blub, int $bat, $float $baz, callable $bar);
$b = new ClassTwo(ClassTwo::CONSTANT, OutOfNamespace $foo, OtherNamespace $bar, OutOfNamespace::FOO);
interface InterfaceTwo
function foo(ClassFour $bar) : ClassFive {}
',
];
$files = array_keys($this->filesystem->filesystem);
Expand All @@ -207,6 +208,8 @@ interface InterfaceTwo
'TypeHintClass' => [8],
'OtherNamespace' => [12],
'Bar' => [3],
'ClassFour' => [14],
'ClassFive' => [14],
],
];

Expand Down

0 comments on commit 7c245a1

Please sign in to comment.