$file = Get-ChildItem -Recurse -Filter "Program.cs" | Select-Object -First 1 $content = Get-Content $file.FullName $reportData = New-Object System.Collections.Generic.List[PSObject] $inFunction = $false $currentFunc = $null $braceCount = 0 for ($i = 0; $i -lt $content.Count; $i++) { $line = $content[$i].Trim() if ([string]::IsNullOrEmpty($line)) { if ($inFunction) { $currentFunc.LOC++ } continue } if (!$inFunction -and $line -match '(?\w+)\s*\((?[^\)]*)\)\s*(?:\{|=>|$)') { if ($Matches['name'] -match '^(if|while|for|foreach|switch|catch)$') { continue } $funcName = $Matches['name'] $paramStr = $Matches['params'].Trim() $paramCount = if ($paramStr -eq "") { 0 } else { ($paramStr -split ',').Count } $currentFunc = [PSCustomObject]@{ "Name" = $funcName "Start" = $i + 1 "Params" = $paramCount "Body" = $line + "`n" "LOC" = 1 } if ($line -match '=>') { $cleanBody = [regex]::Replace($currentFunc.Body, '//.*|(?s)/\*.*?\*/', '') $ccMatches = [regex]::Matches($cleanBody, '\b(if|else\s+if|for|foreach|while|case)\b|&&|\|\||\?') $currentFunc | Add-Member -MemberType NoteProperty -Name "Complexity" -Value ($ccMatches.Count + 1) $currentFunc | Add-Member -MemberType NoteProperty -Name "End" -Value ($i + 1) $reportData.Add($currentFunc) $currentFunc = $null continue } $inFunction = $true $braceCount = ($line.ToCharArray() | Where-Object {$_ -eq '{'}).Count - ($line.ToCharArray() | Where-Object {$_ -eq '}'}).Count continue } if ($inFunction -and $currentFunc -ne $null) { $currentFunc.Body += $content[$i] + "`n" $currentFunc.LOC++ $braceCount += ($content[$i].ToCharArray() | Where-Object {$_ -eq '{'}).Count - ($content[$i].ToCharArray() | Where-Object {$_ -eq '}'}).Count if ($braceCount -eq 0 -and $currentFunc.Body -match '\{') { $cleanBody = [regex]::Replace($currentFunc.Body, '//.*|(?s)/\*.*?\*/', '') $ccMatches = [regex]::Matches($cleanBody, '\b(if|else\s+if|for|foreach|while|case)\b|&&|\|\||\?') $currentFunc | Add-Member -MemberType NoteProperty -Name "Complexity" -Value ($ccMatches.Count + 1) $currentFunc | Add-Member -MemberType NoteProperty -Name "End" -Value ($i + 1) $reportData.Add($currentFunc) $inFunction = $false $currentFunc = $null } } } $rows = foreach ($row in $reportData) { $cc = [int]$row.Complexity $display = if ($cc -gt 11) { "$cc ⚠" } else { "$cc" } $style = if ($cc -gt 11) { "style='background-color:#ffcccc; color:red; font-weight:bold;'" } else { "" } " Program::$($row.Name) $($row.Start) $($row.End) $display $($row.LOC) $($row.Params) " } $html = @"

Code Complexity Analysis Report

$($rows -join "")
Function NameStart LineEnd LineComplexityLOCParams
"@ $html | Out-File "ComplexityReport.html" -Encoding utf8 Invoke-Item "ComplexityReport.html" # [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 # $threshold = 11 # Get-ChildItem -Recurse -Filter *.cs | ForEach-Object { # $content = Get-Content $_.FullName -Raw # # Use regex matches to find ALL occurrences in the entire file content # $pattern = '\b(if|else if|for|foreach|while|case)\b|&&|\|\|' # $matches = [regex]::Matches($content, $pattern) # # Total count of all operators found + 1 base path # $cc = $matches.Count + 1 # $status = if ($cc -gt $threshold) { "WARNING: CC is $cc" } else { "CC: $cc" } # Write-Host "File: $($_.Name) | $status" # }