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
5 changes: 3 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ jobs:
strategy:
matrix:
php-version:
- "7.4"
- "8.0"
- "8.1"
- "8.2"
- "8.3"
Expand All @@ -33,6 +31,9 @@ jobs:

- name: Install dependencies
run: composer install --ansi --no-interaction --no-progress
env:
# humanmade/coding-standards requires wp-coding-standards/wpcs 2.3.0, which has a security advisory
COMPOSER_NO_SECURITY_BLOCKING: 1

- name: Run Tests
run: vendor/bin/phpunit
10 changes: 7 additions & 3 deletions Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
use PhpParser;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Echo_;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\GroupUse;
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\Node\Stmt\Return_;
Expand Down Expand Up @@ -723,6 +725,8 @@ public static function afterEveryFunctionCallAnalysis( AfterEveryFunctionCallAna

/**
* @return non-empty-list<lowercase-string>
*
* @psalm-pure
*/
public static function getFunctionIds() : array {
return [
Expand Down Expand Up @@ -1062,7 +1066,6 @@ public static function getFunctionParams( FunctionParamsProviderEvent $event ) :
new FunctionLikeParameter( 'hook_name', false, Type::getNonEmptyString(), null, null, null, false ),
new FunctionLikeParameter( 'callback', false, new Union( [
new TCallable(
'callable',
$hook_params,
$return_type
),
Expand Down Expand Up @@ -1431,9 +1434,10 @@ public function enterNode( PhpParser\Node $node ) {
// "$var = apply_filters" directly after a function declaration
// "echo apply_filters"
// cannot do this for all cases, as often it will assign completely wrong stuff otherwise
if ( $node->getDocComment() && ( $node instanceof FuncCall || $node instanceof Return_ || $node instanceof Variable || $node instanceof Echo_ ) ) {
// PHP-Parser 5 attaches the phpdoc of "$var = apply_filters" to the expression statement only
if ( $node->getDocComment() && ( $node instanceof FuncCall || $node instanceof Return_ || $node instanceof Variable || $node instanceof Echo_ || $node instanceof Expression ) ) {
$this->last_doc = $node->getDocComment();
} elseif ( isset( $this->last_doc ) && ! $node instanceof FuncCall ) {
} elseif ( isset( $this->last_doc ) && ! $node instanceof FuncCall && ! $node instanceof Assign && ! $node instanceof Variable ) {
// if it's set already and this is not a FuncCall, reset it to null, since there's something else and it would be used incorrectly
$this->last_doc = null;
}
Expand Down
8 changes: 5 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@
"php-stubs/wordpress-stubs": "^6.0",
"php-stubs/wordpress-globals": "^0.2.0",
"php-stubs/wp-cli-stubs": "^2.7",
"vimeo/psalm": "^5 || ^6"
"psalm/psalm-plugin-api": "^0.4",
"vimeo/psalm": "^7|dev-master"
},
"require-dev": {
"dg/bypass-finals": "^1.5",
"humanmade/coding-standards": "^1.2",
"phpunit/phpunit": "^9.0",
"psalm/plugin-phpunit": "^0.18.4"
"phpunit/phpunit": "^10.5 || ^11.5",
"psalm/plugin-phpunit": "^0.21"
},
"extra": {
"psalm" : {
Expand Down
5 changes: 1 addition & 4 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
bootstrap="tests/bootstrap.php"
backupGlobals="false"
beStrictAboutCoversAnnotation="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTestsThatDoNotTestAnything="false"
beStrictAboutTodoAnnotatedTests="true"
colors="true"
executionOrder="default"
verbose="true"
>
<testsuites>
<testsuite name="psalm">
Expand Down
2 changes: 1 addition & 1 deletion stubs/overrides.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ function apply_filters( string $hook_name, $value, ...$args ) {}
* | PHP_URL_QUERY | 6 |
* | PHP_URL_FRAGMENT | 7 |
*
* @template TComponent of (-1|PHP_URL_*)
* @template TComponent of (-1|0|1|2|3|4|5|6|7)
* @param string $url
* @param TComponent $component
* @return (
Expand Down
2 changes: 2 additions & 0 deletions test.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
/**
* @param array{basedir: string, baseurl: string, error: false|string, path: string, subdir: string, url: string} $dir
* @return array{basedir: string, baseurl: string, error: false|string, path: string, subdir: string, url: string}
*
* @psalm-pure
*/
function filter_upload_dir( array $dir ) : array {
return $dir;
Expand Down
6 changes: 6 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

require __DIR__ . '/../vendor/autoload.php';

// Psalm's test classes extend classes that are final outside of its test suite
DG\BypassFinals::enable();
7 changes: 7 additions & 0 deletions tests/unit/BaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ public function analyzeFile(
}

class TestConfig extends Psalm\Tests\TestConfig {
public function __construct() {
parent::__construct();

// Psalm 7 reports the docblock issues of the stubs plugins add, and the third-party WordPress stubs have some
$this->setAdvancedErrorLevel( 'InvalidDocblock', [ [ 'type' => 'suppress', 'directory' => [ [ 'name' => 'vendor' ] ] ] ], 'error' );
}

protected function getContents() : string {
return '<?xml version="1.0"?>
<projectFiles>
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/FiltersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class FiltersTest extends BaseTestCase {
/**
* @return iterable<string,array{string,assertions?:array<string,string>,error_levels?:string[]}>
*/
public function providerValidCodeParse() : iterable {
public static function providerValidCodeParse() : iterable {
return [
'add_filter with docblock' => [
<<<'EOD'
Expand Down Expand Up @@ -67,7 +67,7 @@ public function providerValidCodeParse() : iterable {
/**
* @return iterable<string,array{string,error_message:string,2?:string[],3?:bool,4?:string}>
*/
public function providerInvalidCodeParse() : iterable {
public static function providerInvalidCodeParse() : iterable {
return [
'add_filter fails wrong types' => [
<<<'EOD'
Expand Down
Loading