Skip to content

Commit

Permalink
refactor ImplodeFunctionReturnTypeExtension
Browse files Browse the repository at this point in the history
  • Loading branch information
clxmstaab committed Feb 4, 2022
1 parent afc49fd commit 180b2af
Showing 1 changed file with 8 additions and 14 deletions.
22 changes: 8 additions & 14 deletions src/Type/Php/ImplodeFunctionReturnTypeExtension.php
Expand Up @@ -7,6 +7,7 @@
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\Accessory\AccessoryLiteralStringType;
use PHPStan\Type\Accessory\AccessoryNonEmptyStringType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\StringType;
Expand Down Expand Up @@ -35,20 +36,7 @@ public function getTypeFromFunctionCall(
if (count($args) === 1) {
$argType = $scope->getType($args[0]->value);
if ($argType->isArray()->yes()) {
$accessoryTypes = [];
if ($argType->isIterableAtLeastOnce()->yes() && $argType->getIterableValueType()->isNonEmptyString()->yes()) {
$accessoryTypes[] = new AccessoryNonEmptyStringType();
}
if ($argType->getIterableValueType()->isLiteralString()->yes()) {
$accessoryTypes[] = new AccessoryLiteralStringType();
}

if (count($accessoryTypes) > 0) {
$accessoryTypes[] = new StringType();
return new IntersectionType($accessoryTypes);
}

return new StringType();
return $this->implode($argType, new ConstantStringType(''));
}
}

Expand All @@ -58,6 +46,12 @@ public function getTypeFromFunctionCall(

$separatorType = $scope->getType($args[0]->value);
$arrayType = $scope->getType($args[1]->value);

return $this->implode($arrayType, $separatorType);
}

private function implode(Type $arrayType, Type $separatorType): Type
{
$accessoryTypes = [];
if ($arrayType->isIterableAtLeastOnce()->yes()) {
if ($arrayType->getIterableValueType()->isNonEmptyString()->yes() || $separatorType->isNonEmptyString()->yes()) {
Expand Down

0 comments on commit 180b2af

Please sign in to comment.