Skip to content

Commit 1f60489

Browse files
committed
Add support for enums
1 parent 2c403ad commit 1f60489

File tree

2 files changed

+13
-14
lines changed

2 files changed

+13
-14
lines changed

src/ClassScanner.php

+5-14
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ public function __construct(FilesystemAccess $filesystem, Output $output)
4242
}
4343

4444
/**
45-
* parses all given files for classes and interfaces that are defined or used in this
46-
* files.
45+
* parses all given files for classes and interfaces that are defined or used in these files.
4746
*
4847
* @param array $files
4948
* @param string $root
@@ -80,7 +79,7 @@ private function cleanContent($fileContent)
8079
{
8180
$fileContent = str_replace('\\\'', ' ', $fileContent);
8281
$fileContent = str_replace('\\"', ' ', $fileContent);
83-
$fileContent = preg_replace("/([a-zA-Z])\'([a-zA-Z])/", '$1$2', $fileContent);
82+
$fileContent = preg_replace("/([a-zA-Z])'([a-zA-Z])/", '$1$2', $fileContent);
8483
$getWhitespaces = function ($count) {
8584
$s = '';
8685
for ($i = 0; $i < $count; ++$i) {
@@ -115,15 +114,6 @@ private function cleanContent($fileContent)
115114
$fileContent = $cleanWithWhitespaces('/(<<<(?P<tag>_[A-Za-z]+).*(?P=tag);)/sU', $fileContent);
116115
$fileContent = $cleanWithWhitespaces('/(<<<(?P<tag>[A-Za-z_]+).*(?P=tag);)/sU', $fileContent);
117116

118-
if (false) {
119-
$fileContent = preg_replace("/(\/\*.*\*\/)/sU", '', $fileContent);
120-
$fileContent = preg_replace("/(\?>.*<\?)/sU", '', $fileContent);
121-
$fileContent = preg_replace("/(\?>.*$)/sU", '', $fileContent);
122-
$fileContent = preg_replace("/(\'.*\')/sU", '', $fileContent);
123-
$fileContent = preg_replace('/(".*")/sU', '', $fileContent);
124-
$fileContent = preg_replace("/(\/\/.*)/", '', $fileContent);
125-
}
126-
127117
return $fileContent;
128118
}
129119

@@ -177,9 +167,9 @@ public function parseUsedEntities($file, $namespace, $fileContent, $originalFile
177167
// Extends
178168
$this->parseFileWithRegexForUsedEntities($file, $namespace, $fileContent, $originalFileContent, '/\sextends\s+([a-zA-Z0-9_\\\]+)\W/i', $this->usedEntities, $reservedClassKeywords);
179169
// static call
180-
$this->parseFileWithRegexForUsedEntities($file, $namespace, $fileContent, $originalFileContent, '/\W([\$a-zA-Z0-9_\\\]+)::/i', $this->usedEntities, $reservedClassKeywords);
170+
$this->parseFileWithRegexForUsedEntities($file, $namespace, $fileContent, $originalFileContent, '/\W([$a-zA-Z0-9_\\\]+)::/i', $this->usedEntities, $reservedClassKeywords);
181171
// Typehints
182-
$this->parseFileWithRegexForUsedEntities($file, $namespace, $fileContent, $originalFileContent, '/[\,\(]\s*([a-zA-Z0-9_\\\]+)\s+\$[a-zA-Z0-9_]+/i', $this->usedEntities, $reservedClassKeywords);
172+
$this->parseFileWithRegexForUsedEntities($file, $namespace, $fileContent, $originalFileContent, '/[,(]\s*([a-zA-Z0-9_\\\]+)\s+\$[a-zA-Z0-9_]+/i', $this->usedEntities, $reservedClassKeywords);
183173

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

256247
public function parseUseStatements($file, $namespace, $fileContent, $originalFileContent)

test/rg/tools/phpnsc/ClassScannerTest.php

+8
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ interface InterfaceTwo
5353
'/root/folder/namespace/InterfaceOne.php' => '
5454
<?php
5555
interface InterfaceOne
56+
',
57+
'/root/folder/namespace/EnumOne.php' => '
58+
<?php
59+
enum EnumOne
60+
{}
5661
',
5762
);
5863
$files = array_keys($this->filesystem->filesystem);
@@ -75,6 +80,9 @@ interface InterfaceOne
7580
'InterfaceTwo' => array(
7681
'namespaces' => array('vendor\namespace'),
7782
),
83+
'EnumOne' => array(
84+
'namespaces' => array('vendor\namespace'),
85+
),
7886
);
7987

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

0 commit comments

Comments
 (0)