Skip to content

Commit 87f4987

Browse files
authored
Merge pull request #198 from spicecodecli/n3-features
N3 features
2 parents acfa24a + 395a40d commit 87f4987

File tree

17 files changed

+840
-55
lines changed

17 files changed

+840
-55
lines changed

cli/commands/analyze.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def analyze_command(file, all, json_output, LANG_FILE):
6363
# load translations
6464
messages = get_translation(LANG_FILE)
6565

66-
# define available stats
66+
# define available stats (updated with new features)
6767
available_stats = [
6868
"line_count",
6969
"function_count",
@@ -73,9 +73,12 @@ def analyze_command(file, all, json_output, LANG_FILE):
7373
"external_dependencies_count",
7474
"method_type_count",
7575
"comment_ratio",
76+
"average_function_size",
77+
"duplicate_code_detection",
78+
"asymptotic_complexity"
7679
]
7780

78-
# dictionary for the stats
81+
# dictionary for the stats (updated with new features)
7982
stats_labels = {
8083
"line_count": messages.get("line_count_option", "Line Count"),
8184
"function_count": messages.get("function_count_option", "Function Count"),
@@ -87,6 +90,9 @@ def analyze_command(file, all, json_output, LANG_FILE):
8790
"private_methods_count": messages.get("private_methods_count_option", "Private Methods Count"),
8891
"public_methods_count": messages.get("public_methods_count_option", "Public Methods Count"),
8992
"comment_ratio": messages.get("comment_ratio_option", "Comment to Code Ratio"),
93+
"average_function_size": messages.get("average_function_size_option", "Average Function Size"),
94+
"duplicate_code_detection": messages.get("duplicate_code_detection_option", "Duplicate Code Detection"),
95+
"asymptotic_complexity": messages.get("asymptotic_complexity_option", "Asymptotic Complexity Analysis")
9096
}
9197

9298
# If --all flag is used, skip the selection menu and use all stats
@@ -156,10 +162,26 @@ def analyze_command(file, all, json_output, LANG_FILE):
156162
print(f"{messages.get('private_methods_count_option', 'Private Methods Count')}: {mtc['private']}")
157163
continue
158164

159-
if stat == "indentation_level" and "indentation_type" in results:
165+
elif stat == "indentation_level" and "indentation_type" in results:
160166
print(f"{messages.get('indentation_type', 'Indentation Type')}: {results.get('indentation_type', 'N/A')}")
161167
print(f"{messages.get('indentation_size', 'Indentation Size')}: {results.get('indentation_size', 'N/A')}")
162168
continue
169+
170+
elif stat == "duplicate_code_detection" and any(key in results for key in ["duplicate_blocks", "duplicate_lines", "duplicate_percentage"]):
171+
print(f"{messages.get('duplicate_blocks', 'Duplicate Blocks')}: {results.get('duplicate_blocks', 'N/A')}")
172+
print(f"{messages.get('duplicate_lines', 'Duplicate Lines')}: {results.get('duplicate_lines', 'N/A')}")
173+
print(f"{messages.get('duplicate_percentage', 'Duplicate Percentage')}: {results.get('duplicate_percentage', 'N/A')}%")
174+
continue
175+
176+
elif stat == "asymptotic_complexity" and any(key in results for key in ["average_complexity", "complexity_distribution", "total_analyzed_functions"]):
177+
print(f"{messages.get('average_complexity', 'Average Complexity')}: {results.get('average_complexity', 'N/A')}")
178+
print(f"{messages.get('total_analyzed_functions', 'Total Analyzed Functions')}: {results.get('total_analyzed_functions', 'N/A')}")
179+
complexity_dist = results.get('complexity_distribution', {})
180+
if complexity_dist:
181+
print(f"{messages.get('complexity_distribution', 'Complexity Distribution')}:")
182+
for complexity, count in complexity_dist.items():
183+
print(f" {complexity}: {count}")
184+
continue
163185

164186
elif stat in results:
165187
print(f"{stats_labels[stat]}: {results[stat]}")

cli/translations/en.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,14 @@
3131
"private_methods_count_option": "Private Methods Count",
3232
"public_methods_count_option": "Public Methods Count",
3333
"comment_ratio_option": "Comment to Code Ratio",
34+
# new
35+
"average_function_size_option": "Average Function Size",
36+
"duplicate_code_detection_option": "Duplicate Code Detection",
37+
"asymptotic_complexity_option": "Asymptotic Complexity Analysis",
38+
"duplicate_blocks": "Duplicate Blocks",
39+
"duplicate_lines": "Duplicate Lines",
40+
"duplicate_percentage": "Duplicate Percentage",
41+
"average_complexity": "Average Complexity",
42+
"total_analyzed_functions": "Total Analyzed Functions",
43+
"complexity_distribution": "Complexity Distribution"
3444
}

spice/analyze.py

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ def analyze_file(file_path: str, selected_stats: Optional[List[str]] = None) ->
1111
file_path (str): Path to the file to analyze
1212
selected_stats (list, optional): List of stats to compute. If None, compute all stats.
1313
Valid stats are: "line_count", "function_count", "comment_line_count",
14-
"inline_comment_count", "indentation_level"
14+
"inline_comment_count", "indentation_level", "external_dependencies_count",
15+
"method_type_count", "comment_ratio", "average_function_size",
16+
"duplicate_code_detection", "asymptotic_complexity"
1517
1618
Returns:
1719
dict: Dictionary containing the requested stats and file information
@@ -34,8 +36,13 @@ def analyze_file(file_path: str, selected_stats: Optional[List[str]] = None) ->
3436
if not ext:
3537
raise ValueError("File has no extension")
3638

37-
# Define valid stats
38-
valid_stats = ["line_count", "function_count", "comment_line_count", "inline_comment_count", "indentation_level", "external_dependencies_count", "method_type_count", "comment_ratio"]
39+
# Define valid stats (including new ones)
40+
valid_stats = [
41+
"line_count", "function_count", "comment_line_count", "inline_comment_count",
42+
"indentation_level", "external_dependencies_count", "method_type_count",
43+
"comment_ratio", "average_function_size", "duplicate_code_detection",
44+
"asymptotic_complexity"
45+
]
3946

4047
# default to all stats if none specified
4148
if selected_stats is None:
@@ -110,8 +117,32 @@ def analyze_file(file_path: str, selected_stats: Optional[List[str]] = None) ->
110117
if "comment_ratio" in selected_stats:
111118
from spice.analyzers.count_comment_ratio import count_comment_ratio
112119
results["comment_ratio"] = count_comment_ratio(file_path)
120+
121+
# NEW FEATURES BELOW
122+
123+
# average function size if requested
124+
if "average_function_size" in selected_stats:
125+
from spice.analyzers.average_function_size import calculate_average_function_size
126+
results["average_function_size"] = calculate_average_function_size(file_path)
127+
128+
# duplicate code detection if requested
129+
if "duplicate_code_detection" in selected_stats:
130+
from spice.analyzers.duplicate_code_detection import get_duplicate_code_summary
131+
duplicate_info = get_duplicate_code_summary(file_path)
132+
results["duplicate_blocks"] = duplicate_info["duplicate_blocks"]
133+
results["duplicate_lines"] = duplicate_info["duplicate_lines"]
134+
results["duplicate_percentage"] = duplicate_info["duplicate_percentage"]
135+
136+
# asymptotic complexity analysis if requested
137+
if "asymptotic_complexity" in selected_stats:
138+
from spice.analyzers.asymptotic_complexity import analyze_asymptotic_complexity
139+
complexity_info = analyze_asymptotic_complexity(file_path)
140+
results["average_complexity"] = complexity_info["average_complexity"]
141+
results["complexity_distribution"] = complexity_info["complexity_distribution"]
142+
results["total_analyzed_functions"] = complexity_info.get("total_functions", 0)
143+
113144
return results
114145

115146
except Exception as e:
116147
# Add context to any errors that occur during analysis
117-
raise Exception(f"Error analyzing file {file_path}: {str(e)}")
148+
raise Exception(f"Error analyzing file {file_path}: {str(e)}")

0 commit comments

Comments
 (0)