@@ -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 }
0 commit comments