Skip to content
Permalink
Browse files

Move explode handling into its own file

  • Loading branch information
muglug committed Dec 30, 2019
1 parent b7fa15f commit ec292f58dc6265a50acbc84917065d0fca1f037d
@@ -242,44 +242,6 @@ public static function getReturnTypeFromCallMapWithArgs(

break;

case 'explode':
if (count($call_args) >= 2) {
$can_return_empty = isset($call_args[2])
&& (
!$call_args[2]->value instanceof PhpParser\Node\Scalar\LNumber
|| $call_args[2]->value->value < 0
);

if ($call_args[0]->value instanceof PhpParser\Node\Scalar\String_) {
if ($call_args[0]->value->value === '') {
return Type::getFalse();
}

return new Type\Union([
$can_return_empty
? new Type\Atomic\TList(Type::getString())
: new Type\Atomic\TNonEmptyList(Type::getString())
]);
} elseif (($first_arg_type = $statements_analyzer->node_data->getType($call_args[0]->value))
&& $first_arg_type->hasString()
) {
$falsable_array = new Type\Union([
$can_return_empty
? new Type\Atomic\TList(Type::getString())
: new Type\Atomic\TNonEmptyList(Type::getString()),
new Type\Atomic\TFalse
]);

if ($codebase->config->ignore_internal_falsable_issues) {
$falsable_array->ignore_falsable_issues = true;
}

return $falsable_array;
}
}

break;

case 'abs':
if (isset($call_args[0]->value)) {
$first_arg = $call_args[0]->value;
@@ -53,6 +53,7 @@ public function __construct()
$this->registerClass(ReturnTypeProvider\StrReplaceReturnTypeProvider::class);
$this->registerClass(ReturnTypeProvider\VersionCompareReturnTypeProvider::class);
$this->registerClass(ReturnTypeProvider\MktimeReturnTypeProvider::class);
$this->registerClass(ReturnTypeProvider\ExplodeReturnTypeProvider::class);
$this->registerClass(ReturnTypeProvider\GetObjectVarsReturnTypeProvider::class);
$this->registerClass(ReturnTypeProvider\GetClassMethodsReturnTypeProvider::class);
$this->registerClass(ReturnTypeProvider\FirstArgStringReturnTypeProvider::class);
@@ -0,0 +1,68 @@
<?php
namespace Psalm\Internal\Provider\ReturnTypeProvider;

use PhpParser;
use Psalm\CodeLocation;
use Psalm\Context;
use Psalm\StatementsSource;
use Psalm\Type;

class ExplodeReturnTypeProvider implements \Psalm\Plugin\Hook\FunctionReturnTypeProviderInterface
{
public static function getFunctionIds() : array
{
return ['explode'];
}

/**
* @param array<PhpParser\Node\Arg> $call_args
*/
public static function getFunctionReturnType(
StatementsSource $statements_source,
string $function_id,
array $call_args,
Context $context,
CodeLocation $code_location
) : Type\Union {
if (!$statements_source instanceof \Psalm\Internal\Analyzer\StatementsAnalyzer) {
return Type::getMixed();
}

if (count($call_args) >= 2) {
$can_return_empty = isset($call_args[2])
&& (
!$call_args[2]->value instanceof PhpParser\Node\Scalar\LNumber
|| $call_args[2]->value->value < 0
);

if ($call_args[0]->value instanceof PhpParser\Node\Scalar\String_) {
if ($call_args[0]->value->value === '') {
return Type::getFalse();
}

return new Type\Union([
$can_return_empty
? new Type\Atomic\TList(Type::getString())
: new Type\Atomic\TNonEmptyList(Type::getString())
]);
} elseif (($first_arg_type = $statements_source->node_data->getType($call_args[0]->value))
&& $first_arg_type->hasString()
) {
$falsable_array = new Type\Union([
$can_return_empty
? new Type\Atomic\TList(Type::getString())
: new Type\Atomic\TNonEmptyList(Type::getString()),
new Type\Atomic\TFalse
]);

if ($statements_source->getCodebase()->config->ignore_internal_falsable_issues) {
$falsable_array->ignore_falsable_issues = true;
}

return $falsable_array;
}
}

return Type::getMixed();
}
}

0 comments on commit ec292f5

Please sign in to comment.
You can’t perform that action at this time.