PHPStan - Statikus kódelemzés
PHPStan használata WordPress projektekhez - PHP hibák és biztonsági problémák felderítése futtatás nélkül.
This content is not available in your language yet.
A PHPStan egy statikus elemző eszköz PHP-hoz, amely futtatás nélkül találja meg a hibákat a kódban. A WordPress fejlesztéshez a phpstan-wordpress extension biztosít teljes támogatást.
Telepítés
Szekció neve “Telepítés”Composer-rel (ajánlott)
Szekció neve “Composer-rel (ajánlott)”# PHPStancomposer require --dev phpstan/phpstan
# WordPress extensioncomposer require --dev szepeviktor/phpstan-wordpressGlobálisan
Szekció neve “Globálisan”composer global require phpstan/phpstanAlapvető használat
Szekció neve “Alapvető használat”Parancssorból
Szekció neve “Parancssorból”# Egyetlen fájl elemzésevendor/bin/phpstan analyse src/plugin.php
# Mappa elemzésevendor/bin/phpstan analyse src/
# Megadott szinttelvendor/bin/phpstan analyse src/ --level=5
# Konfiguráció fájllalvendor/bin/phpstan analyse -c phpstan.neonSzintek
Szekció neve “Szintek”| Szint | Mit ellenőriz |
|---|---|
| 0 | Alap hibák, ismeretlen osztályok |
| 1 | + Valószínűleg undefined változók |
| 2 | + Ismeretlen metódusok, tulajdonságok |
| 3 | + Return típusok |
| 4 | + Alap dead code |
| 5 | + Argumentum típusok (ajánlott kezdéshez) |
| 6 | + Hiányzó type hints |
| 7 | + Union típusok |
| 8 | + Nullable típusok |
| 9 | + Mixed típus szigorúan |
WordPress konfiguráció
Szekció neve “WordPress konfiguráció”phpstan.neon
Szekció neve “phpstan.neon”includes: - vendor/szepeviktor/phpstan-wordpress/extension.neon
parameters: level: 5 paths: - plugin.php - includes/ - src/
# WordPress specifikus beállítások bootstrapFiles: - vendor/php-stubs/wordpress-stubs/wordpress-stubs.php
# Ignorált hibák ignoreErrors: - '#^Function apply_filters invoked with .* parameters, 2 required\.$#'
# Kizárt mappák excludePaths: - vendor/ - node_modules/ - tests/
# WordPress globális változók scanDirectories: - /path/to/wordpress/Minimális konfiguráció
Szekció neve “Minimális konfiguráció”includes: - vendor/szepeviktor/phpstan-wordpress/extension.neon
parameters: level: 5 paths: - . excludePaths: - vendorWordPress stubs
Szekció neve “WordPress stubs”A WordPress függvények és osztályok típus definíciói:
# WordPress stubs telepítésecomposer require --dev php-stubs/wordpress-stubs
# WooCommerce stubscomposer require --dev php-stubs/woocommerce-stubs
# ACF stubscomposer require --dev php-stubs/acf-pro-stubsphpstan.neon kiegészítés
Szekció neve “phpstan.neon kiegészítés”parameters: bootstrapFiles: - vendor/php-stubs/wordpress-stubs/wordpress-stubs.php - vendor/php-stubs/woocommerce-stubs/woocommerce-stubs.phpGyakori hibák és javítások
Szekció neve “Gyakori hibák és javítások”1. Undefined variable
Szekció neve “1. Undefined variable”// Hiba: Variable $post might not be definedfunction my_function() { global $post; echo $post->ID;}
// Javítás:function my_function() { global $post; if ($post instanceof WP_Post) { echo $post->ID; }}2. Return type mismatch
Szekció neve “2. Return type mismatch”// Hiba: Function should return string but returns string|falsefunction get_data(): string { return get_option('my_option');}
// Javítás:function get_data(): string { $value = get_option('my_option', ''); return is_string($value) ? $value : '';}3. Parameter type
Szekció neve “3. Parameter type”// Hiba: Parameter #1 expects int, string given$post = get_post($_GET['id']);
// Javítás:$post_id = isset($_GET['id']) ? absint($_GET['id']) : 0;$post = get_post($post_id);4. Array access on mixed
Szekció neve “4. Array access on mixed”// Hiba: Cannot access offset on mixed$data = get_option('my_data');echo $data['key'];
// Javítás:$data = get_option('my_data', []);if (is_array($data) && isset($data['key'])) { echo $data['key'];}PHPDoc annotációk
Szekció neve “PHPDoc annotációk”Típus megadása
Szekció neve “Típus megadása”/** * @param int $post_id Post ID. * @param string $meta_key Meta key. * @return string|null Meta value or null. */function get_custom_meta(int $post_id, string $meta_key): ?string { $value = get_post_meta($post_id, $meta_key, true); return is_string($value) ? $value : null;}Array típusok
Szekció neve “Array típusok”/** * @param array<string, mixed> $args Arguments. * @return array<int, WP_Post> Posts. */function get_custom_posts(array $args): array { return get_posts($args);}@var annotáció
Szekció neve “@var annotáció”/** @var WP_Post|null $post */$post = get_post($id);
if ($post !== null) { echo $post->post_title;}@phpstan annotációk
Szekció neve “@phpstan annotációk”/** * @phpstan-param array{ * title: string, * content: string, * status?: string * } $data */function create_post(array $data): int { // ...}
/** @phpstan-ignore-next-line */$result = some_legacy_function();CI/CD integráció
Szekció neve “CI/CD integráció”GitHub Actions
Szekció neve “GitHub Actions”name: PHPStan
on: [push, pull_request]
jobs: phpstan: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4
- name: Setup PHP uses: shivammathur/setup-php@v2 with: php-version: '8.2'
- name: Install dependencies run: composer install --prefer-dist --no-progress
- name: Run PHPStan run: vendor/bin/phpstan analyse --error-format=githubBaseline használata
Szekció neve “Baseline használata”Ha sok hiba van a meglévő kódban:
# Baseline generálásavendor/bin/phpstan analyse --generate-baseline
# Használatvendor/bin/phpstan analyse --baseline=phpstan-baseline.neonA phpstan-baseline.neon tartalmazza a meglévő hibákat, így csak az új hibák jelennek meg.
VS Code integráció
Szekció neve “VS Code integráció”Extension telepítése
Szekció neve “Extension telepítése”- Telepítsd: “PHP Sniffer & Beautifier” vagy “phpstan” extension
- Konfiguráld a settings.json-ban:
{ "phpstan.enabled": true, "phpstan.configFile": "phpstan.neon", "phpstan.level": "5"}PhpStorm integráció
Szekció neve “PhpStorm integráció”- Settings → PHP → Quality Tools → PHPStan
- Add meg a phpstan executable útvonalát
- Állítsd be a configuration file-t
Strict mode
Szekció neve “Strict mode”declare(strict_types=1)
Szekció neve “declare(strict_types=1)”<?phpdeclare(strict_types=1);
// Most a PHP is ellenőrzi a típusokatfunction add(int $a, int $b): int { return $a + $b;}
add('1', '2'); // TypeError!PHPStan strict rules
Szekció neve “PHPStan strict rules”composer require --dev phpstan/phpstan-strict-rulesincludes: - vendor/phpstan/phpstan-strict-rules/rules.neonHibák ignorálása
Szekció neve “Hibák ignorálása”Egy sor ignorálása
Szekció neve “Egy sor ignorálása”/** @phpstan-ignore-next-line */$result = risky_function();
$result = risky_function(); // @phpstan-ignore-lineHiba pattern ignorálása
Szekció neve “Hiba pattern ignorálása”parameters: ignoreErrors: # Regex pattern - '#Call to an undefined method WP_Query::.*#'
# Specifikus üzenet - message: '#Parameter .* of function esc_attr expects string#' path: includes/
# Teljes fájl - message: '#.*#' path: legacy-code.phpTeljesítmény tippek
Szekció neve “Teljesítmény tippek”Cache használata
Szekció neve “Cache használata”vendor/bin/phpstan analyse --memory-limit=512MParallel futtatás
Szekció neve “Parallel futtatás”parameters: parallel: maximumNumberOfProcesses: 4Csak változott fájlok
Szekció neve “Csak változott fájlok”# Git diff alapjánvendor/bin/phpstan analyse $(git diff --name-only HEAD~1 -- '*.php')PHPStan vs PHPCS
Szekció neve “PHPStan vs PHPCS”| Szempont | PHPStan | PHPCS |
|---|---|---|
| Típus | Statikus elemzés | Coding standard |
| Mit vizsgál | Típusok, hibák | Formázás, stílus |
| WordPress | Extension | WPCS ruleset |
| Javítás | Kézi | Automatikus (phpcbf) |
Mindkettőt használd együtt!