Skip to content

PHPStan enhancements #262

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
16 changes: 15 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@
"php-parallel-lint/php-console-highlighter": "^1.0",
"php-parallel-lint/php-parallel-lint": "^1.3.1",
"phpcompatibility/php-compatibility": "dev-develop",
"phpstan/extension-installer": "^1.4.3",
"phpstan/extension-installer": "^1.4",
"phpstan/phpstan": "^1.12.26",
"phpstan/phpstan-deprecation-rules": "^1.2",
"phpstan/phpstan-phpunit": "^1.4",
"phpstan/phpstan-strict-rules": "^1.6",
"swissspidy/phpstan-no-private": "^0.2.1",
"szepeviktor/phpstan-wordpress": "^v1.3.5",
"wp-cli/config-command": "^1 || ^2",
"wp-cli/core-command": "^1 || ^2",
Expand All @@ -41,6 +45,11 @@
"branch-alias": {
"dev-main": "4.0.x-dev"
},
"phpstan": {
"includes": [
"extension.neon"
]
},
"readme": {
"sections": [
"Using",
Expand All @@ -58,6 +67,11 @@
"WP_CLI\\Tests\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"WP_CLI\\Tests\\Tests\\": "tests/tests"
}
},
"minimum-stability": "dev",
"prefer-stable": true,
"bin": [
Expand Down
51 changes: 51 additions & 0 deletions extension.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
services:
-
class: WP_CLI\Tests\PHPStan\ParseUrlFunctionDynamicReturnTypeExtension
tags:
- phpstan.broker.dynamicFunctionReturnTypeExtension
-
class: WP_CLI\Tests\PHPStan\GetFlagValueFunctionDynamicReturnTypeExtension
tags:
- phpstan.broker.dynamicFunctionReturnTypeExtension
-
class: WP_CLI\Tests\PHPStan\WPCliRuncommandDynamicReturnTypeExtension
tags:
- phpstan.broker.dynamicStaticMethodReturnTypeExtension
parameters:
dynamicConstantNames:
- FOO
strictRules:
allRules: false
disallowedLooseComparison: false
booleansInConditions: false
uselessCast: false
requireParentConstructorCall: false
disallowedConstructs: false
overwriteVariablesWithLoop: false
closureUsesThis: false
matchingInheritedMethodNames: false
numericOperandsInArithmeticOperators: false
strictCalls: false
switchConditionsMatchingType: false
noVariableVariables: false
strictArrayFilter: false

# Add the schema from phpstan-strict-rules so it's available without loading the extension
# and the above configuration works.
parametersSchema:
strictRules: structure([
allRules: anyOf(bool(), arrayOf(bool())),
disallowedLooseComparison: anyOf(bool(), arrayOf(bool())),
booleansInConditions: anyOf(bool(), arrayOf(bool()))
uselessCast: anyOf(bool(), arrayOf(bool()))
requireParentConstructorCall: anyOf(bool(), arrayOf(bool()))
disallowedConstructs: anyOf(bool(), arrayOf(bool()))
overwriteVariablesWithLoop: anyOf(bool(), arrayOf(bool()))
closureUsesThis: anyOf(bool(), arrayOf(bool()))
matchingInheritedMethodNames: anyOf(bool(), arrayOf(bool()))
numericOperandsInArithmeticOperators: anyOf(bool(), arrayOf(bool()))
strictCalls: anyOf(bool(), arrayOf(bool()))
switchConditionsMatchingType: anyOf(bool(), arrayOf(bool()))
noVariableVariables: anyOf(bool(), arrayOf(bool()))
strictArrayFilter: anyOf(bool(), arrayOf(bool()))
])
14 changes: 14 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,20 @@

<exclude-pattern>tests/phpstan/scan-files.php</exclude-pattern>

<rule ref="WordPress.NamingConventions">
<exclude-pattern>tests/data/*</exclude-pattern>
<exclude-pattern>src/PHPStan/*</exclude-pattern>
<exclude-pattern>tests/tests/PHPStan/*</exclude-pattern>
</rule>

<rule ref="WordPress.WP.AlternativeFunctions.parse_url_parse_url">
<exclude-pattern>tests/data/*</exclude-pattern>
</rule>

<rule ref="Squiz.PHP.CommentedOutCode.Found">
<exclude-pattern>src/PHPStan/*</exclude-pattern>
</rule>

<!-- This is a procedural stand-alone file that is never loaded in a WordPress context,
so this file does not have to comply with WP naming conventions. -->
<rule ref="WordPress.NamingConventions.PrefixAllGlobals">
Expand Down
14 changes: 11 additions & 3 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
includes:
- extension.neon
parameters:
level: 6
paths:
- src
- tests
excludePaths:
- tests/data
scanDirectories:
- vendor/wp-cli/wp-cli
- vendor/phpunit/php-code-coverage
Expand All @@ -11,9 +15,13 @@ parameters:
- tests/phpstan/scan-files.php
treatPhpDocTypesAsCertain: false
dynamicConstantNames:
- WP_DEBUG
- WP_DEBUG_LOG
- WP_DEBUG_DISPLAY
- WP_DEBUG
- WP_DEBUG_LOG
- WP_DEBUG_DISPLAY
ignoreErrors:
# Needs fixing in WP-CLI.
- message: '#Parameter \#1 \$cmd of function WP_CLI\\Utils\\esc_cmd expects array<string>#'
- message: '#Dynamic call to static method#'
path: 'tests/tests'
strictRules:
strictCalls: true
2 changes: 2 additions & 0 deletions src/Context/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@

/**
* Features context.
*
* @phpstan-ignore class.implementsDeprecatedInterface
*/
class FeatureContext implements SnippetAcceptingContext {

Expand Down
2 changes: 1 addition & 1 deletion src/Context/GivenStepDefinitions.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
);
}

$this->remove_dir( $dir );
self::remove_dir( $dir );

Check warning on line 69 in src/Context/GivenStepDefinitions.php

View check run for this annotation

Codecov / codecov/patch

src/Context/GivenStepDefinitions.php#L69

Added line #L69 was not covered by tests
if ( 'empty' === $empty_or_nonexistent ) {
mkdir( $dir, 0777, true /*recursive*/ );
}
Expand Down
1 change: 1 addition & 0 deletions src/Context/ThenStepDefinitions.php
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,7 @@ public function then_an_email_should_be_sent( $expected ): void {
* @param int $return_code Expected HTTP status code.
*/
public function then_the_http_status_code_should_be( $return_code ): void {
// @phpstan-ignore staticMethod.deprecatedClass
$response = Requests::request( 'http://localhost:8080' );
$this->assert_equals( $return_code, $response->status_code );
}
Expand Down
114 changes: 114 additions & 0 deletions src/PHPStan/GetFlagValueFunctionDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php

/**
* Set return type of \WP_CLI\Utils\get_flag_value().
*/

declare(strict_types=1);

namespace WP_CLI\Tests\PHPStan;

use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\Type;
use PHPStan\Type\MixedType;
use PHPStan\Type\NullType;
use PHPStan\Type\TypeCombinator;

use function count;

final class GetFlagValueFunctionDynamicReturnTypeExtension implements \PHPStan\Type\DynamicFunctionReturnTypeExtension {

public function isFunctionSupported( FunctionReflection $functionReflection ): bool {
return $functionReflection->getName() === 'WP_CLI\Utils\get_flag_value';

Check warning on line 25 in src/PHPStan/GetFlagValueFunctionDynamicReturnTypeExtension.php

View check run for this annotation

Codecov / codecov/patch

src/PHPStan/GetFlagValueFunctionDynamicReturnTypeExtension.php#L24-L25

Added lines #L24 - L25 were not covered by tests
}

public function getTypeFromFunctionCall(

Check warning on line 28 in src/PHPStan/GetFlagValueFunctionDynamicReturnTypeExtension.php

View check run for this annotation

Codecov / codecov/patch

src/PHPStan/GetFlagValueFunctionDynamicReturnTypeExtension.php#L28

Added line #L28 was not covered by tests
FunctionReflection $functionReflection,
FuncCall $functionCall,
Scope $scope
): Type {
$args = $functionCall->getArgs();

Check warning on line 33 in src/PHPStan/GetFlagValueFunctionDynamicReturnTypeExtension.php

View check run for this annotation

Codecov / codecov/patch

src/PHPStan/GetFlagValueFunctionDynamicReturnTypeExtension.php#L33

Added line #L33 was not covered by tests

if ( count( $args ) < 2 ) {

Check warning on line 35 in src/PHPStan/GetFlagValueFunctionDynamicReturnTypeExtension.php

View check run for this annotation

Codecov / codecov/patch

src/PHPStan/GetFlagValueFunctionDynamicReturnTypeExtension.php#L35

Added line #L35 was not covered by tests
// Not enough arguments, fall back to the function's declared return type.
return $functionReflection->getVariants()[0]->getReturnType();

Check warning on line 37 in src/PHPStan/GetFlagValueFunctionDynamicReturnTypeExtension.php

View check run for this annotation

Codecov / codecov/patch

src/PHPStan/GetFlagValueFunctionDynamicReturnTypeExtension.php#L37

Added line #L37 was not covered by tests
}

$assocArgsType = $scope->getType( $args[0]->value );
$flagArgType = $scope->getType( $args[1]->value );

Check warning on line 41 in src/PHPStan/GetFlagValueFunctionDynamicReturnTypeExtension.php

View check run for this annotation

Codecov / codecov/patch

src/PHPStan/GetFlagValueFunctionDynamicReturnTypeExtension.php#L40-L41

Added lines #L40 - L41 were not covered by tests

// 2. Determine the default type
$defaultType = isset( $args[2] ) ? $scope->getType( $args[2]->value ) : new NullType();

Check warning on line 44 in src/PHPStan/GetFlagValueFunctionDynamicReturnTypeExtension.php

View check run for this annotation

Codecov / codecov/patch

src/PHPStan/GetFlagValueFunctionDynamicReturnTypeExtension.php#L44

Added line #L44 was not covered by tests

$flagConstantStrings = $flagArgType->getConstantStrings();

Check warning on line 46 in src/PHPStan/GetFlagValueFunctionDynamicReturnTypeExtension.php

View check run for this annotation

Codecov / codecov/patch

src/PHPStan/GetFlagValueFunctionDynamicReturnTypeExtension.php#L46

Added line #L46 was not covered by tests

if ( count( $flagConstantStrings ) !== 1 ) {

Check warning on line 48 in src/PHPStan/GetFlagValueFunctionDynamicReturnTypeExtension.php

View check run for this annotation

Codecov / codecov/patch

src/PHPStan/GetFlagValueFunctionDynamicReturnTypeExtension.php#L48

Added line #L48 was not covered by tests
// Flag name is dynamic or not a string.
// Return type is a union of all possible values in $assoc_args + default type.
return $this->getDynamicFlagFallbackType( $assocArgsType, $defaultType );

Check warning on line 51 in src/PHPStan/GetFlagValueFunctionDynamicReturnTypeExtension.php

View check run for this annotation

Codecov / codecov/patch

src/PHPStan/GetFlagValueFunctionDynamicReturnTypeExtension.php#L51

Added line #L51 was not covered by tests
}

// 4. Flag is a single constant string.
$flagValue = $flagConstantStrings[0]->getValue();

Check warning on line 55 in src/PHPStan/GetFlagValueFunctionDynamicReturnTypeExtension.php

View check run for this annotation

Codecov / codecov/patch

src/PHPStan/GetFlagValueFunctionDynamicReturnTypeExtension.php#L55

Added line #L55 was not covered by tests

// 4.a. If $assoc_args is a single ConstantArray:
$assocConstantArrays = $assocArgsType->getConstantArrays();
if ( count( $assocConstantArrays ) === 1 ) {
$assocArgsConstantArray = $assocConstantArrays[0];
$keyTypes = $assocArgsConstantArray->getKeyTypes();
$valueTypes = $assocArgsConstantArray->getValueTypes();
$resolvedValueType = null;

Check warning on line 63 in src/PHPStan/GetFlagValueFunctionDynamicReturnTypeExtension.php

View check run for this annotation

Codecov / codecov/patch

src/PHPStan/GetFlagValueFunctionDynamicReturnTypeExtension.php#L58-L63

Added lines #L58 - L63 were not covered by tests

foreach ( $keyTypes as $index => $keyType ) {
$keyConstantStrings = $keyType->getConstantStrings();
if ( count( $keyConstantStrings ) === 1 && $keyConstantStrings[0]->getValue() === $flagValue ) {
$resolvedValueType = $valueTypes[ $index ];
break;

Check warning on line 69 in src/PHPStan/GetFlagValueFunctionDynamicReturnTypeExtension.php

View check run for this annotation

Codecov / codecov/patch

src/PHPStan/GetFlagValueFunctionDynamicReturnTypeExtension.php#L65-L69

Added lines #L65 - L69 were not covered by tests
}
}

if ( null !== $resolvedValueType ) {

Check warning on line 73 in src/PHPStan/GetFlagValueFunctionDynamicReturnTypeExtension.php

View check run for this annotation

Codecov / codecov/patch

src/PHPStan/GetFlagValueFunctionDynamicReturnTypeExtension.php#L73

Added line #L73 was not covered by tests
// Key definitely exists and has a resolved type.
return $resolvedValueType;

Check warning on line 75 in src/PHPStan/GetFlagValueFunctionDynamicReturnTypeExtension.php

View check run for this annotation

Codecov / codecov/patch

src/PHPStan/GetFlagValueFunctionDynamicReturnTypeExtension.php#L75

Added line #L75 was not covered by tests
} else {
// Key definitely does not exist in this constant array.
return $defaultType;

Check warning on line 78 in src/PHPStan/GetFlagValueFunctionDynamicReturnTypeExtension.php

View check run for this annotation

Codecov / codecov/patch

src/PHPStan/GetFlagValueFunctionDynamicReturnTypeExtension.php#L78

Added line #L78 was not covered by tests
}
}

// 4.b. $assoc_args is not a single ConstantArray (but $flagValue is known):
// Use getOffsetValueType for other array-like types.
$valueForKeyType = $assocArgsType->getOffsetValueType( new ConstantStringType( $flagValue ) );

Check warning on line 84 in src/PHPStan/GetFlagValueFunctionDynamicReturnTypeExtension.php

View check run for this annotation

Codecov / codecov/patch

src/PHPStan/GetFlagValueFunctionDynamicReturnTypeExtension.php#L84

Added line #L84 was not covered by tests

// The key might exist, or its presence is unknown.
// The function returns $assoc_args[$flag] if set, otherwise $default.
return TypeCombinator::union( $valueForKeyType, $defaultType );

Check warning on line 88 in src/PHPStan/GetFlagValueFunctionDynamicReturnTypeExtension.php

View check run for this annotation

Codecov / codecov/patch

src/PHPStan/GetFlagValueFunctionDynamicReturnTypeExtension.php#L88

Added line #L88 was not covered by tests
}

/**
* Handles the case where the flag name is not a single known constant string.
* The return type is a union of all possible values in $assocArgsType and $defaultType.
*/
private function getDynamicFlagFallbackType( Type $assocArgsType, Type $defaultType ): Type {
$possibleValueTypes = [];

Check warning on line 96 in src/PHPStan/GetFlagValueFunctionDynamicReturnTypeExtension.php

View check run for this annotation

Codecov / codecov/patch

src/PHPStan/GetFlagValueFunctionDynamicReturnTypeExtension.php#L95-L96

Added lines #L95 - L96 were not covered by tests

$assocConstantArrays = $assocArgsType->getConstantArrays();
if ( count( $assocConstantArrays ) === 1 ) { // It's one specific constant array
$constantArray = $assocConstantArrays[0];
if ( count( $constantArray->getValueTypes() ) > 0 ) {
$possibleValueTypes = $constantArray->getValueTypes();

Check warning on line 102 in src/PHPStan/GetFlagValueFunctionDynamicReturnTypeExtension.php

View check run for this annotation

Codecov / codecov/patch

src/PHPStan/GetFlagValueFunctionDynamicReturnTypeExtension.php#L98-L102

Added lines #L98 - L102 were not covered by tests
}
} else {
$possibleValueTypes[] = new MixedType();

Check warning on line 105 in src/PHPStan/GetFlagValueFunctionDynamicReturnTypeExtension.php

View check run for this annotation

Codecov / codecov/patch

src/PHPStan/GetFlagValueFunctionDynamicReturnTypeExtension.php#L105

Added line #L105 was not covered by tests
}

if ( empty( $possibleValueTypes ) ) {
return $defaultType;

Check warning on line 109 in src/PHPStan/GetFlagValueFunctionDynamicReturnTypeExtension.php

View check run for this annotation

Codecov / codecov/patch

src/PHPStan/GetFlagValueFunctionDynamicReturnTypeExtension.php#L108-L109

Added lines #L108 - L109 were not covered by tests
}

return TypeCombinator::union( $defaultType, ...$possibleValueTypes );

Check warning on line 112 in src/PHPStan/GetFlagValueFunctionDynamicReturnTypeExtension.php

View check run for this annotation

Codecov / codecov/patch

src/PHPStan/GetFlagValueFunctionDynamicReturnTypeExtension.php#L112

Added line #L112 was not covered by tests
}
}
Loading