-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle return-type of HeaderCollection->get()
- Loading branch information
1 parent
ab51e6b
commit 76aa3bb
Showing
3 changed files
with
63 additions
and
0 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
55 changes: 55 additions & 0 deletions
55
src/Type/HeaderCollectionDynamicMethodReturnTypeExtension.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,55 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Proget\PHPStan\Yii2\Type; | ||
|
||
use PhpParser\Node\Expr\ConstFetch; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\MethodReflection; | ||
use PHPStan\Type\ArrayType; | ||
use PHPStan\Type\DynamicMethodReturnTypeExtension; | ||
use PHPStan\Type\IntegerType; | ||
use PHPStan\Type\StringType; | ||
use PHPStan\Type\Type; | ||
use PHPStan\Type\UnionType; | ||
use yii\web\HeaderCollection; | ||
|
||
class HeaderCollectionDynamicMethodReturnTypeExtension implements DynamicMethodReturnTypeExtension | ||
{ | ||
public function getClass(): string | ||
{ | ||
return HeaderCollection::class; | ||
} | ||
|
||
public function isMethodSupported(MethodReflection $methodReflection): bool | ||
{ | ||
return $methodReflection->getName() === 'get'; | ||
} | ||
|
||
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type | ||
{ | ||
if (count($methodCall->args) < 3) { | ||
// $first === true (the default) and the get-method returns something of type string | ||
return new StringType(); | ||
} | ||
|
||
$val = $methodCall->args[2]->value; | ||
if ($val instanceof ConstFetch) { | ||
$value = $val->name->parts[0]; | ||
if ($value === 'true') { | ||
// $first === true, therefore string | ||
return new StringType(); | ||
} | ||
|
||
if ($value === 'false') { | ||
// $first === false, therefore string[] | ||
return new ArrayType(new IntegerType(), new StringType()); | ||
} | ||
} | ||
|
||
// Unable to figure out value of third parameter $first, therefore it can be of either type | ||
return new UnionType([new ArrayType(new IntegerType(), new StringType()), new StringType()]); | ||
} | ||
} |
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