PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/standard - type.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 141 157 89.8 %
Date: 2016-09-27 Functions: 18 19 94.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 7                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1997-2016 The PHP Group                                |
       6             :    +----------------------------------------------------------------------+
       7             :    | This source file is subject to version 3.01 of the PHP license,      |
       8             :    | that is bundled with this package in the file LICENSE, and is        |
       9             :    | available through the world-wide-web at the following url:           |
      10             :    | http://www.php.net/license/3_01.txt                                  |
      11             :    | If you did not receive a copy of the PHP license and are unable to   |
      12             :    | obtain it through the world-wide-web, please send a note to          |
      13             :    | license@php.net so we can mail you a copy immediately.               |
      14             :    +----------------------------------------------------------------------+
      15             :    | Author: Rasmus Lerdorf <rasmus@php.net>                              |
      16             :    +----------------------------------------------------------------------+
      17             : */
      18             : 
      19             : /* $Id$ */
      20             : 
      21             : #include "php.h"
      22             : #include "php_incomplete_class.h"
      23             : 
      24             : /* {{{ proto string gettype(mixed var)
      25             :    Returns the type of the variable */
      26        2694 : PHP_FUNCTION(gettype)
      27             : {
      28             :         zval *arg;
      29             : 
      30        2694 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &arg) == FAILURE) {
      31           2 :                 return;
      32             :         }
      33             : 
      34        5384 :         switch (Z_TYPE_P(arg)) {
      35             :                 case IS_NULL:
      36         850 :                         RETVAL_STRING("NULL");
      37         425 :                         break;
      38             : 
      39             :                 case IS_FALSE:
      40             :                 case IS_TRUE:
      41         160 :                         RETVAL_STRING("boolean");
      42          80 :                         break;
      43             : 
      44             :                 case IS_LONG:
      45         568 :                         RETVAL_STRING("integer");
      46         284 :                         break;
      47             : 
      48             :                 case IS_DOUBLE:
      49         124 :                         RETVAL_STRING("double");
      50          62 :                         break;
      51             : 
      52             :                 case IS_STRING:
      53        3384 :                         RETVAL_STRING("string");
      54        1692 :                         break;
      55             : 
      56             :                 case IS_ARRAY:
      57         220 :                         RETVAL_STRING("array");
      58         110 :                         break;
      59             : 
      60             :                 case IS_OBJECT:
      61          58 :                         RETVAL_STRING("object");
      62          29 :                         break;
      63             : 
      64             :                 case IS_RESOURCE:
      65             :                         {
      66          10 :                                 const char *type_name = zend_rsrc_list_get_rsrc_type(Z_RES_P(arg));
      67             : 
      68          10 :                                 if (type_name) {
      69          18 :                                         RETVAL_STRING("resource");
      70             :                                 } else {
      71           2 :                                         RETVAL_STRING("resource (closed)");
      72             :                                 }
      73          10 :                                 break;
      74             :                         }
      75             : 
      76             :                 default:
      77           0 :                         RETVAL_STRING("unknown type");
      78             :         }
      79             : }
      80             : /* }}} */
      81             : 
      82             : /* {{{ proto bool settype(mixed var, string type)
      83             :    Set the type of the variable */
      84       45576 : PHP_FUNCTION(settype)
      85             : {
      86             :         zval *var;
      87             :         char *type;
      88       45576 :         size_t type_len = 0;
      89             : 
      90       45576 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "zs", &var, &type, &type_len) == FAILURE) {
      91           2 :                 return;
      92             :         }
      93             : 
      94       91148 :         ZVAL_DEREF(var);
      95       45574 :         if (!strcasecmp(type, "integer")) {
      96          25 :                 convert_to_long(var);
      97       45549 :         } else if (!strcasecmp(type, "int")) {
      98          27 :                 convert_to_long(var);
      99       45522 :         } else if (!strcasecmp(type, "float")) {
     100          14 :                 convert_to_double(var);
     101       45508 :         } else if (!strcasecmp(type, "double")) { /* deprecated */
     102          38 :                 convert_to_double(var);
     103       45470 :         } else if (!strcasecmp(type, "string")) {
     104       90530 :                 convert_to_string(var);
     105         205 :         } else if (!strcasecmp(type, "array")) {
     106          38 :                 convert_to_array(var);
     107         167 :         } else if (!strcasecmp(type, "object")) {
     108          38 :                 convert_to_object(var);
     109         129 :         } else if (!strcasecmp(type, "bool")) {
     110          27 :                 convert_to_boolean(var);
     111         102 :         } else if (!strcasecmp(type, "boolean")) {
     112          25 :                 convert_to_boolean(var);
     113          77 :         } else if (!strcasecmp(type, "null")) {
     114          38 :                 convert_to_null(var);
     115          39 :         } else if (!strcasecmp(type, "resource")) {
     116          38 :                 php_error_docref(NULL, E_WARNING, "Cannot convert to resource type");
     117          38 :                 RETURN_FALSE;
     118             :         } else {
     119           1 :                 php_error_docref(NULL, E_WARNING, "Invalid type");
     120           1 :                 RETURN_FALSE;
     121             :         }
     122       45535 :         RETVAL_TRUE;
     123             : }
     124             : /* }}} */
     125             : 
     126             : /* {{{ proto int intval(mixed var [, int base])
     127             :    Get the integer value of a variable using the optional base for the conversion */
     128          29 : PHP_FUNCTION(intval)
     129             : {
     130             :         zval *num;
     131          29 :         zend_long base = 10;
     132             : 
     133          29 :         if (ZEND_NUM_ARGS() != 1 && ZEND_NUM_ARGS() != 2) {
     134           0 :                 WRONG_PARAM_COUNT;
     135             :         }
     136          29 :         ZEND_PARSE_PARAMETERS_START(1, 2)
     137          29 :                 Z_PARAM_ZVAL(num)
     138          29 :                 Z_PARAM_OPTIONAL
     139          87 :                 Z_PARAM_LONG(base)
     140          29 :         ZEND_PARSE_PARAMETERS_END();
     141             : 
     142          87 :         if (Z_TYPE_P(num) != IS_STRING || base == 10) {
     143          58 :                 RETVAL_LONG(zval_get_long(num));
     144             :         } else {
     145           0 :                 RETVAL_LONG(ZEND_STRTOL(Z_STRVAL_P(num), NULL, base));
     146             :         }
     147             : }
     148             : /* }}} */
     149             : 
     150             : /* {{{ proto float floatval(mixed var)
     151             :    Get the float value of a variable */
     152           8 : PHP_FUNCTION(floatval)
     153             : {
     154             :         zval *num;
     155             : 
     156           8 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &num) == FAILURE) {
     157           8 :                 return;
     158             :         }
     159             : 
     160           0 :         RETURN_DOUBLE(zval_get_double(num));
     161             : }
     162             : /* }}} */
     163             : 
     164             : /* {{{ proto bool boolval(mixed var)
     165             :    Get the boolean value of a variable */
     166           0 : PHP_FUNCTION(boolval)
     167             : {
     168             :         zval *val;
     169             : 
     170           0 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &val) == FAILURE) {
     171           0 :                 return;
     172             :         }
     173             : 
     174           0 :         RETURN_BOOL(zend_is_true(val));
     175             : }
     176             : /* }}} */
     177             : 
     178             : /* {{{ proto string strval(mixed var)
     179             :    Get the string value of a variable */
     180           4 : PHP_FUNCTION(strval)
     181             : {
     182             :         zval *num;
     183             : 
     184           4 :         ZEND_PARSE_PARAMETERS_START(1, 1)
     185           0 :                 Z_PARAM_ZVAL(num)
     186           4 :         ZEND_PARSE_PARAMETERS_END();
     187             : 
     188           0 :         RETVAL_STR(zval_get_string(num));
     189             : }
     190             : /* }}} */
     191             : 
     192          30 : static inline void php_is_type(INTERNAL_FUNCTION_PARAMETERS, int type)
     193             : {
     194             :         zval *arg;
     195             : 
     196          30 :         ZEND_PARSE_PARAMETERS_START(1, 1)
     197          16 :                 Z_PARAM_ZVAL_DEREF(arg)
     198          30 :         ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
     199             : 
     200          16 :         if (Z_TYPE_P(arg) == type) {
     201           6 :                 if (type == IS_RESOURCE) {
     202           0 :                         const char *type_name = zend_rsrc_list_get_rsrc_type(Z_RES_P(arg));
     203           0 :                         if (!type_name) {
     204           0 :                                 RETURN_FALSE;
     205             :                         }
     206             :                 }
     207           6 :                 RETURN_TRUE;
     208             :         } else {
     209           2 :                 RETURN_FALSE;
     210             :         }
     211             : }
     212             : 
     213             : 
     214             : /* {{{ proto bool is_null(mixed var)
     215             :    Returns true if variable is null
     216             :    Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
     217           2 : PHP_FUNCTION(is_null)
     218             : {
     219           2 :         php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_NULL);
     220           2 : }
     221             : /* }}} */
     222             : 
     223             : /* {{{ proto bool is_resource(mixed var)
     224             :    Returns true if variable is a resource
     225             :    Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
     226           2 : PHP_FUNCTION(is_resource)
     227             : {
     228           2 :         php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_RESOURCE);
     229           2 : }
     230             : /* }}} */
     231             : 
     232             : /* {{{ proto bool is_bool(mixed var)
     233             :    Returns true if variable is a boolean
     234             :    Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
     235           2 : PHP_FUNCTION(is_bool)
     236             : {
     237             :         zval *arg;
     238             : 
     239           2 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &arg) == FAILURE) {
     240           2 :                 RETURN_FALSE;
     241             :         }
     242             : 
     243           0 :         ZVAL_DEREF(arg);
     244           0 :         RETURN_BOOL(Z_TYPE_P(arg) == IS_FALSE || Z_TYPE_P(arg) == IS_TRUE);
     245             : }
     246             : /* }}} */
     247             : 
     248             : /* {{{ proto bool is_int(mixed var)
     249             :    Returns true if variable is an integer
     250             :    Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
     251          14 : PHP_FUNCTION(is_int)
     252             : {
     253          14 :         php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_LONG);
     254          14 : }
     255             : /* }}} */
     256             : 
     257             : /* {{{ proto bool is_float(mixed var)
     258             :    Returns true if variable is float point
     259             :    Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
     260           6 : PHP_FUNCTION(is_float)
     261             : {
     262           6 :         php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_DOUBLE);
     263           6 : }
     264             : /* }}} */
     265             : 
     266             : /* {{{ proto bool is_string(mixed var)
     267             :    Returns true if variable is a string
     268             :    Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
     269           2 : PHP_FUNCTION(is_string)
     270             : {
     271           2 :         php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_STRING);
     272           2 : }
     273             : /* }}} */
     274             : 
     275             : /* {{{ proto bool is_array(mixed var)
     276             :    Returns true if variable is an array
     277             :    Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
     278           2 : PHP_FUNCTION(is_array)
     279             : {
     280           2 :         php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_ARRAY);
     281           2 : }
     282             : /* }}} */
     283             : 
     284             : /* {{{ proto bool is_object(mixed var)
     285             :    Returns true if variable is an object
     286             :    Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
     287           2 : PHP_FUNCTION(is_object)
     288             : {
     289           2 :         php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_OBJECT);
     290           2 : }
     291             : /* }}} */
     292             : 
     293             : /* {{{ proto bool is_numeric(mixed value)
     294             :    Returns true if value is a number or a numeric string */
     295         130 : PHP_FUNCTION(is_numeric)
     296             : {
     297             :         zval *arg;
     298             : 
     299         130 :         ZEND_PARSE_PARAMETERS_START(1, 1)
     300         124 :                 Z_PARAM_ZVAL(arg)
     301         130 :         ZEND_PARSE_PARAMETERS_END();
     302             : 
     303         248 :         switch (Z_TYPE_P(arg)) {
     304             :                 case IS_LONG:
     305             :                 case IS_DOUBLE:
     306          62 :                         RETURN_TRUE;
     307             :                         break;
     308             : 
     309             :                 case IS_STRING:
     310          98 :                         if (is_numeric_string(Z_STRVAL_P(arg), Z_STRLEN_P(arg), NULL, NULL, 0)) {
     311          18 :                                 RETURN_TRUE;
     312             :                         } else {
     313          31 :                                 RETURN_FALSE;
     314             :                         }
     315             :                         break;
     316             : 
     317             :                 default:
     318          13 :                         RETURN_FALSE;
     319             :                         break;
     320             :         }
     321             : }
     322             : /* }}} */
     323             : 
     324             : /* {{{ proto bool is_scalar(mixed value)
     325             :    Returns true if value is a scalar */
     326          62 : PHP_FUNCTION(is_scalar)
     327             : {
     328             :         zval *arg;
     329             : 
     330          62 :         ZEND_PARSE_PARAMETERS_START(1, 1)
     331          59 :                 Z_PARAM_ZVAL(arg)
     332          62 :         ZEND_PARSE_PARAMETERS_END();
     333             : 
     334         118 :         switch (Z_TYPE_P(arg)) {
     335             :                 case IS_FALSE:
     336             :                 case IS_TRUE:
     337             :                 case IS_DOUBLE:
     338             :                 case IS_LONG:
     339             :                 case IS_STRING:
     340          37 :                         RETURN_TRUE;
     341             :                         break;
     342             : 
     343             :                 default:
     344          22 :                         RETURN_FALSE;
     345             :                         break;
     346             :         }
     347             : }
     348             : /* }}} */
     349             : 
     350             : /* {{{ proto bool is_callable(mixed var [, bool syntax_only [, string callable_name]])
     351             :    Returns true if var is callable. */
     352         644 : PHP_FUNCTION(is_callable)
     353             : {
     354         644 :         zval *var, *callable_name = NULL;
     355             :         zend_string *name;
     356             :         char *error;
     357             :         zend_bool retval;
     358         644 :         zend_bool syntax_only = 0;
     359         644 :         int check_flags = 0;
     360             : 
     361         644 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|bz/", &var,
     362             :                                                           &syntax_only, &callable_name) == FAILURE) {
     363           2 :                 return;
     364             :         }
     365             : 
     366         642 :         if (syntax_only) {
     367         233 :                 check_flags |= IS_CALLABLE_CHECK_SYNTAX_ONLY;
     368             :         }
     369         642 :         if (ZEND_NUM_ARGS() > 2) {
     370         236 :                 retval = zend_is_callable_ex(var, NULL, check_flags, &name, NULL, &error);
     371         236 :                 zval_dtor(callable_name);
     372             :                 //??? is it necessary to be consistent with old PHP ("\0" support)
     373         236 :                 if (UNEXPECTED(ZSTR_LEN(name) != strlen(ZSTR_VAL(name)))) {
     374           8 :                         ZVAL_STRINGL(callable_name, ZSTR_VAL(name), strlen(ZSTR_VAL(name)));
     375           4 :                         zend_string_release(name);
     376             :                 } else {
     377         232 :                         ZVAL_STR(callable_name, name);
     378             :                 }
     379             :         } else {
     380         406 :                 retval = zend_is_callable_ex(var, NULL, check_flags, NULL, NULL, &error);
     381             :         }
     382         642 :         if (error) {
     383             :                 /* ignore errors */
     384         377 :                 efree(error);
     385             :         }
     386             : 
     387         642 :         RETURN_BOOL(retval);
     388             : }
     389             : /* }}} */
     390             : 
     391             : /* {{{ proto bool is_iterable(mixed var)
     392             :    Returns true if var is iterable (array or instance of Traversable). */
     393           6 : PHP_FUNCTION(is_iterable)
     394             : {
     395             :         zval *var;
     396             :         
     397           6 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &var) == FAILURE) {
     398           0 :                 return;
     399             :         }
     400             :         
     401           6 :         RETURN_BOOL(zend_is_iterable(var));
     402             : }
     403             : /* }}} */
     404             : 
     405             : /*
     406             :  * Local variables:
     407             :  * tab-width: 4
     408             :  * c-basic-offset: 4
     409             :  * End:
     410             :  * vim600: sw=4 ts=4 fdm=marker
     411             :  * vim<600: sw=4 ts=4
     412             :  */

Generated by: LCOV version 1.10

Generated at Tue, 27 Sep 2016 10:26:08 +0000 (46 hours ago)

Copyright © 2005-2016 The PHP Group
All rights reserved.