Skip to content

fix: suppress always-true PHPStan narrowing on Composer tryComposer guard#84

Merged
CybotTM merged 1 commit into
mainfrom
fix/phpstan-trycomposer-narrowed
Jun 7, 2026
Merged

fix: suppress always-true PHPStan narrowing on Composer tryComposer guard#84
CybotTM merged 1 commit into
mainfrom
fix/phpstan-trycomposer-narrowed

Conversation

@CybotTM

@CybotTM CybotTM commented Jun 7, 2026

Copy link
Copy Markdown
Member

Problem

Code Quality (PHPStan) is red on main under current dependency resolution — confirmed by re-running the last-green run (26959408575). This is independent of any workflow change; it's no-committed-lockfile drift.

CommandContextTrait::resolveContext() guards tryComposer() (Composer ≥2.3) behind method_exists($this, 'tryComposer') so the deprecated getComposer() path still works on Composer 2.2 LTS — which composer.json explicitly supports ("composer/composer": "2.2.*|^2.9", and the CI matrix tests 2.2). PHPStan analyses against the highest resolved Composer (2.10.1), where tryComposer() always exists, so it reports function.alreadyNarrowedTypeonce per command class that uses the trait (5 errors). The inline @phpstan-ignore-next-line does not match all per-class trait analyses, so it stopped suppressing them.

Fix

  • Move the suppression to a path-scoped ignoreErrors entry in phpstan.neon (identifier function.alreadyNarrowedType, reportUnmatched: false) — robust across the trait's per-class analyses.
  • Drop the now-ineffective inline ignore.
  • The runtime guard is unchanged and still required for the 2.2.* support path. This is a false-positive suppression, not a weakening — PHPStan can't see the 2.2 resolution where the method genuinely may be absent.

Verification

Local, fresh highest resolution (composer/composer 2.10.1, PHPStan 2.2.2, PHP 8.5): [OK] No errors (was 5 errors before).

Context

Surfaced while migrating CI to the org reusable php-ci.yml (#83) — that PR's Checks job runs PHPStan and inherited this pre-existing failure. This fix is split out so it stands on its own and unblocks main.

…uard

CommandContextTrait guards `tryComposer()` (Composer >=2.3) behind
`method_exists($this, 'tryComposer')` so the deprecated getComposer()
path still works on Composer 2.2 LTS (composer.json: "2.2.*|^2.9").
PHPStan analyses against the highest resolved Composer, where the method
always exists, and reports function.alreadyNarrowedType once per command
class using the trait. The inline @phpstan-ignore-next-line did not match
all per-class trait analyses, so the check became red once the highest
Composer (>=2.3) was resolved (no committed lock file).

Move the suppression to a path-scoped ignoreErrors entry in phpstan.neon
(reportUnmatched: false) and drop the ineffective inline ignore. The
runtime guard is unchanged and still required for the 2.2.* support path.

Fixes the Code Quality job, which is red on main under current resolution.

Signed-off-by: Sebastian Mendel <info@sebastianmendel.de>
Copilot AI review requested due to automatic review settings June 7, 2026 08:45

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request moves the PHPStan suppression for the function.alreadyNarrowedType error from an inline comment in src/Commands/CommandContextTrait.php to a path-scoped ignore in phpstan.neon. This ensures that the runtime guard checking for the existence of tryComposer (which is required for Composer 2.2 LTS compatibility but flagged as always-true when analyzed against Composer >=2.3) is correctly suppressed across all command classes that consume the trait. There are no review comments, so I have no feedback to provide.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

@sonarqubecloud

sonarqubecloud Bot commented Jun 7, 2026

Copy link
Copy Markdown

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a PHPStan false-positive on main caused by PHPStan analyzing against the highest resolved Composer version (where tryComposer() always exists), while the code intentionally retains a runtime guard for Composer 2.2 LTS compatibility.

Changes:

  • Replaces an ineffective inline @phpstan-ignore-next-line with a path-scoped ignoreErrors rule in phpstan.neon for function.alreadyNarrowedType.
  • Updates inline comments in CommandContextTrait::resolveContext() to document where/why the suppression is configured.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/Commands/CommandContextTrait.php Removes the inline PHPStan suppression and points maintainers to the centralized suppression in config.
phpstan.neon Adds a path-scoped ignoreErrors entry (with rationale) to suppress the Composer-version-dependent narrowing false positive.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@CybotTM CybotTM merged commit 2a37721 into main Jun 7, 2026
25 checks passed
@CybotTM CybotTM deleted the fix/phpstan-trycomposer-narrowed branch June 7, 2026 08:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants