-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics.js
1240 lines (1170 loc) · 40.5 KB
/
metrics.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const fileHelper = require('./FileHelper/fileElectronHelper');
const typesInDart = [""];
var keywords = [
'dynamic', 'show', 'as', 'import', 'static', 'assert', 'enum', 'in', 'super','async',
'export', 'switch', 'await', 'extends', 'sync', 'external', 'library', 'case', 'factory',
'mixin', 'throw','catch', 'try', 'finally', 'on', 'for', 'operator', 'covariant',
'Function', 'part', 'get','rethrow', 'while', 'deferred', 'hide','with', 'if', 'set', 'yield', 'int', 'float', 'var'
];
function isValidCharacter(character) {
let condition = character=='_';
condition |= character>='a' && character<='z';
condition |= character>='A' && character<='Z';
condition |= character>='0' && character<='9';
return condition;
}
function cleanLineBeforeProcessing(singleline) {
let str=' ';
for(let i=0; i<singleline.length; i++){
if(isValidCharacter(singleline[i]) || singleline[i]==' ') {
str += singleline[i];
} else {
str += ' ';
str += singleline[i];
str += ' ';
}
}
return str;
}
const isABuiltInType = function (name) {
return (name === 'void' || name === 'int' || name === 'double'|| name === 'bool' || name === 'dynamic')
}
const isTypeName = function(name){
return name[0] === name[0].toUpperCase() || (isABuiltInType(name));
}
const isVariableName = function(name){
let valid = true;
for (const character of name) {
if(!isValidCharacter){
valid = false;
break;
}
}
return !keywords.includes(name) && valid;
}
const containsSemicolon = function(line){
let hasFirstQuote = false, hasSecondQuote = false;
for (const c of line) {
if(c === "\'"){
hasFirstQuote = !hasFirstQuote;
}
else if(c === "\""){
hasSecondQuote = !hasSecondQuote;
}
else if(c === ";" && !hasFirstQuote && !hasSecondQuote){
return true;
}
}
return false;
}
function checkArrowFunctions(lines) {
let publicArrowFunc=[], privateArrowFunc=[];
for (let index = 0; index < lines.length; index++) {
let line = "";
if(lines[index]){
line = lines[index].trim();
}
else{
continue;
}
// if(name === "ChatScreen"){
// console.log("Line: " + line);
// }
let splitedLine = line.split(" "), methodName = "";
if(splitedLine[2]){
if(isTypeName(splitedLine[0]) && isVariableName(splitedLine[1]) && (splitedLine[2] === "async" || splitedLine[2][0] === "(")){
if(line.split(")")[1]){
if(line.split(")")[1].trim().startsWith("=>")){
methodName = splitedLine[1]
//console.log("One space");
}
}
}
if(isTypeName(splitedLine[0]) && isVariableName(splitedLine[1].split("(")[0])){
if(line.split(")")[1]){
if(line.split(")")[1].trim().startsWith("=>")){
methodName = splitedLine[1].split("(")[0]
//console.log("One space");
}
}
}
}
if(methodName !== ""){
let methodLines = [];
while(index < lines.length){
methodLines.push(line);
line = lines[index];
if(containsSemicolon(line)){
break;
}
index++;
}
if(methodName.startsWith("_")){
privateArrowFunc.push({methodName: methodName, lines: methodLines});
}
else{
publicArrowFunc.push({methodName: methodName, lines: methodLines});
}
}
}
return {publicArrowFunctions: publicArrowFunc, privateArrowFunctions:privateArrowFunc}
}
function containsDataType(singleline) {
let is_return='';
for(let i=0; i<singleline.length; i++){
if( !(isValidCharacter(singleline[i]) ||
singleline[i]==' ' || singleline[i]==',' ||
singleline[i]=='<' || singleline[i]=='>')
) {
return false;
}
if(isValidCharacter(singleline[i])){
is_return += singleline[i];
} else {
if(is_return=='return') return false;
is_return = '';
}
}
if(is_return=='return') return false;
let datatype_len_cnt=0; // has there any datatype or doesn't.
for(let i=0; i<singleline.length; i++){
if(isValidCharacter(singleline[i])){
datatype_len_cnt++;
} else {
if(datatype_len_cnt>0){
return true;
}
}
}
return false;
}
function reverse_this(str) {
let temp = '';
for(let i=str.length-1; i>=0; i--){
temp += str[i];
}
return temp;
}
function isMethodSignature(line, methods){
line = line.replace(' get ', ' ');
let splitedLine = line.split(" ");
if(splitedLine[1]){
if(methods.includes(splitedLine[1]) && (splitedLine[2] === "async" || splitedLine[2][0] === "("))
return {isMethod: true, methodName: splitedLine[1]};
if(methods.includes(splitedLine[1].split("(")[0]))
return {isMethod: true, methodName: splitedLine[1].split("(")[0]};
}
return {isMethod: false};
}
const isArrowMethod = function(line, arrowMethods){
let splitedLine = line.split(" ");
let arrowMethodNames = arrowMethods.map(method => method.methodName);
if(splitedLine[1]){
if(arrowMethodNames.includes(splitedLine[1]) && (splitedLine[2] === "async" || splitedLine[2][0] === "(")){
let lines = arrowMethods.filter(method => method.methodName === splitedLine[1])[0].lines;
return {isMethod: true, methodName: splitedLine[1], lines: lines};
}
if(arrowMethodNames.includes(splitedLine[1].split("(")[0])){
let lines = arrowMethods.filter(method => method.methodName === splitedLine[1].split("(")[0])[0].lines;
return {isMethod: true, methodName: splitedLine[1].split("(")[0], lines: lines};
}
}
return {isMethod: false};
}
function getMethods(linesInClass, methods, arrowMethods){
let methodsInFile = [];
for (let index = 1; index < linesInClass.length; index++) {
let line = linesInClass[index];
let checkMethod = isMethodSignature(line, methods);
let checkArrowMethod = isArrowMethod(line, arrowMethods);
if(checkMethod.isMethod){
let methodInFile;
let opening2BrkFound = false, bracket=0, endMethod = false;
methodInFile = {name: checkMethod.methodName, lines: []};
while (index < linesInClass.length) {
for (const character of line) {
if(character === '{'){
opening2BrkFound = true;
bracket++;
}
else if(character === '}'){
bracket--;
}
if(opening2BrkFound && bracket === 0){
endMethod = true;
break;
}
}
methodInFile.lines.push(line);
if (endMethod) {
methodsInFile.push(methodInFile);
break;
}
index++;
if(!linesInClass[index])
break;
line = linesInClass[index].trim();
}
}
else if(checkArrowMethod.isMethod){
methodsInFile.push({name: checkArrowMethod.methodName, lines: checkArrowMethod.lines});
}
}
return methodsInFile;
}
function getMethodCount(singleline){
let reverse_method_name = '';
let method_name = '';
for(let i=singleline.length-1; i>=0; i--){
if(singleline[i]==' '){
if(reverse_method_name.length>0) {
break;
} continue;
}
else if(isValidCharacter(singleline[i])){
reverse_method_name += singleline[i];
} else {
return '';
}
}
for(let i=reverse_method_name.length-1; i>=0; i--){
method_name += reverse_method_name[i];
}
for(let i=0; i<keywords.length; i++){
if(method_name==keywords[i]){
return '';
}
}
return method_name;
}
function findMethod(lines){
let public_methods = [];
let private_methods = [];
for(let k=1; k<lines.length; k++) {
if(lines[k].includes(' get ')){
let splited = lines[k].trim().split(' ');
if(splited[2]){
public_methods.push(splited[2]);
continue;
}
}
if(lines[k].includes(' set ')){
let splited = lines[k].trim().split(' ');
if(splited[2]){
public_methods.push(splited[2]);
continue;
}
}
let singleline = cleanLineBeforeProcessing(lines[k]);
let found = false;
let i;
for(i=singleline.length-1; i>=0; i--){
if(singleline[i]==' ') continue;
else {
if(singleline[i]=='{'){
found=true;
} break;
}
}
if(!found) {
continue;
}
let left_bracket_cnt=0, right_bracket_cnt=0;
for(--i; i>=0; i--) {
if(singleline[i]==' ') continue;
if(singleline[i]==')') {
right_bracket_cnt++;
} else if(singleline[i]=='(' && right_bracket_cnt>left_bracket_cnt){
left_bracket_cnt++;
}
if(right_bracket_cnt>0 && right_bracket_cnt==left_bracket_cnt){
break;
}
}
if(!(left_bracket_cnt>0 && right_bracket_cnt>0 && left_bracket_cnt==right_bracket_cnt)){
continue;
}
let method = getMethodCount(singleline.substring(0, i));
if(method.length>0){
if(method[0]=='_'){
private_methods.push(method);
} else {
public_methods.push(method);
}
}
}
return [public_methods, private_methods];
}
function findAttribute(lines) {
let public_attributes = [];
let private_attributes = [];
for(let k=0; k<lines.length; k++){
let singleline = cleanLineBeforeProcessing(lines[k]);
let equalCnt=0;
let semicolon=false;
let skip_line = false;
let pos=singleline.length-1;
for(let i=singleline.length-1; i>=0; i--){
if(singleline[i]=='='){
equalCnt++;
pos=i;
if(equalCnt>1){
skip_line=true;
break;
}
} else if(singleline[i]==';'){
semicolon=true;
}
}
if(skip_line || !semicolon) continue;
let attribute = '';
if(pos!=singleline.length-1) pos--;
for(; pos>=0; pos--){
if(singleline[pos]==';'){
continue;
} else if(singleline[pos]==' '){
if(attribute.length>0){
if(containsDataType(singleline.substring(0, pos+1))){
attribute = reverse_this(attribute);
if(attribute[0]=='_'){
private_attributes.push(attribute);
} else {
public_attributes.push(attribute);
}
}
break;
}
} else if(isValidCharacter(singleline[pos])){
attribute += singleline[pos];
} else {
break;
}
}
}
return [public_attributes, private_attributes];
}
function classIdentify(line) {
let splitted = line.split(" ");
return line.startsWith("class") || (splitted[0] === "abstract" && splitted[0] === "class");
}
function getClassName(line) {
return line.split(" ")[1];
}
function findClasses(lines) {
let classes = [], numberOfClass = 0;
for (let index = 0; index < lines.length; index++) {
let line = lines[index].trim();
if(classIdentify(line)){
let opening2BrkFound = false, bracket=0, endClass = false;
classes[numberOfClass] = {name: getClassName(line), lines: []};
while (index < lines.length) {
for (const character of line) {
if(character === '{'){
opening2BrkFound = true;
bracket++;
}
else if(character === '}'){
bracket--;
}
if(opening2BrkFound && bracket === 0){
endClass = true;
break;
}
}
classes[numberOfClass].lines.push(line);
if (endClass) {
numberOfClass++;
break;
}
index++;
line = lines[index].trim();
}
}
}
return classes;
}
// mood
const calculateMHF = function (linesWithoutCommentsInFiles, fileNames) {
let totalPrivate=0, total=0;
for (let index = 0; index < linesWithoutCommentsInFiles.length; index++) {
//console.log("File name: " + fileNames[index]);
let methods = findMethod(linesWithoutCommentsInFiles[index]);
let numPrivateAttribute = methods[0].length, numTotalAttributes = methods[0].length + methods[1].length;
if (numTotalAttributes === 0) {
continue;
}
totalPrivate += numPrivateAttribute;
total += numTotalAttributes;
}
return total === 0 ? 0.00+"%" : (totalPrivate/total*100).toFixed(2)+"%";
}
const calculateAHF = function (linesWithoutCommentsInFiles) {
let totalPrivate=0, total=0;
for (let index = 0; index < linesWithoutCommentsInFiles.length; index++) {
const lines = linesWithoutCommentsInFiles[index];
let attributes = findAttribute(lines);
let numPrivateAttribute = attributes[0].length, numTotalAttributes = attributes[0].length + attributes[1].length;
if (numTotalAttributes === 0) {
continue;
}
totalPrivate += numPrivateAttribute;
total += numTotalAttributes;
//console.log(attributes);
}
return total === 0 ? 0.00+"%" : (totalPrivate/total*100).toFixed(2)+"%";
}
const calculateMIF = function () {
}
const calculateAIF = function () { }
const calculatePOF = function () { }
const calculateCOF = function () { }
// // //ck
const calculateWMC = function (linesWithoutCommentsInFiles, fileNames) {
let wmc = 0;
let classesInFiles = getClassesFromAllFile(linesWithoutCommentsInFiles, fileNames);
let numberOfClasses = getNumberOfClasses(classesInFiles);
let ccInfos = calculateCC(linesWithoutCommentsInFiles, fileNames)
let ccs = ccInfos.map(ccInfo => ccInfo.cc);
let cc = ccs.reduce((total, current) => {
return total + current
});
wmc = (cc/numberOfClasses).toFixed(2);
return wmc
}
const getClassToMethodAttrMap = function (classesInFiles) {
let classToMethodAttrMap = {};
for (let index = 0; index < classesInFiles.length; index++) {
//console.log(classesInFiles[index]);
const classes = classesInFiles[index].classes;
//console.log("File name : " + classesInFiles[index].fileName);
for (let j = 0; j < classes.length; j++) {
const oneClass = classes[j];
const allMethods = findMethod(oneClass.lines);
let arrowFunctions = checkArrowFunctions(oneClass.lines);
let methods = [...allMethods[0], ...allMethods[1]];
let arrowMethods = [...arrowFunctions.publicArrowFunctions, ...arrowFunctions.privateArrowFunctions];
let methodsInFile = getMethods(oneClass.lines, methods, arrowMethods);
let attributeInFile = findAttribute(oneClass.lines);
//console.log(methodsInFile.map(method => method.name));
const className = oneClass.name;
classToMethodAttrMap[className] = {methods: methodsInFile.map(method => method.name), attributes: attributeInFile[0], lines: oneClass.lines}
}
}
return classToMethodAttrMap;
}
const isAForeignClass = function (testStr, foreignClasses) {
for(const foreignClass of foreignClasses) {
if(testStr.startsWith(foreignClass) && (testStr[foreignClass.length] === ' ' || testStr[foreignClass.length] === ')' || testStr[foreignClass.length] === ';' || testStr[foreignClass.length] === '>')){
return true;
}
}
return false;
}
const hasMethod = function(testStr, method){
return testStr.startsWith(method) && (testStr[method.length] === ' ' || testStr[method.length] === ')' || testStr[method.length] === '(');
}
const hasForeignMethod = function(testStr, foreignClasses, classToMethodAttrMap){
for(const foreignClass of foreignClasses) {
let methods = classToMethodAttrMap[foreignClass].methods;
for (let index = 0; index < methods.length; index++) {
const method = methods[index];
if(hasMethod(testStr, method)){
return true;
}
}
}
return false;
}
const hasForeignAttribute = function(testStr, foreignClasses, classToMethodAttrMap){
for(const foreignClass of foreignClasses) {
let attributes = classToMethodAttrMap[foreignClass].attributes;
for (let index = 0; index < attributes.length; index++) {
const attribute = attributes[index];
if(testStr.startsWith(attribute)){
return true;
}
}
}
return false;
}
const calculateATFD = function (classesInFiles) {
let classToATFDMap = {};
const classToMethodAttrMap = getClassToMethodAttrMap(classesInFiles);
const classNames = Object.keys(classToMethodAttrMap);
for (let index = 0; index < classNames.length; index++) {
const className = classNames[index];
let ATFD = 0;
let foreignClasses = classNames.filter(cn => cn !== className);
let hasFirstQuote = false, hasSecondQuote = false;
let lines = classToMethodAttrMap[className].lines;
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
for (let j = 0; j < line.length-2; j++) {
if(line[j] === "\'"){
hasFirstQuote = !hasFirstQuote;
}
else if(line[j] === "\""){
hasSecondQuote = !hasSecondQuote;
}
else if(line[j] === "n" && !hasFirstQuote && !hasSecondQuote){
let temp = (j+1);
if (temp < line.length) {
if(line[temp] === 'e'){
temp++;
if (temp < line.length) {
if(line[temp] === 'w' && temp+1 < line.length){
let rest = line.substring(temp+1).trim();
if(isAForeignClass(rest, foreignClasses)){
ATFD++;
}
}
}
}
}
}
else if(line[j] === "<" && !hasFirstQuote && !hasSecondQuote){
let rest = line.substring(j+1).trim();
if(isAForeignClass(rest, foreignClasses)){
ATFD++;
}
}
else if(line[j] === "." && !hasFirstQuote && !hasSecondQuote){
let rest = line.substring(j+1);
if(hasForeignMethod(rest, foreignClasses, classToMethodAttrMap) || hasForeignAttribute(rest, foreignClasses, classToMethodAttrMap)){
ATFD++;
}
}
}
}
classToATFDMap[className] = ATFD;
}
//console.log(classToATFDMap);
return classToATFDMap;
}
const calculateDIT = function (childParentMap) {
let classToDITMap = {};
console.log(childParentMap);
let children = Object.keys(childParentMap);
for (const child of children) {
let currentChildDIT = 1;
let currentParent = childParentMap[child];
let tempChildren = [...children];
while(currentParent.length !== 0){
currentChildDIT++;
let currentChild = currentParent[0];
currentParent = [];
console.log("Child: "+child);
//console.log("currentChild: " + currentChild);
//console.log("Child: "+children);
if(tempChildren.includes(currentChild)){
//console.log("h;saf;sfj;slfjsl;fjs;lfjfjlfj;");
currentParent = childParentMap[currentChild];
console.log(currentParent);
}
}
classToDITMap[child] = currentChildDIT;
}
console.log(classToDITMap);
let totalDIT = Object.values(classToDITMap).reduce((total, current) => {
return total + current;
}, 0);
const averageDIT = totalDIT / Object.values(classToDITMap).length;
return averageDIT.toFixed(2);
}
const calculateNOC = function (childParentMap) {
let classToNOPMap = {};
console.log(childParentMap);
let children = Object.keys(childParentMap);
let parents = Object.values(childParentMap).map(value => value[0]);
//console.log(children);
//console.log(parents);
let allClasses = [...children, ...parents];
console.log(allClasses);
function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
}
let allUniqueClasses = allClasses.filter(onlyUnique);
console.log("Number of classes: " + allClasses.length);
console.log("Number of classes set: " + allUniqueClasses.length);
//console.log(allClasses);
//console.log(allUniqueClasses);
for (const parent of parents) {
if(classToNOPMap.hasOwnProperty(parent)){
classToNOPMap[parent]++;
}
else{
classToNOPMap[parent] = 1;
}
}
console.log(classToNOPMap);
let totalNOP = Object.values(classToNOPMap).reduce((total, current) => {
return total + current;
}, 0);
return (totalNOP/allUniqueClasses.length).toFixed(2);
}
// // const calculateCBO = function () { }
// // const calculateRFC = function () { }
// // const calculateLCOM = function () { }
function getClassesFromAllFile(linesInFiles, fileNames){
let classesInFiles = [];
for (let index = 0; index < linesInFiles.length; index++) {
const classes = findClasses(linesInFiles[index]);
//console.log("File name: " + fileNames[index]);
//console.log("classes: ");
//console.log(classes);
classesInFiles.push({fileName: fileNames[index], classes: classes});
}
return classesInFiles;
}
const checkStartsWithMethod = function(part, methodNames){
let methodFound = false, method = "";
for (const methodName of methodNames) {
if(part.startsWith(methodName)){
method = methodName;
methodFound = true;
}
}
return {method: method, methodFound:methodFound};
}
const ccFromOtherMethodCall = function(line, allMethods){
//console.log(allMethods);
let cc = 0, allMethodNames = Object.keys(allMethods), splittedLine;
if(allMethodNames.length === 0)
return 0;
line = line.replace(/ +/g, ' ');
if(line.includes('\"')){
splittedLine = line.split('\"');
}
else if(line.includes('\'')){
splittedLine = line.split('\'');
}
else{
splittedLine = line.split('\"');
}
let i = 0;
while(i < splittedLine.length){
let splitBySpace = splittedLine[i].split(' ');
for (let index = 0; index < splitBySpace.length; index++) {
let splited = splitBySpace[index];
let checkedMethod = checkStartsWithMethod(splited, allMethodNames);
if(checkedMethod.methodFound){
let lines = allMethods[checkedMethod.method];
delete allMethods[checkedMethod.method];
cc += getCCOfAMethod(lines, allMethods);
}
}
i+=2;
}
return cc;
}
function getCCOfAMethod(linesOfMethod, allMethods) {
let nodes = 1, edges = 0, cc = 0;
if(linesOfMethod){
for (let index = 0; index < linesOfMethod.length; index++) {
const line = linesOfMethod[index];
if(line.startsWith("else if") || line.startsWith("if")){
nodes += 2;
edges += 3;
}
else if(line.startsWith("else")){
nodes ++;
edges ++;
}
else if(line.startsWith("while") || line.startsWith("do") || line.startsWith("for")){
nodes += 2;
edges += 4;
}
else if(line.startsWith("case")){
nodes ++;
edges += 2;
}
if(index !== 0)
cc += ccFromOtherMethodCall(line, allMethods);
}
}
cc += (edges - nodes + 2);
return cc;
}
function getAllMethods(methodsInFiles) {
let methods = {};
for (let index = 0; index < methodsInFiles.length; index++) {
const methodsInFile = methodsInFiles[index];
for (let j = 0; j < methodsInFile.methodsInFile.length; j++) {
const method = methodsInFile.methodsInFile[j];
methods[method.name] = method.lines;
}
}
return methods;
}
//traditional
const calculateCC = function (linesWithoutCommentsInFiles, fileNames) {
let classesInFiles = getClassesFromAllFile(linesWithoutCommentsInFiles, fileNames);
let methodsInFiles = getMethodsInFiles(classesInFiles, linesWithoutCommentsInFiles);
//console.log("Methods in Files: ");
//console.log(methodsInFiles);
let allMethods = getAllMethods(methodsInFiles);
let allCCs = [];
for (let index = 0; index < methodsInFiles.length; index++) {
const methodsInFile = methodsInFiles[index];
//console.log("FIle name: " + methodsInFile.fileName);
for (let j = 0; j < methodsInFile.methodsInFile.length; j++) {
const method = methodsInFile.methodsInFile[j];
//console.log("Cyclometic complexity of " + method.name + " is:");
//console.log(allMethods);
let ccOfAMethod = getCCOfAMethod(method.lines, allMethods);
allCCs.push({cc: ccOfAMethod, methodName: method.name});
}
}
return allCCs;
}
const calculateSLOC = function (linesInFiles) {
let totalLines=0;
for (let index = 0; index < linesInFiles.length; index++) {
totalLines += linesInFiles[index].length;
}
return totalLines;
}
const removeKeywords = function(line){
// if(line.startsWith('final ')){
// line = line.replace('final ', '');
// }
// else if(line.startsWith('static ')){
// line = line.replace('static ', '');
// }
line = line.replace(' final ', ' ');
line = line.replace(' static ', ' ');
// line = line.replace(' get ', ' ');
// line = line.replace(' set ', ' ');
return line;
}
const calculateCP = function (linesInFiles, sloc) {
let numberOfCommentLine = 0, linesWithoutCommentsInFiles=[];
for (let index = 0; index < linesInFiles.length; index++) {
const lines = linesInFiles[index];
linesWithoutCommentsInFiles.push([]);
let multiLine = false;
for (let j = 0; j < lines.length; j++) {
if (lines[j].startsWith("//") && !multiLine) {
numberOfCommentLine++;
continue;
}
else if(lines[j].startsWith("/*") && !multiLine){
multiLine = true;
}
if(multiLine){
numberOfCommentLine++;
if (lines[j].endsWith("*/")) {
multiLine = false;
}
}
else{
lines[j] = removeKeywords(lines[j]);
linesWithoutCommentsInFiles[index].push(lines[j])
}
}
}
return {
cp: (numberOfCommentLine/sloc*100).toFixed(2)+"%",
linesWithoutCommentsInFiles: linesWithoutCommentsInFiles
}
}
const getLinesInFiles= function (dartFiles) {
let linesInFiles = [];
for (let index = 0; index < dartFiles.length; index++) {
const path = dartFiles[index].path;
linesInFiles.push(fileHelper.getAllLines(path));
}
return linesInFiles;
}
const combinationOfTwo = function(a) {
let min = 2;
let fn = function(n, src, got, all) {
if (n == 0) {
if (got.length > 0) {
all[all.length] = got;
}
return;
}
for (let j = 0; j < src.length; j++) {
fn(n - 1, src.slice(j + 1), got.concat([src[j]]), all);
}
return;
}
let all = [];
for (let i = min; i < a.length; i++) {
fn(i, a, [], all);
}
all.push(a);
return all.filter(el => el.length === 2);
}
const getIndexFromZeroToN = function(n){
if(n === 0){
console.error("Array size can't be zero");
}
let arr = [];
do {
n--;
arr.push(n);
} while (n > 0);
return arr;
}
function getMethodsInFiles(classesInFiles,linesWithoutCommentsInFiles){
let methodsInFiles = [];
for (let index = 0; index < classesInFiles.length; index++) {
//console.log(classesInFiles[index]);
const classes = classesInFiles[index].classes;
const lines = linesWithoutCommentsInFiles[index];
let currentFileName = classesInFiles[index].fileName;
//console.log("File name : " + classesInFiles[index].fileName);
for (let j = 0; j < classes.length; j++) {
const oneClass = classes[j];
const allMethods = findMethod(oneClass.lines);
if (oneClass.name === "Injector") {
console.log("Methods: ");
console.log(allMethods);
//console.log(oneClass.lines);
}
let arrowFunctions = checkArrowFunctions(oneClass.lines);
let methods = [...allMethods[0], ...allMethods[1]];
let arrowMethods = [...arrowFunctions.publicArrowFunctions, ...arrowFunctions.privateArrowFunctions];
//console.log("Class name: " + oneClass.name);
//console.log(methods);
let methodsInFile = getMethods(oneClass.lines, methods, arrowMethods);
//console.log(methodsInFile.map(method => method.name));
methodsInFiles.push({fileName: currentFileName, className: oneClass.name, methodsInFile: methodsInFile, lines: oneClass.lines})
}
}
return methodsInFiles;
}
const cohesionExistBetweenMethodPairs = function(method1, method2, localAttributes){
let method1Name = method1.name, method2Name = method2.name;
console.log(method1Name + " " + method2Name);
let method1Lines = method1.lines, method2Lines = method2.lines, attributesInMethod1 = [];
let copyLocalAttr = [...localAttributes];
for (let index = 0; index < method1Lines.length; index++) {
let line = method1Lines[index];
line = line.replace('\\\'', '');
line = line.replace('\\\"', '');
line = line.replace(/ +/g, ' ');
if (!line.includes('\'') && !line.includes('\"')) {
let splitedLine = line.split(' ');
for (let j = 0; j < splitedLine.length; j++) {
let splited = splitedLine[j];
if(hasMethod(splited, method2Name)){
console.log("True because: " + method2Name);
return true;
}
for (let k = 0; k < copyLocalAttr.length; k++) {
const localAttribute = copyLocalAttr[k];
splited = splited.replace('(', '');
splited = splited.replace(')', '');
if(splited.startsWith(localAttribute)){
attributesInMethod1.push(localAttribute);
}
}
copyLocalAttr = copyLocalAttr.filter(attr => !attributesInMethod1.includes(attr))
}
}
else{
let splitedLineByQuote = line.includes('\"') ? line.split('\"') : line.split('\'');
for (let i = 0; i < splitedLineByQuote.length; i+=2) {
let splitedLine = splitedLineByQuote[i].split(' ');
for (let j = 0; j < splitedLine.length; j++) {
let splited = splitedLine[j];
if(hasMethod(splited, method2Name)){
console.log("True because: " + method2Name);
return true;
}
for (let k = 0; k < copyLocalAttr.length; k++) {
const localAttribute = copyLocalAttr[k];
splited = splited.replace('(', '');
splited = splited.replace(')', '');
if(splited.startsWith(localAttribute)){
attributesInMethod1.push(localAttribute);
}
}
copyLocalAttr = copyLocalAttr.filter(attr => !attributesInMethod1.includes(attr))
}
}
}
}
for (let index = 0; index < method2Lines.length; index++) {
let line = method2Lines[index];
line = line.replace('\\\'', '');
line = line.replace('\\\"', '');
line = line.replace(/ +/g, ' ');
if (!line.includes('\'') && !line.includes('\"')) {
let splitedLine = line.split(' ');
for (let j = 0; j < splitedLine.length; j++) {
let splited = splitedLine[j];
if(hasMethod(splited, method1Name)){
console.log(method1Name);
return true;
}
if(attributesInMethod1.length > 0){
for (let k = 0; k < attributesInMethod1.length; k++) {
const attributeInMth1 = attributesInMethod1[k];
splited = splited.replace('(', '');
splited = splited.replace(')', '');
if(splited.startsWith(attributeInMth1)){
console.log("true because Attr: " + attributeInMth1);
return true;
}
}
}
}
}
else{
let splitedLineByQuote = line.includes('\"') ? line.split('\"') : line.split('\'');
for (let i = 0; i < splitedLineByQuote.length; i+=2) {
let splitedLine = splitedLineByQuote[i].split(' ');
for (let j = 0; j < splitedLine.length; j++) {
let splited = splitedLine[j];
if(hasMethod(splited, method1Name)){
console.log(method1Name);
return true;
}
if(attributesInMethod1.length > 0){
for (let k = 0; k < attributesInMethod1.length; k++) {
const attributeInMth1 = attributesInMethod1[k];
splited = splited.replace('(', '');
splited = splited.replace(')', '');
if(splited.startsWith(attributeInMth1)){
console.log("true because Attr: " + attributeInMth1);
return true;
}
}
}
}
}
}
}
return false;
}
const calculateTCC = function(classesInFiles, linesWithoutCommentsInFiles){
let classToTCCMap = {};
console.log("calculating TCC...");