Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 36 additions & 15 deletions src/ClassScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,31 @@

class ClassScanner
{
private const RESERVED_CLASS_KEYWORDS = [
// Core reserved keywords
'__halt_compiler', 'abstract', 'and', 'array', 'as', 'break', 'callable',
'case', 'catch', 'class', 'clone', 'const', 'continue', 'declare',
'default', 'die', 'do', 'echo', 'else', 'elseif', 'empty', 'enddeclare',
'endfor', 'endforeach', 'endif', 'endswitch', 'endwhile', 'enum', 'eval',
'exit', 'extends', 'final', 'finally', 'fn', 'for', 'foreach', 'function',
'global', 'goto', 'if', 'implements', 'include', 'include_once',
'instanceof', 'insteadof', 'interface', 'isset', 'list', 'match',
'namespace', 'new', 'or', 'print', 'private', 'protected', 'public',
'readonly', 'require', 'require_once', 'return', 'static', 'switch',
'throw', 'trait', 'try', 'unset', 'use', 'var', 'while', 'xor', 'yield',
// Compile-time magic constants
'__class__', '__dir__', '__file__', '__function__', '__line__',
'__method__', '__namespace__', '__property__', '__trait__',
// Type & relative class keywords
'bool', 'false', 'float', 'int', 'iterable', 'mixed', 'null',
'numeric', 'object', 'parent', 'resource', 'self', 'string', 'true',
];

private const RESERVED_RETURN_TYPES = [
'never',
'void',
];

/**
* @var FilesystemAccess
*/
Expand Down Expand Up @@ -155,21 +180,14 @@ public function getUseStatements($file)

public function parseUsedEntities($file, $namespace, $fileContent, $originalFileContent)
{
$reservedClassKeywords = [
'class', 'parent', 'self', '__class__', 'static', 'array', 'new', 'clone',
'callable', 'string', 'int', 'float', 'bool', 'resource', 'false', 'true',
'null', 'numeric', 'mixed', 'object', 'iterable', 'yield'
];
$reservedReturnTypes = array_merge($reservedClassKeywords, ['void']);

// new operator
$this->parseFileWithRegexForUsedEntities($file, $namespace, $fileContent, $originalFileContent, '/\Wnew\s+([a-zA-Z0-9_\\\]+)/i', $this->usedEntities, $reservedClassKeywords);
$this->parseFileWithRegexForUsedEntities($file, $namespace, $fileContent, $originalFileContent, '/\Wnew\s+([a-zA-Z0-9_\\\]+)/i', $this->usedEntities, self::RESERVED_CLASS_KEYWORDS);
// Extends
$this->parseFileWithRegexForUsedEntities($file, $namespace, $fileContent, $originalFileContent, '/\sextends\s+([a-zA-Z0-9_\\\]+)\W/i', $this->usedEntities, $reservedClassKeywords);
$this->parseFileWithRegexForUsedEntities($file, $namespace, $fileContent, $originalFileContent, '/\sextends\s+([a-zA-Z0-9_\\\]+)\W/i', $this->usedEntities, self::RESERVED_CLASS_KEYWORDS);
// static call
$this->parseFileWithRegexForUsedEntities($file, $namespace, $fileContent, $originalFileContent, '/\W([$a-zA-Z0-9_\\\]+)::/i', $this->usedEntities, $reservedClassKeywords);
$this->parseFileWithRegexForUsedEntities($file, $namespace, $fileContent, $originalFileContent, '/\W(?<!->)([$a-zA-Z0-9_\\\]+)::/i', $this->usedEntities, self::RESERVED_CLASS_KEYWORDS);
// Typehints
$this->parseFileWithRegexForUsedEntities($file, $namespace, $fileContent, $originalFileContent, '/[,(]\s*([a-zA-Z0-9_\\\]+)\s+\$[a-zA-Z0-9_]+/i', $this->usedEntities, $reservedClassKeywords);
$this->parseFileWithRegexForUsedEntities($file, $namespace, $fileContent, $originalFileContent, '/[,(]\s*([a-zA-Z0-9_\\\]+)\s+\$[a-zA-Z0-9_]+/i', $this->usedEntities, self::RESERVED_CLASS_KEYWORDS);

// Return Typehints
$this->parseFileWithRegexForUsedEntities(
Expand All @@ -179,13 +197,13 @@ public function parseUsedEntities($file, $namespace, $fileContent, $originalFile
$originalFileContent,
'/\)\s*:\s*([a-zA-Z0-9_\\\]+)\s*\{/i',
$this->usedEntities,
$reservedReturnTypes
array_merge(self::RESERVED_CLASS_KEYWORDS, self::RESERVED_RETURN_TYPES)
);

// implements
$this->parseFileWithRegexForUsedEntities($file, $namespace, $fileContent, $originalFileContent, '/\simplements\s+([\s,a-zA-Z0-9_\\\]+)\W/i', $this->usedEntities, [], 1, ',');
$this->parseFileWithRegexForUsedEntities($file, $namespace, $fileContent, $originalFileContent, '/\simplements\s+([\s,a-zA-Z0-9_\\\]+)\W/i', $this->usedEntities, self::RESERVED_CLASS_KEYWORDS, 1, ',');
// Instanceof
$this->parseFileWithRegexForUsedEntities($file, $namespace, $fileContent, $originalFileContent, '/\sinstanceof\s+([a-zA-Z0-9_\\\]+)\W/i', $this->usedEntities);
$this->parseFileWithRegexForUsedEntities($file, $namespace, $fileContent, $originalFileContent, '/\sinstanceof\s+([a-zA-Z0-9_\\\]+)\W/i', $this->usedEntities, self::RESERVED_CLASS_KEYWORDS);
}

private function parseFileWithRegexForUsedEntities(
Expand All @@ -205,7 +223,10 @@ private function parseFileWithRegexForUsedEntities(
$matches = [];
preg_match_all($regex, $fileContent, $matches, PREG_OFFSET_CAPTURE);
$checkAndAddMatch = function ($match, $line) use (&$targetArray, $file, $reservedKeywords) {
if (!in_array(strtolower($match), $reservedKeywords)) {
if (substr($match, 0, 1) === '$') {
return;
}
if (!in_array(strtolower($match), $reservedKeywords, true)) {
if (!isset($targetArray[$file][$match])) {
$targetArray[$file][$match] = [];
}
Expand Down
78 changes: 78 additions & 0 deletions test/rg/tools/phpnsc/ClassScannerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,84 @@ function foo(ClassFour $bar) : ClassFive {}
$this->assertEquals($expectedEntities[$file], $this->classScanner->getUsedEntities($file));
}
}

public function testParseUsedEntitiesIgnoresReservedKeywords() {
$file = '/root/folder/namespace/ModernPhp.php';
$this->filesystem->filesystem = [
$file => '<?php
class ModernPhp
{
public function __construct(
private $a,
protected $b,
public $c,
private readonly $d,
RealParamClass $e
) {
$anon = new readonly class {};
$isSelf = $anon instanceof self;
$isStatic = $anon instanceof static;
$isParent = $anon instanceof parent;
$isReal = $anon instanceof RealInstanceofClass;
$matched = match ($a) {
1 => fn ($x) => $x,
default => null,
};
enum::class;
}

public function fail(): never
{
throw new RealException();
}

public function nothing(): void
{
}
}
',
];
$files = array_keys($this->filesystem->filesystem);

$this->classScanner->parseFilesForClassesAndInterfaces($files, '/root/folder', 'vendor');

$expectedEntities = [
'RealParamClass' => [9],
'RealInstanceofClass' => [15],
'RealException' => [25],
];

$this->assertEquals($expectedEntities, $this->classScanner->getUsedEntities($file));
}

public function testParseUsedEntitiesIgnoresClassConstantOnVariablesAndProperties() {
$file = '/root/folder/namespace/DynamicClassAccess.php';
$this->filesystem->filesystem = [
$file => '<?php
class DynamicClassAccess
{
public function test($foo, $task)
{
$a = $foo->task::class;
$b = $foo?->task::class;
$c = $task::class;
$d = RealClass::class;
$e = \FullyQualifiedClass::class;
}
}
',
];
$files = array_keys($this->filesystem->filesystem);

$this->classScanner->parseFilesForClassesAndInterfaces($files, '/root/folder', 'vendor');

$expectedEntities = [
'RealClass' => [9],
'\FullyQualifiedClass' => [10],
];

$this->assertEquals($expectedEntities, $this->classScanner->getUsedEntities($file));
}
}

class ClassScannerFilesystemMock extends FilesystemAccess
Expand Down
Loading