From 133b3b248154b18edcbf1555663ecc38d28a49c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Kr=C3=A4mer?= Date: Sun, 24 May 2026 14:40:47 +0200 Subject: [PATCH 1/3] Add GitHub Action workflow and sample PHP changes for CI test. --- .github/workflows/cognitive-code-analysis.yml | 29 +++++++++++++++++++ src/Command/InitCommand.php | 2 +- .../Presentation/RuntimeStatusRenderer.php | 3 ++ src/Config/ConfigFileResolver.php | 3 ++ 4 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/cognitive-code-analysis.yml diff --git a/.github/workflows/cognitive-code-analysis.yml b/.github/workflows/cognitive-code-analysis.yml new file mode 100644 index 0000000..9359dfd --- /dev/null +++ b/.github/workflows/cognitive-code-analysis.yml @@ -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 diff --git a/src/Command/InitCommand.php b/src/Command/InitCommand.php index 53c1cb0..025b38c 100644 --- a/src/Command/InitCommand.php +++ b/src/Command/InitCommand.php @@ -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 { diff --git a/src/Command/Presentation/RuntimeStatusRenderer.php b/src/Command/Presentation/RuntimeStatusRenderer.php index c8008cb..530441b 100644 --- a/src/Command/Presentation/RuntimeStatusRenderer.php +++ b/src/Command/Presentation/RuntimeStatusRenderer.php @@ -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 diff --git a/src/Config/ConfigFileResolver.php b/src/Config/ConfigFileResolver.php index 11ceb31..14612fd 100644 --- a/src/Config/ConfigFileResolver.php +++ b/src/Config/ConfigFileResolver.php @@ -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'; From 9ed1527b94aa5361beb92923d371cabd79596ce6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Kr=C3=A4mer?= Date: Sun, 24 May 2026 14:41:52 +0200 Subject: [PATCH 2/3] Add intentionally complex method for cognitive analysis CI test. --- tests/TestCode/FileWithTwoClasses.php | 98 ++++++++++++++++++++++++++- 1 file changed, 97 insertions(+), 1 deletion(-) diff --git a/tests/TestCode/FileWithTwoClasses.php b/tests/TestCode/FileWithTwoClasses.php index e231923..22409e6 100644 --- a/tests/TestCode/FileWithTwoClasses.php +++ b/tests/TestCode/FileWithTwoClasses.php @@ -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 From 141a9f5c3c675ad72b13103260777413dc34df9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Kr=C3=A4mer?= Date: Sun, 24 May 2026 14:43:47 +0200 Subject: [PATCH 3/3] Fix CI docs to pass comma-separated paths to phpcca analyse. --- docs/CI-Integration.md | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/docs/CI-Integration.md b/docs/CI-Integration.md index 49bb73f..b4040f3 100644 --- a/docs/CI-Integration.md +++ b/docs/CI-Integration.md @@ -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 @@ -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). @@ -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 @@ -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.