Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions cli/commands/analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def analyze_command(file, all, json_output, LANG_FILE):
# load translations
messages = get_translation(LANG_FILE)

# define available stats
# define available stats (updated with new features)
available_stats = [
"line_count",
"function_count",
Expand All @@ -73,9 +73,12 @@ def analyze_command(file, all, json_output, LANG_FILE):
"external_dependencies_count",
"method_type_count",
"comment_ratio",
"average_function_size",
"duplicate_code_detection",
"asymptotic_complexity"
]

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

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

if stat == "indentation_level" and "indentation_type" in results:
elif stat == "indentation_level" and "indentation_type" in results:
print(f"{messages.get('indentation_type', 'Indentation Type')}: {results.get('indentation_type', 'N/A')}")
print(f"{messages.get('indentation_size', 'Indentation Size')}: {results.get('indentation_size', 'N/A')}")
continue

elif stat == "duplicate_code_detection" and any(key in results for key in ["duplicate_blocks", "duplicate_lines", "duplicate_percentage"]):
print(f"{messages.get('duplicate_blocks', 'Duplicate Blocks')}: {results.get('duplicate_blocks', 'N/A')}")
print(f"{messages.get('duplicate_lines', 'Duplicate Lines')}: {results.get('duplicate_lines', 'N/A')}")
print(f"{messages.get('duplicate_percentage', 'Duplicate Percentage')}: {results.get('duplicate_percentage', 'N/A')}%")
continue

elif stat == "asymptotic_complexity" and any(key in results for key in ["average_complexity", "complexity_distribution", "total_analyzed_functions"]):
print(f"{messages.get('average_complexity', 'Average Complexity')}: {results.get('average_complexity', 'N/A')}")
print(f"{messages.get('total_analyzed_functions', 'Total Analyzed Functions')}: {results.get('total_analyzed_functions', 'N/A')}")
complexity_dist = results.get('complexity_distribution', {})
if complexity_dist:
print(f"{messages.get('complexity_distribution', 'Complexity Distribution')}:")
for complexity, count in complexity_dist.items():
print(f" {complexity}: {count}")
continue

elif stat in results:
print(f"{stats_labels[stat]}: {results[stat]}")
Expand Down
10 changes: 10 additions & 0 deletions cli/translations/en.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,14 @@
"private_methods_count_option": "Private Methods Count",
"public_methods_count_option": "Public Methods Count",
"comment_ratio_option": "Comment to Code Ratio",
# new
"average_function_size_option": "Average Function Size",
"duplicate_code_detection_option": "Duplicate Code Detection",
"asymptotic_complexity_option": "Asymptotic Complexity Analysis",
"duplicate_blocks": "Duplicate Blocks",
"duplicate_lines": "Duplicate Lines",
"duplicate_percentage": "Duplicate Percentage",
"average_complexity": "Average Complexity",
"total_analyzed_functions": "Total Analyzed Functions",
"complexity_distribution": "Complexity Distribution"
}
39 changes: 35 additions & 4 deletions spice/analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ def analyze_file(file_path: str, selected_stats: Optional[List[str]] = None) ->
file_path (str): Path to the file to analyze
selected_stats (list, optional): List of stats to compute. If None, compute all stats.
Valid stats are: "line_count", "function_count", "comment_line_count",
"inline_comment_count", "indentation_level"
"inline_comment_count", "indentation_level", "external_dependencies_count",
"method_type_count", "comment_ratio", "average_function_size",
"duplicate_code_detection", "asymptotic_complexity"

Returns:
dict: Dictionary containing the requested stats and file information
Expand All @@ -34,8 +36,13 @@ def analyze_file(file_path: str, selected_stats: Optional[List[str]] = None) ->
if not ext:
raise ValueError("File has no extension")

# Define valid stats
valid_stats = ["line_count", "function_count", "comment_line_count", "inline_comment_count", "indentation_level", "external_dependencies_count", "method_type_count", "comment_ratio"]
# Define valid stats (including new ones)
valid_stats = [
"line_count", "function_count", "comment_line_count", "inline_comment_count",
"indentation_level", "external_dependencies_count", "method_type_count",
"comment_ratio", "average_function_size", "duplicate_code_detection",
"asymptotic_complexity"
]

# default to all stats if none specified
if selected_stats is None:
Expand Down Expand Up @@ -110,8 +117,32 @@ def analyze_file(file_path: str, selected_stats: Optional[List[str]] = None) ->
if "comment_ratio" in selected_stats:
from spice.analyzers.count_comment_ratio import count_comment_ratio
results["comment_ratio"] = count_comment_ratio(file_path)

# NEW FEATURES BELOW

# average function size if requested
if "average_function_size" in selected_stats:
from spice.analyzers.average_function_size import calculate_average_function_size
results["average_function_size"] = calculate_average_function_size(file_path)

# duplicate code detection if requested
if "duplicate_code_detection" in selected_stats:
from spice.analyzers.duplicate_code_detection import get_duplicate_code_summary
duplicate_info = get_duplicate_code_summary(file_path)
results["duplicate_blocks"] = duplicate_info["duplicate_blocks"]
results["duplicate_lines"] = duplicate_info["duplicate_lines"]
results["duplicate_percentage"] = duplicate_info["duplicate_percentage"]

# asymptotic complexity analysis if requested
if "asymptotic_complexity" in selected_stats:
from spice.analyzers.asymptotic_complexity import analyze_asymptotic_complexity
complexity_info = analyze_asymptotic_complexity(file_path)
results["average_complexity"] = complexity_info["average_complexity"]
results["complexity_distribution"] = complexity_info["complexity_distribution"]
results["total_analyzed_functions"] = complexity_info.get("total_functions", 0)

return results

except Exception as e:
# Add context to any errors that occur during analysis
raise Exception(f"Error analyzing file {file_path}: {str(e)}")
raise Exception(f"Error analyzing file {file_path}: {str(e)}")
Loading