Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ImplodeFunctionRule #679

Merged
merged 25 commits into from Sep 22, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/Type/Php/ImplodeFunctionReturnTypeExtension.php
Expand Up @@ -8,8 +8,10 @@
use PHPStan\Type\Accessory\AccessoryLiteralStringType;
use PHPStan\Type\Accessory\AccessoryNonEmptyStringType;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\ErrorType;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\StringType;
use PHPStan\Type\UnionType;

class ImplodeFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension
{
Expand Down Expand Up @@ -62,6 +64,17 @@ public function getTypeFromFunctionCall(
}
}

if ($arrayType->getIterableValueType()->isArray()->yes()) {
return new ErrorType();
}
if ($arrayType->getIterableValueType() instanceof UnionType) {
foreach ($arrayType->getIterableValueType()->getTypes() as $subType) {
if ($subType->isArray()->yes()) {
return new ErrorType();
staabm marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

if ($arrayType->getIterableValueType()->isLiteralString()->yes() && $separatorType->isLiteralString()->yes()) {
$accessoryTypes[] = new AccessoryLiteralStringType();
}
Expand Down
2 changes: 2 additions & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Expand Up @@ -510,6 +510,8 @@ public function dataFileAsserts(): iterable

yield from $this->gatherAssertTypes(__DIR__ . '/data/eval-implicit-throw.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-5628.php');

yield from $this->gatherAssertTypes(__DIR__ . '/data/implode.php');
}

/**
Expand Down
14 changes: 14 additions & 0 deletions tests/PHPStan/Analyser/data/implode.php
@@ -0,0 +1,14 @@
<?php

namespace ImplodeErrors;

use function PHPStan\Testing\assertType;

class Foo
{
public function invalidArgs(): void
{
assertType('*ERROR*', implode('',['12','123',['1234','12345']]));
assertType('*ERROR*', implode('',[['1234','12345']]));
}
}