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
2 changes: 0 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
],
"require": {
"php": ">=8.1",
"react/async": "~4.3.0",
"react/filesystem": "^0.2@dev",
"phpdocumentor/type-resolver": "~1.7.2 || ~2.0",
"phpdocumentor/reflection-docblock": "~5.3 || ~6.0",
"phpdocumentor/reflection-common": "~2.2",
Expand Down
54 changes: 13 additions & 41 deletions src/voku/SimplePhpParser/Parsers/PhpCodeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor\NameResolver;
use PhpParser\ParserFactory;
use React\Filesystem\Node\FileInterface;
use React\Filesystem\Node\NodeInterface;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use SplFileInfo;
Expand All @@ -20,8 +18,6 @@
use voku\SimplePhpParser\Parsers\Helper\Utils;
use voku\SimplePhpParser\Parsers\Visitors\ASTVisitor;
use voku\SimplePhpParser\Parsers\Visitors\ParentConnector;
use function React\Async\await;
use function React\Promise\all;

final class PhpCodeParser
{
Expand Down Expand Up @@ -265,8 +261,6 @@
$phpCodes = [];
/** @var SplFileInfo[] $phpFileIterators */
$phpFileIterators = [];
/** @var list<\React\Promise\PromiseInterface<array{content: \React\Promise\PromiseInterface<string>, fileName: string, cacheKey: string}>> $phpFilePromises */
$phpFilePromises = [];

// fallback
if (\count($fileExtensions) === 0) {
Expand Down Expand Up @@ -329,45 +323,23 @@
$phpFileArray[$cacheKey] = $path;
}

$phpFileArrayChunks = \array_chunk($phpFileArray, Utils::getCpuCores(), true);
foreach ($phpFileArrayChunks as $phpFileArrayChunk) {
$filesystem = \React\Filesystem\Factory::create();

foreach ($phpFileArrayChunk as $cacheKey => $path) {
$phpFilePromises[] = $filesystem->detect($path)->then(
static function (NodeInterface $node) use ($path, $cacheKey): array {
if (!$node instanceof FileInterface) {
throw new \RuntimeException('Expected a file node for: ' . $path);
}

return [
'content' => $node->getContents()->then(static function (string $contents): string {
return $contents;
}),
'fileName' => $path,
'cacheKey' => $cacheKey,
];
},
function ($e) {
throw $e;
}
);
foreach ($phpFileArray as $cacheKey => $path) {
$content = \file_get_contents($path);
if ($content === false) {
$lastError = \error_get_last();
throw new \RuntimeException('Could not read file: ' . $path . ($lastError !== null ? ' (' . $lastError['message'] . ')' : ''));

Check warning on line 330 in src/voku/SimplePhpParser/Parsers/PhpCodeParser.php

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Define and throw a dedicated exception instead of using a generic one.

See more on https://sonarcloud.io/project/issues?id=voku_Simple-PHP-Code-Parser&issues=AZ17Vk9rXISY38E6-Hf7&open=AZ17Vk9rXISY38E6-Hf7&pullRequest=84
}

/** @var list<array{content: \React\Promise\PromiseInterface<string>, fileName: string, cacheKey: string}> $phpFilePromiseResponses */
$phpFilePromiseResponses = await(all($phpFilePromises));
foreach ($phpFilePromiseResponses as $response) {
$response['content'] = await($response['content']);

assert(is_string($response['content']));
assert(is_string($response['cacheKey']));
assert($response['fileName'] === null || is_string($response['fileName']));
$response = [
'content' => $content,
'fileName' => $path,
'cacheKey' => $cacheKey,
];

@$cache->setItem($response['cacheKey'], $response);
@$cache->setItem($cacheKey, $response);

$phpCodes[$response['cacheKey']]['content'] = $response['content'];
$phpCodes[$response['cacheKey']]['fileName'] = $response['fileName'];
}
$phpCodes[$cacheKey]['content'] = $content;
$phpCodes[$cacheKey]['fileName'] = $path;
}

return $phpCodes;
Expand Down