--- name: php-dev-core description: | This skill should be used when the user is writing generic PHP, creating a Composer package, working with PSR-4/PSR-12, running PHPStan/PHPCS, handling PHP security, or building framework-agnostic PHP logic. Foundation skill for PHP development. DEPENDENT on govard-toolbox for environment commands. compatibility: claude, codex, opencode, copilot, dsh depends: [govard-toolbox] metadata: audience: developers workflow: php --- # PHP Developer Core Foundation skill for framework-agnostic PHP. Covers coding standards, Composer, static analysis, security, and testing across Magento 2, Laravel, Symfony, and WordPress. Pair with `govard-toolbox` for container and environment commands (`govard up`, `govard sh`, `govard audit`). For framework-specific tooling see `govard-laravel`, `govard-symfony`, `govard-wordpress`, or `magento2-dev-core` for Magento DI and service contracts. ## Related Skills **REQUIRED BACKGROUND:** Load `govard-toolbox` first for environment lifecycle and audit matrix. This skill adds language-level patterns. Framework skills (`govard-laravel`, `govard-symfony`, `govard-wordpress`) depend on both `govard-toolbox` and `php-dev-core`; `magento2-dev-core` cross-references this skill for generic PHP and owns Magento-specific DI and plugins. ## 1. Coding Standards & Types Every new PHP file MUST start with strict types: ```php Status::Active, 'inactive' => Status::Inactive, default => throw new \InvalidArgumentException("Unknown status: $raw"), }; } ``` Run `composer dump-autoload --optimize` after changing autoload maps and verify with `composer validate --strict`. ## 2. Composer Composer is the single source for dependencies, autoloading, and scripts. ```bash composer validate --strict # validate before commit composer audit # known vulnerabilities composer install --no-dev --optimize-autoloader # CI/production composer outdated # check outdated composer update vendor/package --with-dependencies # update with care ``` Version constraints: prefer `^` (caret) for semver (`^8.1` allows `8.x` but not `9.0`). Never commit `vendor/`; always commit `composer.lock` for applications, omit for libraries. `composer.json` essentials: ```json { "name": "vendor/package", "type": "library", "require": { "php": "^8.1" }, "autoload": { "psr-4": { "Vendor\\Package\\": "src/" } }, "scripts": { "lint": "phpcs --standard=PSR12 src tests", "analyse": "phpstan analyse -c phpstan.neon", "test": "phpunit --testdox" } } ``` Private package auth belongs in `auth.json` (gitignored) or env vars, never in `composer.json`. ## 3. Static Analysis Two gates: PHPCS for style, PHPStan for types. Both must pass before merge. ### PHPCS (PSR-12) ```bash vendor/bin/phpcs --standard=PSR12 src tests govard audit run --checks lint --lint-provider govard --mode project --format json govard audit run --checks lint --lint-provider govard --mode project # text, capped ``` Fix auto-fixable violations with `phpcbf --standard=PSR12 src`. ### PHPStan (Level 6+) Level 6 is the minimum; raise to 8 when possible. ```bash vendor/bin/phpstan analyse -c phpstan.neon govard audit run --checks lint --lint-provider govard --mode project --format json ``` Sample `phpstan.neon` (full config in `references/coding-standards.md`): ```neon parameters: level: 6 paths: [src] checkMissingIterableValueType: false ``` Govard audit is the source of truth for CI — it runs PHPCS PSR12 plus PHPStan and the pub/media guard. The `lint` check is the only audit check for generic PHP; other values are rejected. Use `--mode project` for full scans, `--mode standalone` for isolated packages, `--scope diff --base origin/master` for PR quick scans (see `govard-toolbox` ## Audit). Fallback without Govard is `phpcs --standard=PSR12` plus standalone `phpstan`. Treat every PHPStan error as a defect — add types instead of baseline ignores. ## 4. Security Baseline Apply these rules to every PHP change regardless of framework. ### Input — Never Trust Superglobals **NEVER** read `$_GET`, `$_POST`, `$_REQUEST`, `$_SESSION`, `$_COOKIE` directly. Use `filter_input` or framework request objects. ```php // WRONG $id = $_GET['id']; // CORRECT — filter_input with explicit filter $id = filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT); $name = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_FULL_SPECIAL_CHARS); // Fallback when filter_input not available $id = filter_var($_GET['id'] ?? null, FILTER_VALIDATE_INT); if ($id === false || $id === null) { throw new \InvalidArgumentException('Invalid id'); } ``` ### Output — Escape on Render Escape at the boundary where data becomes HTML, JS, URL, or CSS: ```php echo htmlspecialchars($userInput, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); echo '