Skip to content

Commit

Permalink
Fix get_posts detection
Browse files Browse the repository at this point in the history
  • Loading branch information
szepeviktor committed Feb 2, 2020
1 parent b885e4a commit d4e3826
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
7 changes: 0 additions & 7 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,3 @@ parameters:
-
path: %rootDir%/../../../src/GetPostDynamicFunctionReturnTypeExtension.php
message: "#^Strict comparison using !== between PhpParser\\\\Node\\\\Expr and 'OBJECT' will always evaluate to true\\.$#"
# Accessing value of function arguments.
-
message: "#^Cannot access offset 'fields' on PHPStan\\\\Type\\\\Constant\\\\ConstantArrayType\\.$#"
path: src/GetPostsDynamicFunctionReturnTypeExtension.php
-
message: "#^Parameter \\#1 \\$encoded_string of function parse_str expects string, PHPStan\\\\Type\\\\Constant\\\\ConstantStringType given\\.$#"
path: src/GetPostsDynamicFunctionReturnTypeExtension.php
14 changes: 12 additions & 2 deletions src/GetPostsDynamicFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,21 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,

// Called with an array argument
if ($argumentType instanceof ConstantArrayType) {
$fields = $argumentType['fields'] ?? 'all';
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, $variables);
parse_str($argumentType->getValue(), $variables);
$fields = $variables['fields'] ?? 'all';
}

Expand Down

2 comments on commit d4e3826

@szepeviktor
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lkraav What a 🌟 !
Thank you for starring this repo.

@lkraav
Copy link

@lkraav lkraav commented on d4e3826 Mar 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Y, great work

Please sign in to comment.