Skip to content

Commit

Permalink
Use the same version of PHP Parser that PHPStan uses.
Browse files Browse the repository at this point in the history
  • Loading branch information
johnbillion committed Nov 19, 2021
1 parent f082ca2 commit cbdc4e5
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 31 deletions.
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
"require-dev": {
"composer/composer": "^1.10.23",
"dealerdirect/phpcodesniffer-composer-installer": "^0.7",
"nikic/php-parser": "< 4.13.0",
"php-parallel-lint/php-parallel-lint": "^1.1",
"phpstan/phpstan-strict-rules": "^1.0",
"phpunit/phpunit": "^7 || ^9",
Expand Down
5 changes: 3 additions & 2 deletions src/CurrentTimeDynamicFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo
*/
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
{
$argumentType = $scope->getType($functionCall->args[0]->value);
$args = $functionCall->getArgs();
$argumentType = $scope->getType($args[0]->value);

// When called with a $type that isn't a constant string, return default return type
if (! $argumentType instanceof ConstantStringType) {
return ParametersAcceptorSelector::selectFromArgs(
$scope,
$functionCall->args,
$args,
$functionReflection->getVariants()
)->getReturnType();
}
Expand Down
11 changes: 6 additions & 5 deletions src/GetCommentDynamicFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,23 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
{
$output = 'OBJECT';
$args = $functionCall->getArgs();

if (count($functionCall->args) >= 2) {
$argumentType = $scope->getType($functionCall->args[1]->value);
if (count($args) >= 2) {
$argumentType = $scope->getType($args[1]->value);

// When called with an $output that isn't a constant string, return default return type
if (! $argumentType instanceof ConstantStringType) {
return ParametersAcceptorSelector::selectFromArgs(
$scope,
$functionCall->args,
$args,
$functionReflection->getVariants()
)->getReturnType();
}
}

if (count($functionCall->args) >= 2 && $functionCall->args[1]->value instanceof ConstFetch) {
$output = $functionCall->args[1]->value->name->getLast();
if (count($args) >= 2 && $args[1]->value instanceof ConstFetch) {
$output = $args[1]->value->name->getLast();
}
if ($output === 'ARRAY_A') {
return TypeCombinator::union(new ArrayType(new StringType(), new MixedType()), new NullType());
Expand Down
8 changes: 5 additions & 3 deletions src/GetListTableDynamicFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,20 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo
// phpcs:ignore SlevomatCodingStandard.Functions.UnusedParameter
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
{
$args = $functionCall->getArgs();

// Called without $class argument
if (count($functionCall->args) < 1) {
if (count($args) < 1) {
return new ConstantBooleanType(false);
}

$argumentType = $scope->getType($functionCall->args[0]->value);
$argumentType = $scope->getType($args[0]->value);

// When called with a $class that isn't a constant string, return default return type
if (! $argumentType instanceof ConstantStringType) {
return ParametersAcceptorSelector::selectFromArgs(
$scope,
$functionCall->args,
$args,
$functionReflection->getVariants()
)->getReturnType();
}
Expand Down
8 changes: 5 additions & 3 deletions src/GetObjectTaxonomiesDynamicFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,20 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo
*/
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
{
$args = $functionCall->getArgs();

// Called without second $output argument
if (count($functionCall->args) <= 1) {
if (count($args) <= 1) {
return new ArrayType(new IntegerType(), new StringType());
}

$argumentType = $scope->getType($functionCall->args[1]->value);
$argumentType = $scope->getType($args[1]->value);

// When called with an $output that isn't a constant string, return default return type
if (! $argumentType instanceof ConstantStringType) {
return ParametersAcceptorSelector::selectFromArgs(
$scope,
$functionCall->args,
$args,
$functionReflection->getVariants()
)->getReturnType();
}
Expand Down
9 changes: 5 additions & 4 deletions src/GetPostDynamicFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,23 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
{
$output = 'OBJECT';
$args = $functionCall->getArgs();

if (count($functionCall->args) >= 2) {
$argumentType = $scope->getType($functionCall->args[1]->value);
$argumentType = $scope->getType($args[1]->value);

// When called with an $output that isn't a constant string, return default return type
if (! $argumentType instanceof ConstantStringType) {
return ParametersAcceptorSelector::selectFromArgs(
$scope,
$functionCall->args,
$args,
$functionReflection->getVariants()
)->getReturnType();
}
}

if (count($functionCall->args) >= 2 && $functionCall->args[1]->value instanceof ConstFetch) {
$output = $functionCall->args[1]->value->name->getLast();
if (count($args) >= 2 && $args[1]->value instanceof ConstFetch) {
$output = $args[1]->value->name->getLast();
}
if ($output === 'ARRAY_A') {
return TypeCombinator::union(new ArrayType(new StringType(), new MixedType()), new NullType());
Expand Down
6 changes: 4 additions & 2 deletions src/GetPostsDynamicFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo
// phpcs:ignore SlevomatCodingStandard.Functions.UnusedParameter
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
{
$args = $functionCall->getArgs();

// Called without arguments
if (count($functionCall->args) === 0) {
if (count($args) === 0) {
return new ArrayType(new IntegerType(), new ObjectType('WP_Post'));
}

$argumentType = $scope->getType($functionCall->args[0]->value);
$argumentType = $scope->getType($args[0]->value);

// Called with an array argument
if ($argumentType instanceof ConstantArrayType) {
Expand Down
6 changes: 4 additions & 2 deletions src/GetTaxonomiesDynamicFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
$namesReturnType
);

$args = $functionCall->getArgs();
// Called without second $output arguments
if (count($functionCall->args) <= 1) {

if (count($args) <= 1) {
return $namesReturnType;
}

$argumentType = $scope->getType($functionCall->args[1]->value);
$argumentType = $scope->getType($args[1]->value);

// When called with a non-string $output, return default return type
if (! $argumentType instanceof ConstantStringType) {
Expand Down
4 changes: 3 additions & 1 deletion src/IsWpErrorFunctionTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ public function specifyTypes(FunctionReflection $functionReflection, FuncCall $n
throw new \PHPStan\ShouldNotHappenException();
}

return $this->typeSpecifier->create($node->args[0]->value, new ObjectType('WP_Error'), $context);
$args = $node->getArgs();

return $this->typeSpecifier->create($args[0]->value, new ObjectType('WP_Error'), $context);
}

public function setTypeSpecifier(TypeSpecifier $typeSpecifier): void
Expand Down
5 changes: 3 additions & 2 deletions src/MySQL2DateDynamicFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo
*/
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
{
$argumentType = $scope->getType($functionCall->args[0]->value);
$args = $functionCall->getArgs();
$argumentType = $scope->getType($args[0]->value);

// When called with a $format that isn't a constant string, return default return type
if (! $argumentType instanceof ConstantStringType) {
return ParametersAcceptorSelector::selectFromArgs(
$scope,
$functionCall->args,
$args,
$functionReflection->getVariants()
)->getReturnType();
}
Expand Down
7 changes: 4 additions & 3 deletions src/ShortcodeAttsDynamicFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,16 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo

public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
{
if ($functionCall->args === []) {
$args = $functionCall->getArgs();
if ($args === []) {
return ParametersAcceptorSelector::selectFromArgs(
$scope,
$functionCall->args,
$args,
$functionReflection->getVariants()
)->getReturnType();
}

$type = $scope->getType($functionCall->args[0]->value);
$type = $scope->getType($args[0]->value);

if ($type instanceof ConstantArrayType) {
// shortcode_atts values are coming from the defined defaults or from the actual string shortcode attributes
Expand Down
7 changes: 4 additions & 3 deletions src/StringOrArrayDynamicFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,17 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo

public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
{
$argsCount = count($functionCall->args);
$args = $functionCall->getArgs();
$argsCount = count($args);
if ($argsCount === 0) {
return ParametersAcceptorSelector::selectFromArgs(
$scope,
$functionCall->args,
$args,
$functionReflection->getVariants()
)->getReturnType();
}

$dataArg = $functionCall->args[0]->value;
$dataArg = $args[0]->value;
$dataArgType = $scope->getType($dataArg);
if ($dataArgType instanceof ArrayType) {
$keyType = $dataArgType->getIterableKeyType();
Expand Down

0 comments on commit cbdc4e5

Please sign in to comment.