Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,34 +1,45 @@
# Polylang PHPStan

This package provides a [PHPStan](https://phpstan.org/) extension for [Polylang](https://wordpress.org/plugins/polylang/) and [Polylang Pro](https://polylang.pro).
It's should be used in combination with [Polylang Stubs](https://github.com/polylang/polylang-stubs/).
It should be used in combination with [Polylang Stubs](https://github.com/polylang/polylang-stubs/).

### Requirements
## Requirements

- PHP >=7.1
- PHP 8+

### Installation
## Installation

Require this package as a development dependency with Composer.

> [!TIP]
> `polylang/polylang-stubs` is optional but strongly recommended.

```bash
composer require --dev wpsyntex/polylang-phpstan
composer require --dev wpsyntex/polylang-stubs
```

### Configuration
## Configuration

### Adding the extension

Include the extension and stubs in the PHPStan configuration file.

> [!IMPORTANT]
> Prior to version 2.1 (included), `polylang/polylang-stubs` is automatically loaded.
> Starting from version 2.2, it must be configured manually.

```yaml
includes:
- vendor/wpsyntex/polylang-phpstan/extension.neon
parameters:
bootstrapFiles:
stubFiles:
- vendor/wpsyntex/polylang-stubs/polylang-stubs.php
```

Opt-in for WordPress stubs override.
### Opt in to WordPress stubs overrides

The `stubs/wordpress-override.php` file provides corrected type definitions for specific WordPress functions that have imprecise or incorrect type hints in the standard WordPress stubs (currently `sanitize_key()`, `maybe_serialize()`, and `sanitize_text_field()`).

```yaml
stubFiles:
Expand Down
2 changes: 1 addition & 1 deletion bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
define( 'POLYLANG', 'Polylang' );
define( 'POLYLANG_BASENAME', 'polylang/polylang.php' );
define( 'POLYLANG_DIR', './' );
define( 'POLYLANG_VERSION', '3.0' ); // Must also be defined as dynamic in config file.
define( 'POLYLANG_VERSION', '3.8' ); // Must also be defined as dynamic in config file.
define( 'PLL_COOKIE', 'pll_language' );
define( 'PLL_LOCAL_DIR', './wp-content/polylang' );
define( 'PLL_ADMIN', false ); // Must also be defined as dynamic in config file.
Expand Down
4 changes: 2 additions & 2 deletions extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ includes:
- ../../szepeviktor/phpstan-wordpress/extension.neon
parameters:
bootstrapFiles:
- %rootDir%/../../php-stubs/wordpress-stubs/wordpress-stubs.php
- %rootDir%/../../wpsyntex/polylang-stubs/polylang-stubs.php
- bootstrap.php
stubFiles:
- %rootDir%/../../php-stubs/wordpress-stubs/wordpress-stubs.php
dynamicConstantNames:
- POLYLANG_VERSION
- PLL_ADMIN
Expand Down
5 changes: 4 additions & 1 deletion tests/DynamicReturnTypeExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public function testFileAsserts(string $assertType, string $file, ...$args): voi
public static function getAdditionalConfigFiles(): array
{
// phpstan.neon or extension.neon use relative paths, so we need to use a fake vendor directory regarding szepeviktor/phpstan-wordpress dependency.
return [dirname(__DIR__) . '/vendor/wpsyntex/polylang-phpstan/extension.neon'];
return [
dirname(__DIR__) . '/vendor/wpsyntex/polylang-phpstan/extension.neon',
dirname(__DIR__) . '/vendor/wpsyntex/polylang-phpstan/test-extension.neon',
];
}
}
1 change: 1 addition & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

// Copy package to a fake vendor directory.
copy(dirname(__DIR__) . '/extension.neon', $helperDirectory . '/extension.neon');
copy(dirname(__DIR__) . '/tests/test-extension.neon', $helperDirectory . '/test-extension.neon');
copy(dirname(__DIR__) . '/bootstrap.php', $helperDirectory . '/bootstrap.php');

require_once dirname(__DIR__) . '/vendor/autoload.php';
3 changes: 3 additions & 0 deletions tests/test-extension.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
parameters:
bootstrapFiles:
- %rootDir%/../../wpsyntex/polylang-stubs/polylang-stubs.php
Loading