Skip to content

Commit

Permalink
Refactor PullRequestCodeScanner for Improved Comment Reporting (#608)
Browse files Browse the repository at this point in the history
* Update PullRequestCodeScanner.php

* style: format code with PHP CS Fixer

This commit fixes the style issues introduced in a97001c according to the output
from PHP CS Fixer.

Details: #608

* Update PullRequestCodeScanner.php

* Update PullRequestCodeScanner.php

---------

Co-authored-by: deepsource-autofix[bot] <62050782+deepsource-autofix[bot]@users.noreply.github.com>
  • Loading branch information
guibranco and deepsource-autofix[bot] authored Oct 8, 2024
1 parent 2b3f4a5 commit b8fce37
Showing 1 changed file with 29 additions and 19 deletions.
48 changes: 29 additions & 19 deletions Src/Library/PullRequestCodeScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,48 @@

class PullRequestCodeScanner
{
public function scanDiffForKeywords($diff): array
public function scanDiffForKeywords($diffContent): array
{
$keywords = ['bug', 'todo', 'fixme'];
$linesWithKeywords = [];
$diffLines = explode("\n", $diff);

foreach ($diffLines as $line) {
if (strpos($line, '+') === 0) {
$cleanedLine = trim(substr($line, 1));
if (preg_match('/(\/\/|#|\/\*)/i', $cleanedLine)) {
foreach ($keywords as $keyword) {
if (stripos($cleanedLine, $keyword) !== false) {
$linesWithKeywords[] = $cleanedLine;
break;
}
$lines = explode(PHP_EOL, $diffContent);
$files = [];
$currentFile = null;

$commentPatterns = [
'/\bbug\b/i',
'/\bfixme\b/i',
'/\btodo\b/i',
];

foreach ($lines as $line) {
if (preg_match('/^\+\+\+ b\/(.+)/', $line, $matches)) {
$currentFile = $matches[1];
}

if ($currentFile && preg_match('/^\+(.*)/', $line, $matches)) {
$codeLine = $matches[1];
foreach ($commentPatterns as $pattern) {
if (preg_match($pattern, $codeLine)) {
$files[$currentFile][] = trim($codeLine);
break;
}
}
}
}

return $linesWithKeywords;
return $files;
}

public function generateReport(array $foundComments): string
public function generateReport(array $files): string
{
if (empty($foundComments)) {
if (empty($files)) {
return "No 'bug', 'todo', or 'fixme' comments found in the pull request.";
}

$report = "Found the following comments with 'bug', 'todo', or 'fixme':\n";
foreach ($foundComments as $comment) {
$report .= "- $comment\n";
foreach ($files as $file => $lines) {
foreach ($lines as $line) {
$report .= "- {$line} ({$file})\n";
}
}

return $report;
Expand Down

0 comments on commit b8fce37

Please sign in to comment.