From 7c245a1779aaaddb37d5138cee35fb6b6bbcdbdf Mon Sep 17 00:00:00 2001 From: Bastian Hofmann Date: Mon, 30 May 2016 13:52:29 +0200 Subject: [PATCH] PHP7: support return types and add more reserved keywords --- src/rg/tools/phpnsc/ClassScanner.php | 16 +++++++++++++++- test/rg/tools/phpnsc/ClassScannerTest.php | 5 ++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/rg/tools/phpnsc/ClassScanner.php b/src/rg/tools/phpnsc/ClassScanner.php index 26806d2..d2c33fa 100644 --- a/src/rg/tools/phpnsc/ClassScanner.php +++ b/src/rg/tools/phpnsc/ClassScanner.php @@ -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); @@ -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 diff --git a/test/rg/tools/phpnsc/ClassScannerTest.php b/test/rg/tools/phpnsc/ClassScannerTest.php index 536c441..f9fcffc 100644 --- a/test/rg/tools/phpnsc/ClassScannerTest.php +++ b/test/rg/tools/phpnsc/ClassScannerTest.php @@ -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); @@ -207,6 +208,8 @@ interface InterfaceTwo 'TypeHintClass' => [8], 'OtherNamespace' => [12], 'Bar' => [3], + 'ClassFour' => [14], + 'ClassFive' => [14], ], ];