Skip to content

Commit

Permalink
Fix implode() for non-empty-string return type
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jul 14, 2021
1 parent 5eb96f5 commit 520ae22
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 0 deletions.
5 changes: 5 additions & 0 deletions conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -1250,6 +1250,11 @@ services:
tags:
- phpstan.broker.dynamicFunctionReturnTypeExtension

-
class: PHPStan\Type\Php\ImplodeFunctionReturnTypeExtension
tags:
- phpstan.broker.dynamicFunctionReturnTypeExtension

-
class: PHPStan\Type\Php\ParseUrlFunctionDynamicReturnTypeExtension
tags:
Expand Down
69 changes: 69 additions & 0 deletions src/Type/Php/ImplodeFunctionReturnTypeExtension.php
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();
}

}
89 changes: 89 additions & 0 deletions tests/PHPStan/Analyser/data/non-empty-string.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,92 @@ public function doEmpty2(string $s): void
}

}

class ImplodingStrings
{

/**
* @param array<string> $commonStrings
*/
public function doFoo(string $s, array $commonStrings): void
{
assertType('string', implode($s, $commonStrings));
assertType('string', implode(' ', $commonStrings));
assertType('string', implode('', $commonStrings));
assertType('string', implode($commonStrings));
}

/**
* @param non-empty-array<string> $nonEmptyArrayWithStrings
*/
public function doFoo2(string $s, array $nonEmptyArrayWithStrings): void
{
assertType('string', implode($s, $nonEmptyArrayWithStrings));
assertType('string', implode('', $nonEmptyArrayWithStrings));
assertType('non-empty-string', implode(' ', $nonEmptyArrayWithStrings));
assertType('string', implode($nonEmptyArrayWithStrings));
}

/**
* @param array<non-empty-string> $arrayWithNonEmptyStrings
*/
public function doFoo3(string $s, array $arrayWithNonEmptyStrings): void
{
assertType('string', implode($s, $arrayWithNonEmptyStrings));
assertType('string', implode('', $arrayWithNonEmptyStrings));
assertType('string', implode(' ', $arrayWithNonEmptyStrings));
assertType('string', implode($arrayWithNonEmptyStrings));
}

/**
* @param non-empty-array<non-empty-string> $nonEmptyArrayWithNonEmptyStrings
*/
public function doFoo4(string $s, array $nonEmptyArrayWithNonEmptyStrings): void
{
assertType('non-empty-string', implode($s, $nonEmptyArrayWithNonEmptyStrings));
assertType('non-empty-string', implode('', $nonEmptyArrayWithNonEmptyStrings));
assertType('non-empty-string', implode(' ', $nonEmptyArrayWithNonEmptyStrings));
assertType('non-empty-string', implode($nonEmptyArrayWithNonEmptyStrings));
}

public function sayHello(): void
{
// coming from issue #5291
$s = array(1,2);

assertType('non-empty-string', implode("a", $s));
}

/**
* @param non-empty-string $glue
*/
public function nonE($glue, array $a) {
// coming from issue #5291
if (empty($a)) {
return "xyz";
}

assertType('non-empty-string', implode($glue, $a));
}

public function sayHello2(): void
{
// coming from issue #5291
$s = array(1,2);

assertType('non-empty-string', join("a", $s));
}

/**
* @param non-empty-string $glue
*/
public function nonE2($glue, array $a) {
// coming from issue #5291
if (empty($a)) {
return "xyz";
}

assertType('non-empty-string', join($glue, $a));
}

}

0 comments on commit 520ae22

Please sign in to comment.