Skip to content

Commit 0ed8019

Browse files
committed
Duplicates line number showing
1 parent 348a330 commit 0ed8019

File tree

5 files changed

+37
-58
lines changed

5 files changed

+37
-58
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ vendor
22
temp
33
composer.lock
44
.DS_STORE
5-
create-phar.php
5+
generate-phar.php

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ $ phpcloc duplicate directory --ext=php --exclude=vendor
4646
```
4747

4848
## Todo
49-
- Line number show on duplicate code checker command
5049
- Improve algorithm complexity
5150
- Testing
5251

src/Analyzers/DuplicateAnalyzer.php

Lines changed: 18 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -66,62 +66,31 @@ protected function processLines(SplFileObject $file)
6666
$filename = $file->getPathname();
6767

6868
$lines = [];
69-
$totalLines = 0;
70-
$isMultilines = false;
71-
while ($file->valid()) {
72-
$currentLine = $file->fgets();
73-
$trimLine = trim($currentLine);
74-
$lineProperties = [];
75-
76-
// Ignoring the last new line
77-
if ($file->eof() && empty($trimLine)) {
78-
break;
79-
}
80-
81-
$totalLines ++;
82-
if (empty($trimLine)) {
83-
$lineProperties['blank'] = true;
69+
$duplicates = [];
70+
foreach ($file as $line) {
71+
$trimLine = trim($line);
72+
$lineNo = ($file->key() + 1);
73+
74+
if ($foundIndex = array_search($trimLine, array_column($lines, 'code'))) {
75+
$duplicates[] = $lines[$foundIndex]['lineNo'];
76+
$duplicates[] = $lineNo;
8477
}
8578

86-
// Detecting comments
87-
if (strpos($trimLine, '//') === 0
88-
|| strpos($trimLine, '#') === 0) {
89-
$lineProperties['comment'] = true;
90-
}
91-
92-
// Detecting multilines comments
93-
if (strpos($trimLine, '/*') === 0) {
94-
$isMultilines = true;
95-
}
96-
if ($isMultilines) {
97-
$lineProperties['comment'] = true;
98-
}
99-
if (strpos($trimLine, '*/') === 0) {
100-
$isMultilines = false;
79+
if (strlen($trimLine) > 3) {
80+
$lines[] = [
81+
'lineNo' => $lineNo,
82+
'code' => $trimLine,
83+
];
10184
}
102-
103-
$lineProperties['code'] = $currentLine;
104-
$lines[] = $lineProperties;
10585
}
10686

107-
$code = array_filter($lines, function ($line) {
108-
if (isset($line['blank']) || isset($line['comment'])) {
109-
return false;
110-
}
111-
112-
return true;
113-
});
114-
115-
$codeFlatten = array_column($code, 'code');
116-
$totalCode = count($codeFlatten);
117-
$totalUniqueCode = count(array_unique($codeFlatten));
118-
$duplicate = $totalCode - $totalUniqueCode;
119-
120-
if ($duplicate > 0) {
87+
$totalDuplicates = count($duplicates);
88+
if ($totalDuplicates > 0) {
89+
sort($duplicates);
12190
$this->stats[$filename] = [
12291
'file' => $filename,
123-
'line' => $totalLines,
124-
'duplicate' => $duplicate,
92+
'duplicate' => $totalDuplicates,
93+
'line_no' => implode(',', $duplicates),
12594
];
12695
}
12796
}

src/Commands/ClocCommand.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ public function execute(InputInterface $input, OutputInterface $output)
5252
$exclude = $input->getOption('exclude');
5353

5454
$stats = array_values((new ClocAnalyzer($path, $ext, $exclude))->stats());
55+
if (count($stats) === 0) {
56+
$output->writeln('No files found.');
57+
return;
58+
}
59+
5560
array_push(
5661
$stats,
5762
new TableSeparator(),

src/Commands/DuplicateCommand.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Symfony\Component\Console\Output\OutputInterface;
99
use Symfony\Component\Console\Helper\Table;
1010
use Symfony\Component\Console\Helper\TableSeparator;
11+
use Symfony\Component\Console\Helper\TableCell;
1112
use Symfony\Component\Console\Input\InputArgument;
1213
use Symfony\Component\Console\Input\InputOption;
1314

@@ -53,23 +54,28 @@ public function execute(InputInterface $input, OutputInterface $output)
5354
$exclude = $input->getOption('exclude');
5455

5556
$stats = (new DuplicateAnalyzer($path, $ext, $exclude))->stats();
57+
if (count($stats) === 0) {
58+
$output->writeln('No files found.');
59+
return;
60+
}
61+
5662
array_push(
5763
$stats,
5864
new TableSeparator(),
5965
[
6066
'Total',
61-
array_reduce($stats, function ($carry, $item) {
62-
return $carry + $item['line'];
63-
}),
64-
array_reduce($stats, function ($carry, $item) {
65-
return $carry + $item['duplicate'];
66-
}),
67+
new TableCell(
68+
array_reduce($stats, function ($carry, $item) {
69+
return $carry + $item['duplicate'];
70+
}),
71+
['colspan' => 2]
72+
),
6773
]
6874
);
6975

7076
$table = new Table($output);
7177
$table
72-
->setHeaders(['File', 'Line', 'Duplicate'])
78+
->setHeaders(['File', 'Duplicate', 'In Line(s)'])
7379
->setRows($stats)
7480
;
7581
$table->render();

0 commit comments

Comments
 (0)