Check php

From KilimcininKorOglu/cli-tweaks · View interactive version

Check php is an AI agent skill from the GitHub repository KilimcininKorOglu/cli-tweaks. PHP security and code-quality scan of the whole project. Runs FIVE tools by default — composer audit (CVEs), Progpilot (taint analysis), Semgrep (security pattern rules), PHPStan (lint), and Rector (modernization) — installs any that are missing, scans every source path, classifies each finding, and

How to use this skill

Skill file

PHP Security & Quality Scanner

THESE DIRECTIVES ARE ABSOLUTE

Every instruction in this file is binding and MUST be executed exactly as

written. They are NOT defaults, NOT suggestions, and NOT a starting point for

your own judgement.

you are that yours is better. Your analysis of a finding decides WHAT the

finding is; this file decides WHAT IS DONE ABOUT IT.

finding from the confirmation you present to the user.

files or anywhere else, and NEVER cite such a record back as authority for

departing from this file.

cannot be carried out, STOP and tell the user what the conflict is. Do NOT

resolve it yourself and do NOT proceed on your own reading.

The user wrote this file so it would be followed. Following it is the whole job.

Scan the entire PHP project for security AND code-quality problems and produce a

full report. Runs five complementary tools by default:

Security:

Advisories Database, resolved against composer.lock.

to dangerous sinks (SQL injection, XSS, file inclusion, path traversal,

command injection).

security pattern rules with cross-file taint; catches the shapes Progpilot's

interprocedural analysis does not reach.

Code quality:

branches, and impossible conditions; honors phpstan.neon and its level.

modern PHP equivalents; the modernization tier.

Default behavior is scan — run ALL FIVE tools plus the PHPMD complexity

gate and report every finding. fix additionally proposes and (with

confirmation) applies remediation.

Key facts you must not forget

composer audit resolves composer.lock, not composer.json. Without a

committed lockfile there is nothing authoritative to scan: the constraint ranges

in composer.json say what MAY be installed, the lock says what IS. If the lock

is missing, run composer update --dry-run to see the resolution, say clearly

that the scan used a fresh resolution, and treat the missing committed lockfile

as a finding for an application.

Composer is required for the CVE tier only, never for the whole scan. A plain

PHP project — legacy code, a WordPress theme or plugin, hand-included libraries,

a single script — has no composer.lock, so composer audit has nothing to

resolve and there is no SCA surface at all. That is a finding in its own right,

not a green light, and it is NOT a reason to refuse the project: static analysis

is exactly what such a codebase needs most. Progpilot, Semgrep, PHPStan and PHPMD

all run without Composer. State that the dependency tier did not run, and never

report "0 advisories" without saying that nothing was scanned for them.

Do NOT use Psalm --taint-analysis as the taint tier. It was the tool this

skill named before and it does not finish on a legacy PHP codebase. Measured on a

354-file, 94k-line plain-PHP project: Psalm re-analyses the file-scope

include_once chain in every call context, so one bootstrap file was analysed

1356 times in two minutes and two of 366 files completed in 35 minutes. Eight

threads were killed by the OOM killer because Psalm forks per thread and each

fork holds its own taint graph. Progpilot scanned the same tree in 90 seconds and

Semgrep in about 3 minutes. Psalm remains usable for TYPE analysis; it is not the

taint tier.

Progpilot does not model control flow that ends the request. It reports a

page that validates its input and then calls exit(), and it does not follow an

in_array() whitelist or a preg_replace() strip. Read the guard above every

reported line before you accept the finding. A finding whose guard refuses the

input is a false positive, and you must say which guard closed it.

Semgrep withholds the code snippet. extra.lines in the JSON reads

requires login, so the finding carries a rule id, a path and a line but no

evidence. Read that line from the file yourself before classifying it. Its

echoed-request and tainted-sql-string rules also ignore (int) casts,

intval(), json_encode() output and htmlspecialchars(), so the raw count is

dominated by already-guarded lines.

Filter Semgrep before you triage it. Keep the findings whose reported line

holds a superglobal ($_GET, $_POST, $_REQUEST, $_COOKIE) with no cast and

no escaping function on the same line. On the measured project that filter cut

272 findings to 12 candidates, of which 2 were live reflected XSS. Report the

full count AND the filtered count; never present the filtered set as the total.

The declared PHP version can lie about the runtime. require.php in

composer.json is the floor, but config.platform.php overrides what Composer

pretends the runtime is when resolving. A project can therefore resolve

packages for PHP 8.1 while production actually runs 8.3, or the reverse. Compare

php -v, require.php, config.platform.php, the Dockerfile base image, and

every CI workflow, and flag any drift. An end-of-life PHP branch is itself a

security finding: it stops receiving patches even when every package is current.

Baselines hide real findings. phpstan-baseline.neon suppresses pre-existing

errors so a legacy codebase can adopt the tool. A green run with a baseline is

NOT a clean codebase. Always report whether a baseline is in effect and how many

findings it is suppressing; never present a baselined green as a genuine zero,

and NEVER regenerate a baseline to make new findings disappear.

PHPStan's level decides what it even looks for. Levels run 0 (loosest) to 10

(strictest, formerly max). A green run at level 0 says almost nothing. Always

report the level that actually applied, and when it is below 5 say plainly that

the analysis is shallow.

Rector rewrites code, and its dry run is the only safe default. `rector

process edits files in place. In scan/report mode ALWAYS pass --dry-run`.

Rector's suggestions depend entirely on the rule sets configured in rector.php

and the target PHP version declared there — a suggestion list is meaningless

without naming which sets ran.

Security vs quality are separate tiers. Security findings (composer audit,

Progpilot, Semgrep) always rank above code-quality findings (PHPStan, PHPMD,

Rector). A PHPStan type nit never blocks the way a CVE does — report both, but

never let quality noise bury a real vulnerability. Rector suggestions are

optional idiom upgrades; apply them only in fix mode and only when they do not

change behavior.

Usage

/check-php          # scan whole project with all five tools, report all (default)
/check-php scan     # same as default
/check-php report   # scan + write a markdown report file
/check-php fix      # scan, then propose/apply fixes after confirmation

Step 1: Preconditions

Verify this is a PHP project and the tools are available.

  1. Confirm this is a PHP project and determine its shape. Composer is NOT

required — a plain PHP project still gets the four static-analysis tools:

```bash

test -f composer.json && head -30 composer.json || echo "NO composer.json — plain PHP mode"

find . -name '.php' -not -path './vendor/' -not -path './.git/*' | head -1 \

|| echo "NO .php files — not a PHP project"

```

STOP only when there are no .php files at all. Record the mode:

autoload.psr-4 source paths; they decide scan scope.

others still do from their PHARs (see item 5). Derive the scan scope from

the real source directories instead:

```bash

ls -d src app lib includes classes inc wp-content 2>/dev/null || echo "scope: repo root"

```

  1. Confirm a lockfile exists; findings hinge on it. Composer mode only:

```bash

test -f composer.lock && echo "composer.lock present" || echo "NO composer.lock"

```

If missing, record in the report that the scan used a fresh resolution, and

for an application treat the missing committed lockfile as a finding.

In plain PHP mode there is no lockfile by definition; record the absent SCA

surface as a finding of its own (see Key facts) rather than a clean result.

  1. Record the runtime version — findings hinge on it:

```bash

php -v && composer --version

```

Progpilot v1.3.0 requires PHP 8.3 or newer. When the host PHP is older, or

when the project runs in a container, run it inside the container that has a

matching PHP and say in the report where it ran.

  1. Inventory EVERY declared PHP version and compare them. Drift between these is

itself a finding. This scans whatever exists without depending on shell glob

expansion (unmatched globs abort the command under zsh):

```bash

grep -E '"php"' composer.json # the floor

grep -E -A3 '"platform"' composer.json 2>/dev/null # what Composer pretends

# every other declaration, across whatever config files the repo actually has:

grep -rniE 'php-version"?:|php:[0-9]|PHP_VERSION' \

--include='.yml' --include='.yaml' --include='Dockerfile*' \

--include='*.Dockerfile' . 2>/dev/null

```

Flag any source whose PHP version differs from the require.php floor —

config.platform.php, Dockerfile base images, php-version in EVERY

workflow (ci, release, etc., not just one), and container/CI configs.

Check the floor against https://www.php.net/supported-versions.php; an

end-of-life branch is a security finding on its own.

A red CI job is not always visible in the run you are looking at. When the

inventory finds drift, check the last few runs of the affected workflow

(gh run list --workflow=<name>.yml --limit 6 --json headSha,conclusion),

because a job broken by an earlier bump keeps failing on every commit after

it and is easy to read as a new failure or to miss entirely.

  1. Ensure the tools are available. Composer mode:

```bash

test -f vendor/bin/phpstan || composer require --dev phpstan/phpstan --no-interaction

test -f vendor/bin/rector || composer require --dev rector/rector --no-interaction

test -f vendor/bin/phpmd || composer require --dev phpmd/phpmd --no-interaction

# composer audit ships with Composer 2.4+; upgrade Composer if it is older

```

Progpilot and Semgrep are NOT installed as dev dependencies: Progpilot runs

from its PHAR and Semgrep from its Docker image, so neither touches

composer.json. Download the Progpilot PHAR and pull the Semgrep image with

the Tool install commands in

references/plain-php.md. Prefer the project's own vendor/bin binaries over globally

installed ones so the configured rule sets and extensions resolve. If

installing a dev dependency would modify composer.json, ask the user first —

in scan mode you may run the tools via composer exec or a throwaway

install instead. If the project's CI pins specific tool versions (check

.github/workflows/*.y*ml), install those exact versions instead so local

results match CI.

Plain PHP mode — read references/plain-php.md

in full and install the tools as its Tool install section says.

Step 2: Scan the whole project (all five tools)

Run every tool against every source path. Capture human output plus

machine-readable streams (authoritative for classification).

Write every machine-readable stream into a fresh per-run directory, NEVER a

fixed /tmp/phpstan.json / /tmp/progpilot.json. A shared path is the classic

cross-project trap: if a tool errors and does not overwrite, you silently parse

another repo's stale JSON as this project's findings. A unique dir per run makes

a write failure show up as a missing file instead of stale data.

RUNDIR="$(mktemp -d /tmp/check-php.XXXXXXXX)"; echo "run dir: $RUNDIR"

# --- Security ---
composer audit ; echo "composer audit exit: $?"
composer audit --format=json > "$RUNDIR/composer-audit.json" 2>/dev/null

# Progpilot takes the files as ARGUMENTS, not a directory. Exit 1 means findings.
find . -name '*.php' -not -path './vendor/*' -not -path './.git/*' > "$RUNDIR/phpfiles.txt"
php -d memory_limit=6G "$PHARDIR/progpilot.phar" $(cat "$RUNDIR/phpfiles.txt" | tr '\n' ' ') \
  > "$RUNDIR/progpilot.json" 2>"$RUNDIR/progpilot.err" ; echo "progpilot exit: $?"

# Semgrep: mount the repo READ-ONLY and write the JSON outside it, so the scan
# cannot dirty the working tree.
mkdir -p "$RUNDIR/semgrep"
docker run --rm -v "$PWD":/src:ro -v "$RUNDIR/semgrep":/out semgrep/semgrep \
  semgrep scan --config=p/php --config=p/owasp-top-ten --metrics=off \
  --json -o /out/semgrep.json /src ; echo "semgrep exit: $?"

# --- Code quality ---
vendor/bin/phpstan analyse --no-progress ; echo "phpstan exit: $?"
vendor/bin/phpstan analyse --no-progress --error-format=json > "$RUNDIR/phpstan.json" 2>/dev/null

vendor/bin/rector process --dry-run ; echo "rector exit: $?"    # NEVER without --dry-run in scan mode

# Cyclomatic complexity gate: every method must stay at or below 10.
# PHPMD's codesize ruleset reports CyclomaticComplexity at a threshold of 10.
vendor/bin/phpmd <src paths> text codesize ; echo "phpmd exit: $?"
vendor/bin/phpmd <src paths> json codesize > "$RUNDIR/phpmd.json" 2>/dev/null

Also audit what actually ships, not just the whole tree:

composer audit --no-dev          # production-only advisories; label these separately

Plain PHP mode runs the scan without Composer, as the Scan section of

references/plain-php.md says.

If step 4 showed the runtime differs from the declared floor, ALSO run the

analysis pinned to the floor — that is what CI/production actually execute:

FLOOR=$(grep -E '"php"' composer.json | head -1 | grep -oE '[0-9]+\.[0-9]+' | head -1)
vendor/bin/phpstan analyse --no-progress --php-version "${FLOOR/./0}"   # e.g. 8.1 -> 801

Report the floor-pinned result as the real one; a green scan on a newer local

runtime does NOT clear what production runs.

Before classifying, confirm the report belongs to THIS project. The scan scope is

the autoload.psr-4 source paths; every finding's file path MUST fall under the

current repo root, and none may sit inside vendor/. If a path points outside it

(a sibling project, a stale file), discard that finding and re-run the tool into

a fresh $RUNDIR — never report another project's issues as this one's.

Notes:

not just the repo root, and never scan vendor/.

Semgrep 1 = findings; PHPStan 1 = errors; Rector 2 = changes proposed

(with --dry-run). 0 = clean for all. None of these are tool errors — parse

the findings.

tier needs network access. If the fetch fails, say the pattern tier did not run

rather than reporting Progpilot's count as the whole security result.

baseline; Rector honors rector.php. Note which config and which level applied

in the report — and whether a baseline is suppressing findings:

```bash

ls -1 phpstan-baseline.neon 2>/dev/null

grep -c '' phpstan-baseline.neon 2>/dev/null # rough size of what is hidden

```

Step 3: Classify every finding

composer audit — for each advisory extract:

path with composer why <package>.

run tells you which ship. Label them separately.

Progpilot — each JSON entry carries vuln_name (sql_injection, xss,

file_inclusion, path_traversal, …), vuln_cwe, source_name/source_file/

source_line and sink_name/sink_file/sink_line. For each one:

model exit(), in_array() whitelists or preg_replace() strips, so a page

that refuses bad input still appears here.

sink; handle both shapes when you parse the JSON.

Semgrep — each result carries check_id, path, start.line and

extra.message. extra.lines is unusable (requires login), so:

evidence, not the tool's output.

((int)/intval), JSON output (json_encode with a JSON content type),

bound parameter, or UNGUARDED.

the Key facts. A finding that echoes a database column rather than a

superglobal is a stored-XSS candidate and belongs in its own group, not in the

reflected set.

PHPStan — for each error extract file:line, the message, and the

identifier when present (e.g. argument.type, return.missing). Record the

level that produced it — a level 9 finding is not comparable to a level 1

finding.

Rector — for each proposed change extract file:line and the rule that fired

(e.g. AddVoidReturnTypeRector, ReadOnlyPropertyRector). These are optional

improvements, not defects. Record which rule sets are configured in rector.php.

PHPMD codesize — for each CyclomaticComplexity violation extract file:line,

the class and method name, and the reported complexity. Every one is a method

over the limit of 10. Unlike a Rector suggestion this is NOT optional: the limit

is a project rule, and a method above it must be refactored into smaller

single-responsibility methods, never suppressed and never given a raised

threshold. The same ruleset also reports NPathComplexity and ExcessiveMethodLength;

report those alongside it.

Rank all findings by severity for action (security tier first, always):

  1. Production CVE (require) — highest; ships to production.
  2. Injection reaching a dangerous sink — Progpilot sql_injection,

command_injection, file_inclusion, path_traversal, or the Semgrep

equivalent, with no guard on the line; fix in code.

  1. XSS — Progpilot xss or an unguarded Semgrep echoed-request; fix in

code.

  1. Dev-only CVE (require-dev) — build/CI supply-chain risk.
  2. Taint false positive — sanitize properly if cheap, else record the guard

that closes it.

  1. End-of-life PHP branch or version drift — standing security exposure.
  2. PHPStan error — quality; fix in code (higher levels first).
  3. Method over the complexity limit (PHPMD CyclomaticComplexity > 10) —

quality; refactor into smaller methods. Ranked above Rector because it is a

limit, not a suggestion.

  1. Rector suggestion — lowest; optional idiom upgrade, behavior-preserving.

Step 4: Produce the report

Always print a ranked summary to the user, most severe first. Read

references/report-template.md and use its

shape exactly.

Verdict rule: Quality is 🟢 green ONLY when PHPStan, PHPMD AND Rector all report

zero AND no baseline is suppressing findings. Any Rector suggestion (or any PHPStan

error, or an active baseline) means quality is NOT clean — mark it 🟡 yellow and

list the outstanding items. Never call a tier green while it still has open

findings, however minor.

Coverage rule: A green security verdict MUST name what was not scanned. In

plain PHP mode that is the whole dependency tier: composer audit did not run,

so "0 advisories" means "nothing was scanned for advisories", and reporting it as

safety is a lie. Name the skipped tools (Rector too, when no PHAR exists; Semgrep

when the registry fetch failed) rather than letting the report imply all five ran.

Security follows the same bar. Judging a taint finding a false positive does not

close it until you have named the guard that refuses the input. Count open

findings, never "real" ones. Semgrep findings you filtered out are not closed

either: say how many were filtered and on what rule.

For report mode, also write this to VULN-REPORT.md (or append to an

existing BUG-REPORT.md if the project uses one) in English.

Step 5: Fix (only in `fix` mode, after confirmation)

Never edit files in scan/report mode. In fix mode, read

references/fix.md in full before you change any file, and

follow every step in it.

Rules

PHPStan, Rector) plus the PHPMD complexity gate every time; never modify files

unless invoked as fix.

legacy PHP codebase; Progpilot and Semgrep are the taint tier.

classify any Progpilot or Semgrep finding. Progpilot does not model exit() or

an in_array() whitelist, and Semgrep reports extra.lines as

requires login, so neither tool's output is evidence on its own.

present the filtered set as the total.

it, and put the proving response in the commit message. Report a finding you

cannot reproduce as unproven.

phpmd <src paths> text codesize. Any violation is a finding. NEVER raise the

threshold, add a @SuppressWarnings annotation, or drop the gate to make the

report green; refactor into smaller single-responsibility methods instead.

there are no .php files. Without Composer, run Progpilot, Semgrep, PHPStan

and PHPMD from their PHARs and the Docker image, skip composer audit and

Rector, and name every tool that did not run.

scanned for advisories, so "0 advisories" must never be presented as safety.

place.

repository, so a scan cannot dirty the working tree.

A capped run reports the first few findings and hides the rest, so the next

run finds work you already called done. Read the whole output; use the

machine-readable stream in $RUNDIR when the human output is long. A

config-header display, an existence probe and a single-value extraction may

still use them, because they read one known field, not a finding list.

security always ranks first. Never let PHPStan/Rector noise bury a real CVE.

lower-severity advisories, PHPStan errors, and Rector suggestions — do not

silently drop anything.

and development advisories separately; a dev CVE is a build/CI supply-chain

risk, not a production one, and must not be dropped.

a baselined green is NOT a clean codebase. NEVER regenerate a baseline to hide

new findings.

shallow result and must be labelled as such.

defined elsewhere; a single-file run reports class.notFound for every such

class and hides the findings that depend on those types.

config.platform.php override in every report; flag drift between them and

treat an end-of-life PHP branch as a security finding.

bump; raise the PHP version in EVERY source that declares it, not just

composer.json, then re-run the inventory and confirm none still names a

version below the floor.

never a fixed /tmp path, so a failed write shows up as a missing file instead

of another project's stale JSON.

vendor/ before classifying it; discard and re-scan anything that points

elsewhere.

and composer.lock to their committed state.

and treat an application's missing committed lockfile as a finding.

mode with user agreement, review every hunk, and never treat them as blocking

defects.

local results match CI.

language.

code change in fix mode.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.