forked from GoogleForCreators/web-stories-wp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Install phpstan via composer (GoogleForCreators#4540)
- Loading branch information
1 parent
bad3f1d
commit e895876
Showing
10 changed files
with
373 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
tests/phpstan/src/GetPostDynamicFunctionReturnTypeExtension.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
/** | ||
* Copied from szepeviktor/phpstan-wordpress | ||
*/ | ||
|
||
/** | ||
* Set return type of get_post(). | ||
*/ | ||
|
||
namespace PHPStan\WordPress; | ||
|
||
use PhpParser\Node\Expr\ConstFetch; | ||
use PhpParser\Node\Expr\FuncCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\FunctionReflection; | ||
use PHPStan\Type\Type; | ||
use PHPStan\Type\ArrayType; | ||
use PHPStan\Type\StringType; | ||
use PHPStan\Type\IntegerType; | ||
use PHPStan\Type\MixedType; | ||
use PHPStan\Type\ObjectType; | ||
use PHPStan\Type\NullType; | ||
use PHPStan\Type\TypeCombinator; | ||
|
||
class GetPostDynamicFunctionReturnTypeExtension implements \PHPStan\Type\DynamicFunctionReturnTypeExtension | ||
{ | ||
public function isFunctionSupported(FunctionReflection $functionReflection): bool | ||
{ | ||
return in_array($functionReflection->getName(), ['get_post', 'get_page_by_path'], true); | ||
} | ||
|
||
// phpcs:ignore SlevomatCodingStandard.Functions.UnusedParameter | ||
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type | ||
{ | ||
$output = 'OBJECT'; | ||
if (count($functionCall->args) >= 2 && $functionCall->args[1]->value instanceof ConstFetch) { | ||
$output = $functionCall->args[1]->value->name->getLast(); | ||
} | ||
if ($output === 'ARRAY_A') { | ||
return TypeCombinator::union(new ArrayType(new StringType(), new MixedType()), new NullType()); | ||
} | ||
if ($output === 'ARRAY_N') { | ||
return TypeCombinator::union(new ArrayType(new IntegerType(), new MixedType()), new NullType()); | ||
} | ||
|
||
return TypeCombinator::union(new ObjectType('WP_Post'), new NullType()); | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
tests/phpstan/src/GetPostsDynamicFunctionReturnTypeExtension.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
/** | ||
* Copied from szepeviktor/phpstan-wordpress | ||
*/ | ||
|
||
/** | ||
* Set return type of get_post(). | ||
*/ | ||
|
||
namespace PHPStan\WordPress; | ||
|
||
use PhpParser\Node\Expr\FuncCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\FunctionReflection; | ||
use PHPStan\Reflection\ParametersAcceptorSelector; | ||
use PHPStan\Type\Type; | ||
use PHPStan\Type\ArrayType; | ||
use PHPStan\Type\IntegerType; | ||
use PHPStan\Type\ObjectType; | ||
use PHPStan\Type\Constant\ConstantArrayType; | ||
use PHPStan\Type\Constant\ConstantStringType; | ||
|
||
class GetPostsDynamicFunctionReturnTypeExtension implements \PHPStan\Type\DynamicFunctionReturnTypeExtension | ||
{ | ||
public function isFunctionSupported(FunctionReflection $functionReflection): bool | ||
{ | ||
return in_array($functionReflection->getName(), ['get_posts'], true); | ||
} | ||
|
||
/** | ||
* @see https://developer.wordpress.org/reference/classes/wp_query/#return-fields-parameter | ||
*/ | ||
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type | ||
{ | ||
// Called without arguments | ||
if (count($functionCall->args) === 0) { | ||
return new ArrayType(new IntegerType(), new ObjectType('WP_Post')); | ||
} | ||
|
||
$argumentType = $scope->getType($functionCall->args[0]->value); | ||
|
||
// Called with an array argument | ||
if ($argumentType instanceof ConstantArrayType) { | ||
foreach ($argumentType->getKeyTypes() as $index => $key) { | ||
if (! $key instanceof ConstantStringType || $key->getValue() !== 'fields') { | ||
continue; | ||
} | ||
|
||
$fieldsType = $argumentType->getValueTypes()[$index]; | ||
if ($fieldsType instanceof ConstantStringType) { | ||
$fields = $fieldsType->getValue(); | ||
} | ||
break; | ||
} | ||
} | ||
// Called with a string argument | ||
if ($argumentType instanceof ConstantStringType) { | ||
parse_str($argumentType->getValue(), $variables); | ||
$fields = $variables['fields'] ?? 'all'; | ||
} | ||
|
||
// Without constant argument return default return type | ||
if (! isset($fields)) { | ||
return ParametersAcceptorSelector::selectFromArgs( | ||
$scope, | ||
$functionCall->args, | ||
$functionReflection->getVariants() | ||
)->getReturnType(); | ||
} | ||
|
||
switch ($fields) { | ||
case 'ids': | ||
return new ArrayType(new IntegerType(), new IntegerType()); | ||
case 'id=>parent': | ||
return new ArrayType(new IntegerType(), new ObjectType('stdClass')); | ||
case 'all': | ||
default: | ||
return new ArrayType(new IntegerType(), new ObjectType('WP_Post')); | ||
} | ||
} | ||
} |
Oops, something went wrong.