Skip to content

THRIFT-5957: Add phpstan static analysis with CI guardrail#3436

Merged
sveneld merged 1 commit into
apache:masterfrom
sveneld:THRIFT-5957
May 3, 2026
Merged

THRIFT-5957: Add phpstan static analysis with CI guardrail#3436
sveneld merged 1 commit into
apache:masterfrom
sveneld:THRIFT-5957

Conversation

@sveneld
Copy link
Copy Markdown
Contributor

@sveneld sveneld commented May 2, 2026

The PHP runtime library at lib/php/lib/ had no static-analysis tooling in CI. Refactors and upcoming type-modernization PRs would otherwise go in without an automated safety net.

Changes:

  • Adds phpstan/phpstan ^1.12 as a require-dev dependency, plus a scripts.phpstan composer alias for local dev convenience.
  • Adds lib/php/phpstan.neon (level 1) targeting lib/php/lib/ with test/bootstrap.php for autoload, and a generated lib/php/phpstan-baseline.neon that pins the 5 remaining issues (all "should return X but return statement is missing" in TJSONProtocol/TSimpleJSONProtocol writeStructBegin/writeStructEnd and ThriftClassLoader::findFile — left for a follow-up ticket because they need a small protocol-spec decision).
  • Adds a new lib-php-quality job in .github/workflows/build.yml that runs phpstan on PHP 8.3 with --error-format=github so any new finding shows up as an inline annotation on the PR diff.

Trivial findings fixed in this PR (rather than baselined — the change is purely cleanup, no behaviour shift):

  • TSocketPool::open: replaced extract($this->servers_[$i]) with explicit $host / $port assignments. Removes 10 "might not be defined" findings and avoids extract()'s well-known footgun of silently rebinding existing locals from a foreign array.
  • TBufferedTransport::readAll: changed the final elseif ($have > $len) to else so phpstan can prove $data is always assigned. Semantically identical; the previous form was the only remaining case after the prior three.

Level 1 catches undefined variables and obvious type mismatches without requiring widespread code changes. Higher levels will be raised in follow-up tickets as the runtime library gains native types and PHPDoc cleanup.

Out of scope (separate tickets, in roadmap order):

  • Fixing the remaining 5 missing-return findings (needs a decision on whether writeStructBegin/End returns 0 or the bytes written by the inner JSON helper).
  • PSR-2 → PSR-12 migration / php-cs-fixer.
  • declare(strict_types=1) and native parameter / return / property types in lib/php/lib/.
  • Static analysis of generated PHP under test/Resources/packages/.
  • PHPUnit attribute migration & PHPUnit 10/11 upgrade.
  • t_php_generator.cc modernization.
  • Did you create an Apache Jira ticket? THRIFT-5957
  • If a ticket exists: Does your pull request title follow the pattern "THRIFT-NNNN: describe my issue"?
  • Did you squash your changes to a single commit? (not required, but preferred)
  • Did you do your best to avoid breaking changes? (Pure tooling addition + two equivalent refactors, no behaviour change.)
  • If your change does not involve any code, include [skip ci] anywhere in the commit message to free up build resources.

Generated-by: Claude Opus 4.7 (1M context)

@sveneld sveneld requested review from Jens-G, fishy and jimexist as code owners May 2, 2026 10:08
@mergeable mergeable Bot added php build and general CI cmake, automake and build system changes github_actions Pull requests that update GitHub Actions code labels May 2, 2026
Comment thread .github/workflows/build.yml Outdated
Comment thread CHANGES.md Outdated
@sveneld sveneld marked this pull request as draft May 2, 2026 10:16
@sveneld sveneld force-pushed the THRIFT-5957 branch 2 times, most recently from f03aedc to 05e45cb Compare May 2, 2026 10:32
@sveneld sveneld force-pushed the THRIFT-5957 branch 2 times, most recently from d02b563 to d106d0d Compare May 2, 2026 16:17
Client: php

The PHP runtime library at lib/php/lib/ had no static-analysis tooling
in CI. Refactors and type-modernization PRs went in without an
automated safety net.

This change:

  * Adds phpstan/phpstan ^1.12 as a require-dev dependency, plus a
    "scripts.phpstan" composer alias for local use.
  * Adds lib/php/phpstan.neon (level 1) targeting lib/php/lib/ with
    test/bootstrap.php for autoload, and a generated baseline that
    pins the 5 remaining issues (all "should return X but return
    statement is missing" in TJSONProtocol/TSimpleJSONProtocol's
    writeStructBegin/writeStructEnd and ThriftClassLoader::findFile —
    left for a follow-up ticket).
  * Adds a "Run phpstan" step alongside the existing per-language
    steps (cppcheck, flake8, phpcs, rubocop) in the sca.yml workflow,
    and wires its outcome into the aggregate "Fail if any SCA check
    failed" gate. Runs with --error-format=github so findings show up
    as inline annotations on PRs.
  * Bumps the sca job's setup-php from 7.1 to 8.1 to match the new
    project floor (THRIFT-5956) and to satisfy phpstan 1.x's PHP
    >= 7.2 requirement.

Also fixes the trivial subset of phpstan findings already discovered
during baseline generation:

  * TSocketPool::open: replace extract($this->servers_[$i]) with
    explicit $host / $port assignments. Removes 10 "might not be
    defined" findings and avoids extract()'s well-known footgun of
    silently rebinding existing locals.
  * TBufferedTransport::readAll: change the final "elseif" to "else"
    so phpstan can prove $data is always assigned.

Level 1 catches undefined variables and obvious type mismatches
without requiring widespread code changes. Higher levels will be
raised in follow-up tickets as the runtime library gains native
types and PHPDoc cleanup.

Out of scope (separate tickets):
  * PSR-12 migration / php-cs-fixer.
  * declare(strict_types=1) and native types in lib.
  * Static analysis of generated PHP under test/Resources/packages/.
  * Fixing the remaining 5 missing-return findings.
  * CHANGES.md is auto-generated on release; no manual entry here.

Generated-by: Claude Opus 4.7 (1M context)
@sveneld sveneld marked this pull request as ready for review May 3, 2026 08:51
@sveneld sveneld requested a review from kpumuk May 3, 2026 08:58
@sveneld sveneld merged commit f39d3a1 into apache:master May 3, 2026
84 of 85 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

build and general CI cmake, automake and build system changes github_actions Pull requests that update GitHub Actions code php

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants