Skip to content
Open
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
9 changes: 7 additions & 2 deletions src/CodeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,18 @@ public function parseCode(string $code, string $modifiedLines, string $fileType)
$traverser = new NodeTraverser();
$traverser->addVisitor($visitor);
$traverser->traverse($ast);

// Format output according to required structure
return [
$result = [
'methods' => $visitor->getMethods(),
'classes' => $visitor->getClasses(),
'imports' => $visitor->getImports()
];

// Clean up visitor to free memory for large files
$visitor->cleanup();

return $result;

} catch (Error $e) {
throw new \Exception("Parse error: {$e->getMessage()}");
Expand Down
39 changes: 35 additions & 4 deletions src/CodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,16 @@ class CodeVisitor extends NodeVisitorAbstract
* @var string Original code
*/
private $code;

/**
* @var array Modified line ranges
*/
private $modifiedLineRanges;

/**
* @var array|null Cached code lines (lazy loaded)
*/
private $codeLines = null;

/**
* Constructor
Expand Down Expand Up @@ -184,18 +189,34 @@ private function extractMethodData(Node\FunctionLike $node)
];
}

/**
* Get code lines array (lazy loaded and cached)
*
* @return array Array of code lines
*/
private function getCodeLines(): array
{
if ($this->codeLines === null) {
$this->codeLines = explode("\n", $this->code);
}
return $this->codeLines;
}

/**
* Extract content of a node from original code
* Optimized to handle large files by caching line splits
*
* @param Node $node AST node
* @return string Node content
*/
private function extractNodeContent(Node $node)
private function extractNodeContent(Node $node): string
{
$startLine = $node->getStartLine();
$endLine = $node->getEndLine();
$codeLines = explode("\n", $this->code);


// Get cached code lines (only splits once per visitor instance)
$codeLines = $this->getCodeLines();

// Lines are 1-indexed in the parser, but arrays are 0-indexed
$relevantLines = array_slice($codeLines, $startLine - 1, $endLine - $startLine + 1);
return implode("\n", $relevantLines);
Expand Down Expand Up @@ -299,4 +320,14 @@ public function getImports()
{
return $this->imports;
}

/**
* Clean up cached data to free memory
* Call this after getting all results for large files
*/
public function cleanup(): void
{
$this->codeLines = null;
$this->code = '';
}
}
4 changes: 2 additions & 2 deletions src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ private function handleParseRequest()
$requestData['fileType']
);

// Return result
// Return result with optimized JSON encoding for large files
http_response_code(200);
echo json_encode($result);
echo json_encode($result, JSON_PARTIAL_OUTPUT_ON_ERROR);
} catch (\Exception $e) {
http_response_code(500);
echo json_encode(['error' => $e->getMessage()]);
Expand Down