Skip to content

Commit 2b59ffd

Browse files
authored
feat: improve namespaces PHPFile (#22)
1 parent b27b2bf commit 2b59ffd

File tree

2 files changed

+29
-21
lines changed

2 files changed

+29
-21
lines changed

src/PhpFileCleaner.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ class PhpFileCleaner
5353
public static function setTypeConfig(array $types): void
5454
{
5555
foreach ($types as $type) {
56-
self::$typeConfig[$type[0]] = array(
56+
self::$typeConfig[$type[0]] = [
5757
'name' => $type,
5858
'length' => \strlen($type),
5959
'pattern' => '{.\b(?<![\$:>])'.$type.'\s++[a-zA-Z_\x7f-\xff:][a-zA-Z0-9_\x7f-\xff:\-]*+}Ais',
60-
);
60+
];
6161
}
6262

6363
self::$restPattern = '{[^?"\'</'.implode('', array_keys(self::$typeConfig)).']+}A';
@@ -110,6 +110,7 @@ public function clean(): string
110110
$this->skipToNewline();
111111
continue;
112112
}
113+
113114
if ($this->peek('*')) {
114115
$this->skipComment();
115116
continue;
@@ -122,9 +123,7 @@ public function clean(): string
122123
\substr($this->contents, $this->index, $type['length']) === $type['name']
123124
&& Preg::isMatch($type['pattern'], $this->contents, $match, 0, $this->index - 1)
124125
) {
125-
$clean .= $match[0];
126-
127-
return $clean;
126+
return $clean . $match[0];
128127
}
129128
}
130129

@@ -161,10 +160,12 @@ private function skipString(string $delimiter): void
161160
$this->index += 2;
162161
continue;
163162
}
163+
164164
if ($this->contents[$this->index] === $delimiter) {
165165
$this->index += 1;
166166
break;
167167
}
168+
168169
$this->index += 1;
169170
}
170171
}
@@ -188,6 +189,7 @@ private function skipToNewline(): void
188189
if ($this->contents[$this->index] === "\r" || $this->contents[$this->index] === "\n") {
189190
return;
190191
}
192+
191193
$this->index += 1;
192194
}
193195
}
@@ -214,6 +216,7 @@ private function skipHeredoc(string $delimiter): void
214216

215217
return;
216218
}
219+
217220
break;
218221
}
219222

src/PhpFileParser.php

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
namespace Composer\ClassMapGenerator;
1414

15+
use RuntimeException;
1516
use Composer\Pcre\Preg;
1617

1718
/**
@@ -23,16 +24,17 @@ class PhpFileParser
2324
* Extract the classes in the given file
2425
*
2526
* @param string $path The file to check
26-
* @throws \RuntimeException
27+
* @throws RuntimeException
2728
* @return list<class-string> The found classes
2829
*/
2930
public static function findClasses(string $path): array
3031
{
3132
$extraTypes = self::getExtraTypes();
3233

3334
if (!function_exists('php_strip_whitespace')) {
34-
throw new \RuntimeException('Classmap generation relies on the php_strip_whitespace function, but it has been disabled by the disable_functions directive.');
35+
throw new RuntimeException('Classmap generation relies on the php_strip_whitespace function, but it has been disabled by the disable_functions directive.');
3536
}
37+
3638
// Use @ here instead of Silencer to actively suppress 'unhelpful' output
3739
// @link https://github.com/composer/composer/pull/4886
3840
$contents = @php_strip_whitespace($path);
@@ -43,21 +45,23 @@ public static function findClasses(string $path): array
4345
$message = 'File at "%s" is not readable, check its permissions';
4446
} elseif ('' === trim((string) file_get_contents($path))) {
4547
// The input file was really empty and thus contains no classes
46-
return array();
48+
return [];
4749
} else {
4850
$message = 'File at "%s" could not be parsed as PHP, it may be binary or corrupted';
4951
}
52+
5053
$error = error_get_last();
5154
if (isset($error['message'])) {
5255
$message .= PHP_EOL . 'The following message may be helpful:' . PHP_EOL . $error['message'];
5356
}
54-
throw new \RuntimeException(sprintf($message, $path));
57+
58+
throw new RuntimeException(sprintf($message, $path));
5559
}
5660

5761
// return early if there is no chance of matching anything in this file
5862
Preg::matchAllStrictGroups('{\b(?:class|interface|trait'.$extraTypes.')\s}i', $contents, $matches);
59-
if (0 === \count($matches)) {
60-
return array();
63+
if ([] === $matches) {
64+
return [];
6165
}
6266

6367
$p = new PhpFileCleaner($contents, count($matches[0]));
@@ -71,22 +75,26 @@ public static function findClasses(string $path): array
7175
)
7276
}ix', $contents, $matches);
7377

74-
$classes = array();
78+
$classes = [];
7579
$namespace = '';
7680

77-
for ($i = 0, $len = count($matches['type']); $i < $len; $i++) {
81+
for ($i = 0, $len = count($matches['type']); $i < $len; ++$i) {
7882
if (isset($matches['ns'][$i]) && $matches['ns'][$i] !== '') {
79-
$namespace = str_replace(array(' ', "\t", "\r", "\n"), '', (string) $matches['nsname'][$i]) . '\\';
83+
$namespace = str_replace([' ', "\t", "\r", "\n"], '', (string) $matches['nsname'][$i]) . '\\';
8084
} else {
8185
$name = $matches['name'][$i];
8286
assert(is_string($name));
8387
// skip anon classes extending/implementing
84-
if ($name === 'extends' || $name === 'implements') {
88+
if ($name === 'extends') {
89+
continue;
90+
}
91+
if ($name === 'implements') {
8592
continue;
8693
}
94+
8795
if ($name[0] === ':') {
8896
// This is an XHP class, https://github.com/facebook/xhp
89-
$name = 'xhp'.substr(str_replace(array('-', ':'), array('_', '__'), $name), 1);
97+
$name = 'xhp'.substr(str_replace(['-', ':'], ['_', '__'], $name), 1);
9098
} elseif (strtolower((string) $matches['type'][$i]) === 'enum') {
9199
// something like:
92100
// enum Foo: int { HERP = '123'; }
@@ -101,6 +109,7 @@ public static function findClasses(string $path): array
101109
$name = substr($name, 0, $colonPos);
102110
}
103111
}
112+
104113
/** @var class-string */
105114
$className = ltrim($namespace . $name, '\\');
106115
$classes[] = $className;
@@ -110,9 +119,6 @@ public static function findClasses(string $path): array
110119
return $classes;
111120
}
112121

113-
/**
114-
* @return string
115-
*/
116122
private static function getExtraTypes(): string
117123
{
118124
static $extraTypes = null;
@@ -123,7 +129,7 @@ private static function getExtraTypes(): string
123129
$extraTypes .= '|enum';
124130
}
125131

126-
$extraTypesArray = array_filter(explode('|', $extraTypes), function (string $type) {
132+
$extraTypesArray = array_filter(explode('|', $extraTypes), function (string $type): bool {
127133
return $type !== '';
128134
});
129135
PhpFileCleaner::setTypeConfig(array_merge(['class', 'interface', 'trait'], $extraTypesArray));
@@ -140,7 +146,6 @@ private static function getExtraTypes(): string
140146
*
141147
* @see Composer\Util\Filesystem::isReadable
142148
*
143-
* @param string $path
144149
* @return bool
145150
*/
146151
private static function isReadable(string $path)

0 commit comments

Comments
 (0)