Skip to content
Permalink
Browse files

Fix #2012 - count(callable-array) is fine and always equals 2

  • Loading branch information...
muglug committed Aug 12, 2019
1 parent 10b8a94 commit 58b6d8bed4881648f8c3d98b613af25185fd2e36
@@ -112,19 +112,23 @@ public static function getReturnTypeFromCallMapWithArgs(
if (isset($call_args[0]->value->inferredType)) {
$atomic_types = $call_args[0]->value->inferredType->getTypes();
if (count($atomic_types) === 1 && isset($atomic_types['array'])) {
if ($atomic_types['array'] instanceof Type\Atomic\TNonEmptyArray) {
return new Type\Union([
$atomic_types['array']->count !== null
? new Type\Atomic\TLiteralInt($atomic_types['array']->count)
: new Type\Atomic\TInt
]);
} elseif ($atomic_types['array'] instanceof Type\Atomic\ObjectLike
&& $atomic_types['array']->sealed
) {
return new Type\Union([
new Type\Atomic\TLiteralInt(count($atomic_types['array']->properties))
]);
if (count($atomic_types) === 1) {
if (isset($atomic_types['array'])) {
if ($atomic_types['array'] instanceof Type\Atomic\TNonEmptyArray) {
return new Type\Union([
$atomic_types['array']->count !== null
? new Type\Atomic\TLiteralInt($atomic_types['array']->count)
: new Type\Atomic\TInt
]);
} elseif ($atomic_types['array'] instanceof Type\Atomic\ObjectLike
&& $atomic_types['array']->sealed
) {
return new Type\Union([
new Type\Atomic\TLiteralInt(count($atomic_types['array']->properties))
]);
}
} elseif (isset($atomic_types['callable-array'])) {
return Type::getInt(false, 2);
}
}
}
@@ -724,6 +724,8 @@ public static function isAtomicContainedBy(
}
if ($input_type_part->shallowEquals($container_type_part)
|| ($input_type_part instanceof Type\Atomic\TCallableObjectLikeArray
&& $container_type_part instanceof TArray)
|| (($input_type_part instanceof TNamedObject
|| ($input_type_part instanceof TTemplateParam
&& $input_type_part->as->hasObjectType())
@@ -1698,6 +1698,15 @@ function bar($s) : void {
$xml = new SimpleXMLElement("<?xml version=\"1.0\"?><a><b></b><b></b></a>");
echo count($xml);',
],
'countableCallableArray' => [
'<?php
/** @param callable|false $x */
function example($x) : void {
if (is_array($x)) {
echo "Count is: " . count($x);
}
}'
],
'refineWithTraitExists' => [
'<?php
function foo(string $s) : void {
@@ -2358,6 +2367,17 @@ function foo() : array {
}',
'error_message' => 'UndefinedVariable',
],
'countCallableArrayShouldBeTwo' => [
'<?php
/** @param callable|false $x */
function example($x) : void {
if (is_array($x)) {
$c = count($x);
if ($c !== 2) {}
}
}',
'error_message' => 'TypeDoesNotContainType',
],
];
}
}

0 comments on commit 58b6d8b

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