Skip to content

Commit a467aa5

Browse files
committed
Add is_countable function
1 parent 3917b51 commit a467aa5

File tree

7 files changed

+66
-0
lines changed

7 files changed

+66
-0
lines changed

Zend/zend_API.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4281,6 +4281,19 @@ ZEND_API zend_bool zend_is_iterable(zval *iterable) /* {{{ */
42814281
}
42824282
/* }}} */
42834283

4284+
ZEND_API zend_bool zend_is_countable(zval *countable) /* {{{ */
4285+
{
4286+
switch (Z_TYPE_P(countable)) {
4287+
case IS_ARRAY:
4288+
return 1;
4289+
case IS_OBJECT:
4290+
return instanceof_function(Z_OBJCE_P(countable), zend_ce_countable);
4291+
default:
4292+
return 0;
4293+
}
4294+
}
4295+
/* }}} */
4296+
42844297
/*
42854298
* Local variables:
42864299
* tab-width: 4

Zend/zend_API.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,8 @@ ZEND_API const char *zend_get_object_type(const zend_class_entry *ce);
564564

565565
ZEND_API zend_bool zend_is_iterable(zval *iterable);
566566

567+
ZEND_API zend_bool zend_is_countable(zval *countable);
568+
567569
#define add_method(arg, key, method) add_assoc_function((arg), (key), (method))
568570

569571
ZEND_API ZEND_FUNCTION(display_disabled_function);

ext/opcache/Optimizer/zend_func_info.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,7 @@ static const func_info_t func_infos[] = {
610610
F0("is_object", MAY_BE_FALSE | MAY_BE_TRUE), // TODO: inline with support for incomplete class
611611
F0("is_scalar", MAY_BE_NULL | MAY_BE_FALSE | MAY_BE_TRUE),
612612
F0("is_callable", MAY_BE_NULL | MAY_BE_FALSE | MAY_BE_TRUE),
613+
F0("is_countable", MAY_BE_NULL | MAY_BE_FALSE | MAY_BE_TRUE),
613614
F0("pclose", MAY_BE_FALSE | MAY_BE_LONG),
614615
F1("popen", MAY_BE_NULL | MAY_BE_FALSE | MAY_BE_RESOURCE),
615616
F0("readfile", MAY_BE_FALSE | MAY_BE_LONG),

ext/standard/basic_functions.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2586,6 +2586,10 @@ ZEND_END_ARG_INFO()
25862586
ZEND_BEGIN_ARG_INFO_EX(arginfo_is_iterable, 0, 0, 1)
25872587
ZEND_ARG_INFO(0, var)
25882588
ZEND_END_ARG_INFO()
2589+
2590+
ZEND_BEGIN_ARG_INFO_EX(arginfo_is_countable, 0, 0, 1)
2591+
ZEND_ARG_INFO(0, var)
2592+
ZEND_END_ARG_INFO()
25892593
/* }}} */
25902594
/* {{{ uniqid.c */
25912595
#ifdef HAVE_GETTIMEOFDAY
@@ -3111,6 +3115,7 @@ static const zend_function_entry basic_functions[] = { /* {{{ */
31113115
PHP_FE(is_scalar, arginfo_is_scalar)
31123116
PHP_FE(is_callable, arginfo_is_callable)
31133117
PHP_FE(is_iterable, arginfo_is_iterable)
3118+
PHP_FE(is_countable, arginfo_is_countable)
31143119

31153120
/* functions from file.c */
31163121
PHP_FE(pclose, arginfo_pclose)

ext/standard/php_type.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,6 @@ PHP_FUNCTION(is_object);
3939
PHP_FUNCTION(is_scalar);
4040
PHP_FUNCTION(is_callable);
4141
PHP_FUNCTION(is_iterable);
42+
PHP_FUNCTION(is_countable);
4243

4344
#endif
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
Test is_countable() function
3+
--FILE--
4+
<?php
5+
6+
var_dump(is_countable([1, 2, 3]));
7+
var_dump(is_countable(new ArrayIterator(['foo', 'bar', 'baz'])));
8+
var_dump(is_countable(new ArrayIterator()));
9+
var_dump(is_countable(new stdClass()));
10+
11+
$foo = ['', []];
12+
13+
if (is_countable($foo)) {
14+
var_dump(count($foo));
15+
}
16+
17+
$bar = null;
18+
if (!is_countable($bar)) {
19+
count($bar);
20+
}
21+
22+
?>
23+
--EXPECTF--
24+
bool(true)
25+
bool(true)
26+
bool(true)
27+
bool(false)
28+
int(2)
29+
30+
Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d

ext/standard/type.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,20 @@ PHP_FUNCTION(is_iterable)
398398
}
399399
/* }}} */
400400

401+
/* {{{ proto bool is_countable(mixed var)
402+
Returns true if var is countable (array or instance of Countable). */
403+
PHP_FUNCTION(is_countable)
404+
{
405+
zval *var;
406+
407+
ZEND_PARSE_PARAMETERS_START(1, 1)
408+
Z_PARAM_ZVAL(var)
409+
ZEND_PARSE_PARAMETERS_END();
410+
411+
RETURN_BOOL(zend_is_countable(var));
412+
}
413+
/* }}} */
414+
401415
/*
402416
* Local variables:
403417
* tab-width: 4

0 commit comments

Comments
 (0)