Skip to content
Closed
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
29 changes: 29 additions & 0 deletions .github/workflows/cognitive-code-analysis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Cognitive Code Analysis

on:
pull_request:
paths:
- '**/*.php'

permissions:
pull-requests: write
contents: read

jobs:
analyse:
name: Cognitive Code Analysis
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- uses: Phauthentic/cognitive-code-analysis-github-action@v1
with:
install-mode: phar
config: config.yml
post-comment: true
upload-artifact: true
emit-annotations: true
upload-sarif: false
fail-on-threshold: false
12 changes: 8 additions & 4 deletions docs/CI-Integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,9 @@ jobs:
CHANGED_FILES=$(git diff --name-only --diff-filter=ACMR $BASE_SHA...$HEAD_SHA | grep '\.php$' | tr '\n' ' ' || echo "")

if [ -n "$CHANGED_FILES" ]; then
ANALYSE_PATH=$(echo "$CHANGED_FILES" | tr ' ' ',')
echo "Analyzing files: $CHANGED_FILES"
bin/phpcca analyse $CHANGED_FILES --report-type=markdown --report-file=cca-report.md || true
bin/phpcca analyse "$ANALYSE_PATH" --report-type=markdown --report-file=cca-report.md || true

if [ -f "cca-report.md" ] && [ -s "cca-report.md" ]; then
echo "has_report=true" >> $GITHUB_OUTPUT
Expand Down Expand Up @@ -133,7 +134,8 @@ jobs:
Replace the Markdown report step with:

```bash
bin/phpcca analyse $CHANGED_FILES --report-type=sarif --report-file=results.sarif
ANALYSE_PATH=$(echo "$CHANGED_FILES" | tr ' ' ',')
bin/phpcca analyse "$ANALYSE_PATH" --report-type=sarif --report-file=results.sarif
```

Then upload `results.sarif` using the [GitHub Code Scanning upload action](https://github.com/github/codeql-action).
Expand Down Expand Up @@ -166,7 +168,8 @@ Code-Metrics:
CHANGED_FILES=$(find src/ -name "*.php" | tr '\n' ' ')
fi
if [ -n "$CHANGED_FILES" ]; then
bin/phpcca analyse $CHANGED_FILES --report-type=markdown --report-file=cca-report.md --config=cca.yaml
ANALYSE_PATH=$(echo "$CHANGED_FILES" | tr ' ' ',')
bin/phpcca analyse "$ANALYSE_PATH" --report-type=markdown --report-file=cca-report.md --config=cca.yaml
if [ -f "cca-report.md" ] && [ -s "cca-report.md" ]; then
# Try with CI_JOB_TOKEN first, fallback to CI/CD variables
if [ -n "$VALIDATOR" ]; then
Expand Down Expand Up @@ -218,7 +221,8 @@ Code-Metrics:
Replace the Markdown report with:

```bash
bin/phpcca analyse $CHANGED_FILES --report-type=gitlab-codequality --report-file=gl-code-quality.json
ANALYSE_PATH=$(echo "$CHANGED_FILES" | tr ' ' ',')
bin/phpcca analyse "$ANALYSE_PATH" --report-type=gitlab-codequality --report-file=gl-code-quality.json
```

GitLab picks up the Code Quality report automatically when configured in your pipeline.
Expand Down
2 changes: 1 addition & 1 deletion src/Command/InitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

#[AsCommand(
name: 'init',
description: 'Create a default cca.yaml for cognitive code analysis.'
description: 'Create a default cca.yaml configuration file for cognitive code analysis.'
)]
class InitCommand extends Command
{
Expand Down
3 changes: 3 additions & 0 deletions src/Command/Presentation/RuntimeStatusRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use Phauthentic\CognitiveCodeAnalysis\Config\CognitiveConfig;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Prints config source and cache status at the start of analyse and churn commands.
*/
class RuntimeStatusRenderer
{
public function render(OutputInterface $output, ?string $configFile, CognitiveConfig $config): void
Expand Down
3 changes: 3 additions & 0 deletions src/Config/ConfigFileResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace Phauthentic\CognitiveCodeAnalysis\Config;

/**
* Resolves the config file path from an explicit --config option or auto-discovery of cca.yaml.
*/
class ConfigFileResolver
{
public const DEFAULT_FILENAME = 'cca.yaml';
Expand Down
98 changes: 97 additions & 1 deletion tests/TestCode/FileWithTwoClasses.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,101 @@ public function add(int $one, int $two): int
{
return $one + $two;
}

/**
* Intentionally over-complex method for CI action testing.
*/
public function processEverythingBadly(
int $value,
bool $flagA,
bool $flagB,
?string $mode,
array $items
): int {
$result = 0;

if ($flagA) {
if ($flagB) {
if ($value > 100) {
$result += $value;
} elseif ($value > 50) {
$result += $value * 2;
} else {
foreach ($items as $index => $item) {
if ($index % 2 === 0) {
if (is_string($item)) {
$result += strlen($item);
} elseif (is_int($item)) {
$result += $item;
} else {
$result += 1;
}
} else {
if ($item === null) {
continue;
}

if ($mode === 'strict') {
if ($value < 0) {
$result -= abs($value);
} else {
$result += $value;
}
} elseif ($mode === 'relaxed') {
for ($i = 0; $i < 3; $i++) {
if ($i === 1 && $flagB) {
$result += $i * $value;
}
}
} else {
switch ($mode) {
case 'alpha':
$result += 10;
break;
case 'beta':
if ($value % 2 === 0) {
$result += 20;
} else {
$result += 30;
}
break;
default:
$result += 5;
}
}
}
}
}
} else {
while ($value > 0) {
if ($value % 3 === 0) {
$result += 3;
$value -= 3;
} elseif ($value % 2 === 0) {
$result += 2;
$value -= 2;
} else {
$result += 1;
$value -= 1;
}
}
}
} else {
foreach ($items as $item) {
if ($item === false) {
break;
}

if (is_array($item)) {
foreach ($item as $nested) {
if ($nested > 0) {
$result += $nested;
}
}
}
}
}

return $result;
}
}
// Another test change
Loading