Skip to content

Commit

Permalink
Add support for enums
Browse files Browse the repository at this point in the history
  • Loading branch information
mdio committed Apr 4, 2024
1 parent 2c403ad commit 1f60489
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
19 changes: 5 additions & 14 deletions src/ClassScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ public function __construct(FilesystemAccess $filesystem, Output $output)
}

/**
* parses all given files for classes and interfaces that are defined or used in this
* files.
* parses all given files for classes and interfaces that are defined or used in these files.
*
* @param array $files
* @param string $root
Expand Down Expand Up @@ -80,7 +79,7 @@ private function cleanContent($fileContent)
{
$fileContent = str_replace('\\\'', ' ', $fileContent);
$fileContent = str_replace('\\"', ' ', $fileContent);
$fileContent = preg_replace("/([a-zA-Z])\'([a-zA-Z])/", '$1$2', $fileContent);
$fileContent = preg_replace("/([a-zA-Z])'([a-zA-Z])/", '$1$2', $fileContent);
$getWhitespaces = function ($count) {
$s = '';
for ($i = 0; $i < $count; ++$i) {
Expand Down Expand Up @@ -115,15 +114,6 @@ private function cleanContent($fileContent)
$fileContent = $cleanWithWhitespaces('/(<<<(?P<tag>_[A-Za-z]+).*(?P=tag);)/sU', $fileContent);
$fileContent = $cleanWithWhitespaces('/(<<<(?P<tag>[A-Za-z_]+).*(?P=tag);)/sU', $fileContent);

if (false) {
$fileContent = preg_replace("/(\/\*.*\*\/)/sU", '', $fileContent);
$fileContent = preg_replace("/(\?>.*<\?)/sU", '', $fileContent);
$fileContent = preg_replace("/(\?>.*$)/sU", '', $fileContent);
$fileContent = preg_replace("/(\'.*\')/sU", '', $fileContent);
$fileContent = preg_replace('/(".*")/sU', '', $fileContent);
$fileContent = preg_replace("/(\/\/.*)/", '', $fileContent);
}

return $fileContent;
}

Expand Down Expand Up @@ -177,9 +167,9 @@ public function parseUsedEntities($file, $namespace, $fileContent, $originalFile
// Extends
$this->parseFileWithRegexForUsedEntities($file, $namespace, $fileContent, $originalFileContent, '/\sextends\s+([a-zA-Z0-9_\\\]+)\W/i', $this->usedEntities, $reservedClassKeywords);
// 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, $reservedClassKeywords);
// 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, $reservedClassKeywords);

// Return Typehints
$this->parseFileWithRegexForUsedEntities(
Expand Down Expand Up @@ -251,6 +241,7 @@ public function parseDefinedEntities($file, $namespace, $fileContent, $originalF
{
$this->parseFileWithRegexForDefinedEntities($file, $namespace, $fileContent, $originalFileContent, '/^\s*(abstract\s+|final\s+)?class\s+([a-zA-Z0-9_]+)\W/mi', $this->definedEntities, 2);
$this->parseFileWithRegexForDefinedEntities($file, $namespace, $fileContent, $originalFileContent, '/^\s*interface\s+([a-zA-Z0-9_]+)\W/mi', $this->definedEntities);
$this->parseFileWithRegexForDefinedEntities($file, $namespace, $fileContent, $originalFileContent, '/^\s*enum\s+([a-zA-Z0-9_]+)\W/mi', $this->definedEntities);
}

public function parseUseStatements($file, $namespace, $fileContent, $originalFileContent)
Expand Down
8 changes: 8 additions & 0 deletions test/rg/tools/phpnsc/ClassScannerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ interface InterfaceTwo
'/root/folder/namespace/InterfaceOne.php' => '
<?php
interface InterfaceOne
',
'/root/folder/namespace/EnumOne.php' => '
<?php
enum EnumOne
{}
',
);
$files = array_keys($this->filesystem->filesystem);
Expand All @@ -75,6 +80,9 @@ interface InterfaceOne
'InterfaceTwo' => array(
'namespaces' => array('vendor\namespace'),
),
'EnumOne' => array(
'namespaces' => array('vendor\namespace'),
),
);

$this->assertEquals($expectedEntities, $this->classScanner->getDefinedEntities());
Expand Down

0 comments on commit 1f60489

Please sign in to comment.