-
Notifications
You must be signed in to change notification settings - Fork 478
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix implode() for non-empty-string return type
- Loading branch information
1 parent
5eb96f5
commit 520ae22
Showing
3 changed files
with
163 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
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,69 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Php; | ||
|
||
use PhpParser\Node\Expr\FuncCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\FunctionReflection; | ||
use PHPStan\Type\Accessory\AccessoryNonEmptyStringType; | ||
use PHPStan\Type\DynamicFunctionReturnTypeExtension; | ||
use PHPStan\Type\IntersectionType; | ||
use PHPStan\Type\StringType; | ||
|
||
class ImplodeFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension | ||
{ | ||
|
||
public function isFunctionSupported(FunctionReflection $functionReflection): bool | ||
{ | ||
return in_array($functionReflection->getName(), [ | ||
'implode', | ||
'join', | ||
], true); | ||
} | ||
|
||
public function getTypeFromFunctionCall( | ||
FunctionReflection $functionReflection, | ||
FuncCall $functionCall, | ||
Scope $scope | ||
): \PHPStan\Type\Type | ||
{ | ||
$args = $functionCall->args; | ||
if (count($args) === 1) { | ||
$argType = $scope->getType($args[0]->value); | ||
if ($argType->isArray()->yes()) { | ||
if ($argType->isIterableAtLeastOnce()->yes() && $argType->getIterableValueType()->isNonEmptyString()->yes()) { | ||
return new IntersectionType([ | ||
new StringType(), | ||
new AccessoryNonEmptyStringType(), | ||
]); | ||
} | ||
|
||
return new StringType(); | ||
} | ||
} | ||
|
||
if (count($args) !== 2) { | ||
return new StringType(); | ||
} | ||
|
||
$separatorType = $scope->getType($args[0]->value); | ||
$arrayType = $scope->getType($args[1]->value); | ||
if ($arrayType->isIterableAtLeastOnce()->yes()) { | ||
if ($arrayType->getIterableValueType()->isNonEmptyString()->yes()) { | ||
return new IntersectionType([ | ||
new StringType(), | ||
new AccessoryNonEmptyStringType(), | ||
]); | ||
} | ||
if ($separatorType->isNonEmptyString()->yes()) { | ||
return new IntersectionType([ | ||
new StringType(), | ||
new AccessoryNonEmptyStringType(), | ||
]); | ||
} | ||
} | ||
|
||
return 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