Skip to content

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.


Terminál
# PHPStan
composer require --dev phpstan/phpstan
# WordPress extension
composer require --dev szepeviktor/phpstan-wordpress
Terminál
composer global require phpstan/phpstan

Terminál
# Egyetlen fájl elemzése
vendor/bin/phpstan analyse src/plugin.php
# Mappa elemzése
vendor/bin/phpstan analyse src/
# Megadott szinttel
vendor/bin/phpstan analyse src/ --level=5
# Konfiguráció fájllal
vendor/bin/phpstan analyse -c phpstan.neon
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

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/
includes:
- vendor/szepeviktor/phpstan-wordpress/extension.neon
parameters:
level: 5
paths:
- .
excludePaths:
- vendor

A WordPress függvények és osztályok típus definíciói:

Terminál
# WordPress stubs telepítése
composer require --dev php-stubs/wordpress-stubs
# WooCommerce stubs
composer require --dev php-stubs/woocommerce-stubs
# ACF stubs
composer require --dev php-stubs/acf-pro-stubs
parameters:
bootstrapFiles:
- vendor/php-stubs/wordpress-stubs/wordpress-stubs.php
- vendor/php-stubs/woocommerce-stubs/woocommerce-stubs.php

// Hiba: Variable $post might not be defined
function my_function() {
global $post;
echo $post->ID;
}
// Javítás:
function my_function() {
global $post;
if ($post instanceof WP_Post) {
echo $post->ID;
}
}
// Hiba: Function should return string but returns string|false
function 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 : '';
}
// 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);
// 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'];
}

/**
* @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;
}
/**
* @param array<string, mixed> $args Arguments.
* @return array<int, WP_Post> Posts.
*/
function get_custom_posts(array $args): array {
return get_posts($args);
}
/** @var WP_Post|null $post */
$post = get_post($id);
if ($post !== null) {
echo $post->post_title;
}
/**
* @phpstan-param array{
* title: string,
* content: string,
* status?: string
* } $data
*/
function create_post(array $data): int {
// ...
}
/** @phpstan-ignore-next-line */
$result = some_legacy_function();

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=github

Ha sok hiba van a meglévő kódban:

Terminál
# Baseline generálása
vendor/bin/phpstan analyse --generate-baseline
# Használat
vendor/bin/phpstan analyse --baseline=phpstan-baseline.neon

A phpstan-baseline.neon tartalmazza a meglévő hibákat, így csak az új hibák jelennek meg.


  1. Telepítsd: “PHP Sniffer & Beautifier” vagy “phpstan” extension
  2. Konfiguráld a settings.json-ban:
{
"phpstan.enabled": true,
"phpstan.configFile": "phpstan.neon",
"phpstan.level": "5"
}
  1. Settings → PHP → Quality Tools → PHPStan
  2. Add meg a phpstan executable útvonalát
  3. Állítsd be a configuration file-t

<?php
declare(strict_types=1);
// Most a PHP is ellenőrzi a típusokat
function add(int $a, int $b): int {
return $a + $b;
}
add('1', '2'); // TypeError!
Terminál
composer require --dev phpstan/phpstan-strict-rules
includes:
- vendor/phpstan/phpstan-strict-rules/rules.neon

/** @phpstan-ignore-next-line */
$result = risky_function();
$result = risky_function(); // @phpstan-ignore-line
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.php

Terminál
vendor/bin/phpstan analyse --memory-limit=512M
parameters:
parallel:
maximumNumberOfProcesses: 4
Terminál
# Git diff alapján
vendor/bin/phpstan analyse $(git diff --name-only HEAD~1 -- '*.php')

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!


We respect your privacy

We use cookies to improve your browsing experience and to analyse traffic. Analytics only starts if you allow it. Privacy policy, Cookie policy.

Cookie settings