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: 145 158 91.8 %
Date: 2014-09-27 Functions: 18 18 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 7                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1997-2014 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        2703 : PHP_FUNCTION(gettype)
      27             : {
      28             :         zval *arg;
      29             : 
      30        2703 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &arg) == FAILURE) {
      31           2 :                 return;
      32             :         }
      33             : 
      34        5402 :         switch (Z_TYPE_P(arg)) {
      35             :                 case IS_NULL:
      36         920 :                         RETVAL_STRING("NULL");
      37         460 :                         break;
      38             : 
      39             :                 case IS_FALSE:
      40             :                 case IS_TRUE:
      41         180 :                         RETVAL_STRING("boolean");
      42          90 :                         break;
      43             : 
      44             :                 case IS_LONG:
      45         562 :                         RETVAL_STRING("integer");
      46         281 :                         break;
      47             : 
      48             :                 case IS_DOUBLE:
      49         124 :                         RETVAL_STRING("double");
      50          62 :                         break;
      51             :         
      52             :                 case IS_STRING:
      53        3350 :                         RETVAL_STRING("string");
      54        1675 :                         break;
      55             :         
      56             :                 case IS_ARRAY:
      57         204 :                         RETVAL_STRING("array");
      58         102 :                         break;
      59             : 
      60             :                 case IS_OBJECT:
      61          50 :                         RETVAL_STRING("object");
      62             :                 /*
      63             :                    {
      64             :                    char *result;
      65             :                    int res_len;
      66             : 
      67             :                    res_len = sizeof("object of type ")-1 + Z_OBJCE_P(arg)->name_length;
      68             :                    spprintf(&result, 0, "object of type %s", Z_OBJCE_P(arg)->name);
      69             :                    RETVAL_STRINGL(result, res_len);
      70             :                    efree(result);
      71             :                    }
      72             :                  */
      73          25 :                         break;
      74             : 
      75             :                 case IS_RESOURCE:
      76             :                         {
      77           6 :                                 const char *type_name = zend_rsrc_list_get_rsrc_type(Z_RES_P(arg) TSRMLS_CC);
      78             : 
      79           6 :                                 if (type_name) {
      80          10 :                                         RETVAL_STRING("resource");
      81           5 :                                         break;
      82             :                                 }
      83             :                         }
      84             : 
      85             :                 default:
      86           2 :                         RETVAL_STRING("unknown type");
      87             :         }
      88             : }
      89             : /* }}} */
      90             : 
      91             : /* {{{ proto bool settype(mixed var, string type)
      92             :    Set the type of the variable */
      93       38778 : PHP_FUNCTION(settype)
      94             : {
      95             :         zval *var;
      96             :         char *type;
      97       38778 :         size_t type_len = 0;
      98             : 
      99       38778 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zs", &var, &type, &type_len) == FAILURE) {
     100           2 :                 return;
     101             :         }
     102             : 
     103       77552 :         ZVAL_DEREF(var);
     104      115742 :         SEPARATE_ZVAL_NOREF(var);
     105       38776 :         if (!strcasecmp(type, "integer")) {
     106          25 :                 convert_to_long(var);
     107       38751 :         } else if (!strcasecmp(type, "int")) {
     108          27 :                 convert_to_long(var);
     109       38724 :         } else if (!strcasecmp(type, "float")) {
     110          14 :                 convert_to_double(var);
     111       38710 :         } else if (!strcasecmp(type, "double")) { /* deprecated */
     112          38 :                 convert_to_double(var);
     113       38672 :         } else if (!strcasecmp(type, "string")) {
     114       76934 :                 convert_to_string(var);
     115         205 :         } else if (!strcasecmp(type, "array")) {
     116          38 :                 convert_to_array(var);
     117         167 :         } else if (!strcasecmp(type, "object")) {
     118          38 :                 convert_to_object(var);
     119         129 :         } else if (!strcasecmp(type, "bool")) {
     120          27 :                 convert_to_boolean(var);
     121         102 :         } else if (!strcasecmp(type, "boolean")) {
     122          25 :                 convert_to_boolean(var);
     123          77 :         } else if (!strcasecmp(type, "null")) {
     124          38 :                 convert_to_null(var);
     125          39 :         } else if (!strcasecmp(type, "resource")) {
     126          38 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot convert to resource type");
     127          38 :                 RETURN_FALSE;
     128             :         } else {
     129           1 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid type");
     130           1 :                 RETURN_FALSE;
     131             :         }
     132       38737 :         RETVAL_TRUE;
     133             : }
     134             : /* }}} */
     135             : 
     136             : /* {{{ proto int intval(mixed var [, int base])
     137             :    Get the integer value of a variable using the optional base for the conversion */
     138        3729 : PHP_FUNCTION(intval)
     139             : {
     140             :         zval *num;
     141        3729 :         zend_long base = 10;
     142             : 
     143        3729 :         if (ZEND_NUM_ARGS() != 1 && ZEND_NUM_ARGS() != 2) {
     144           0 :                 WRONG_PARAM_COUNT;
     145             :         }
     146             : #ifndef FAST_ZPP
     147             :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|l", &num, &base) == FAILURE) {
     148             :                 return;
     149             :         }
     150             : #else
     151        3729 :         ZEND_PARSE_PARAMETERS_START(1, 2)
     152        3729 :                 Z_PARAM_ZVAL(num)
     153        3729 :                 Z_PARAM_OPTIONAL
     154        3837 :                 Z_PARAM_LONG(base)
     155        3729 :         ZEND_PARSE_PARAMETERS_END();
     156             : #endif
     157             : 
     158        7434 :         RETVAL_ZVAL(num, 1, 0);
     159        3717 :         convert_to_long_base(return_value, base);
     160             : }
     161             : /* }}} */
     162             : 
     163             : /* {{{ proto float floatval(mixed var)
     164             :    Get the float value of a variable */
     165         153 : PHP_FUNCTION(floatval)
     166             : {
     167             :         zval *num;
     168             : 
     169         153 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &num) == FAILURE) {
     170           8 :                 return;
     171             :         }
     172             : 
     173         290 :         RETVAL_ZVAL(num, 1, 0);
     174         145 :         convert_to_double(return_value);
     175             : }
     176             : /* }}} */
     177             : 
     178             : /* {{{ proto bool boolval(mixed var)
     179             :    Get the boolean value of a variable */
     180          11 : PHP_FUNCTION(boolval)
     181             : {
     182             :         zval *val;
     183             : 
     184          11 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &val) == FAILURE) {
     185           0 :                 return;
     186             :         }
     187             : 
     188          11 :         RETURN_BOOL(zend_is_true(val TSRMLS_CC));
     189             : }
     190             : /* }}} */
     191             : 
     192             : /* {{{ proto string strval(mixed var)
     193             :    Get the string value of a variable */
     194      100729 : PHP_FUNCTION(strval)
     195             : {
     196             :         zval *num;
     197      100729 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &num) == FAILURE) {
     198           4 :                 return;
     199             :         }
     200             : 
     201      201449 :         RETVAL_STR(zval_get_string(num));
     202             : }
     203             : /* }}} */
     204             : 
     205          30 : static inline void php_is_type(INTERNAL_FUNCTION_PARAMETERS, int type)
     206             : {
     207             :         zval *arg;
     208             : 
     209             : #ifndef FAST_ZPP
     210             :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &arg) == FAILURE) {
     211             :                 RETURN_FALSE;
     212             :         }
     213             :         ZVAL_DEREF(arg);
     214             : #else
     215          30 :         ZEND_PARSE_PARAMETERS_START(1, 1)
     216          16 :                 Z_PARAM_ZVAL_DEREF(arg)
     217          30 :         ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
     218             : #endif
     219             : 
     220          16 :         if (Z_TYPE_P(arg) == type) {
     221           6 :                 if (type == IS_OBJECT) {
     222             :                         zend_class_entry *ce;
     223           0 :                         if (Z_OBJ_HT_P(arg)->get_class_entry == NULL) {
     224             :                                 /* if there's no get_class_entry it's not a PHP object, so it can't be INCOMPLETE_CLASS */
     225           0 :                                 RETURN_TRUE;
     226             :                         }
     227           0 :                         ce = Z_OBJCE_P(arg);
     228           0 :                         if (ce->name->len == sizeof(INCOMPLETE_CLASS) - 1 
     229           0 :                                         && !strncmp(ce->name->val, INCOMPLETE_CLASS, ce->name->len)) {
     230           0 :                                 RETURN_FALSE;
     231             :                         }
     232           6 :                 } else if (type == IS_RESOURCE) {
     233           0 :                         const char *type_name = zend_rsrc_list_get_rsrc_type(Z_RES_P(arg) TSRMLS_CC);
     234           0 :                         if (!type_name) {
     235           0 :                                 RETURN_FALSE;
     236             :                         }
     237             :                 }
     238           6 :                 RETURN_TRUE;
     239             :         } else {
     240           2 :                 RETURN_FALSE;
     241             :         }
     242             : }
     243             : 
     244             : 
     245             : /* {{{ proto bool is_null(mixed var)
     246             :    Returns true if variable is null
     247             :    Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
     248           2 : PHP_FUNCTION(is_null)
     249             : {
     250           2 :         php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_NULL);
     251           2 : }
     252             : /* }}} */
     253             : 
     254             : /* {{{ proto bool is_resource(mixed var)
     255             :    Returns true if variable is a resource
     256             :    Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
     257           2 : PHP_FUNCTION(is_resource)
     258             : {
     259           2 :         php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_RESOURCE);
     260           2 : }
     261             : /* }}} */
     262             : 
     263             : /* {{{ proto bool is_bool(mixed var)
     264             :    Returns true if variable is a boolean
     265             :    Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
     266           2 : PHP_FUNCTION(is_bool)
     267             : {
     268             :         zval *arg;
     269             : 
     270           2 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &arg) == FAILURE) {
     271           2 :                 RETURN_FALSE;
     272             :         }
     273             : 
     274           0 :         ZVAL_DEREF(arg);
     275           0 :         RETURN_BOOL(Z_TYPE_P(arg) == IS_FALSE || Z_TYPE_P(arg) == IS_TRUE);
     276             : }
     277             : /* }}} */
     278             : 
     279             : /* {{{ proto bool is_long(mixed var)
     280             :    Returns true if variable is a long (integer)
     281             :    Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
     282          14 : PHP_FUNCTION(is_long)
     283             : {
     284          14 :         php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_LONG);
     285          14 : }
     286             : /* }}} */
     287             : 
     288             : /* {{{ proto bool is_float(mixed var)
     289             :    Returns true if variable is float point
     290             :    Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
     291           6 : PHP_FUNCTION(is_float)
     292             : {
     293           6 :         php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_DOUBLE);
     294           6 : }
     295             : /* }}} */
     296             : 
     297             : /* {{{ proto bool is_string(mixed var)
     298             :    Returns true if variable is a string
     299             :    Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
     300           2 : PHP_FUNCTION(is_string)
     301             : {
     302           2 :         php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_STRING);
     303           2 : }
     304             : /* }}} */
     305             : 
     306             : /* {{{ proto bool is_array(mixed var)
     307             :    Returns true if variable is an array
     308             :    Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
     309           2 : PHP_FUNCTION(is_array)
     310             : {
     311           2 :         php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_ARRAY);
     312           2 : }
     313             : /* }}} */
     314             : 
     315             : /* {{{ proto bool is_object(mixed var)
     316             :    Returns true if variable is an object
     317             :    Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
     318           2 : PHP_FUNCTION(is_object)
     319             : {
     320           2 :         php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_OBJECT);
     321           2 : }
     322             : /* }}} */
     323             : 
     324             : /* {{{ proto bool is_numeric(mixed value)
     325             :    Returns true if value is a number or a numeric string */
     326         121 : PHP_FUNCTION(is_numeric)
     327             : {
     328             :         zval *arg;
     329             : 
     330         121 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &arg) == FAILURE) {
     331           6 :                 return;
     332             :         }
     333             : 
     334         230 :         switch (Z_TYPE_P(arg)) {
     335             :                 case IS_LONG:
     336             :                 case IS_DOUBLE:
     337          62 :                         RETURN_TRUE;
     338             :                         break;
     339             : 
     340             :                 case IS_STRING:
     341          80 :                         if (is_numeric_string(Z_STRVAL_P(arg), Z_STRLEN_P(arg), NULL, NULL, 0)) {
     342          20 :                                 RETURN_TRUE;
     343             :                         } else {
     344          20 :                                 RETURN_FALSE;
     345             :                         }
     346             :                         break;
     347             : 
     348             :                 default:
     349          13 :                         RETURN_FALSE;
     350             :                         break;
     351             :         }
     352             : }
     353             : /* }}} */
     354             : 
     355             : /* {{{ proto bool is_scalar(mixed value)
     356             :    Returns true if value is a scalar */
     357          62 : PHP_FUNCTION(is_scalar)
     358             : {
     359             :         zval *arg;
     360             : 
     361             : #ifndef FAST_ZPP
     362             :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &arg) == FAILURE) {
     363             :                 return;
     364             :         }
     365             : #else
     366          62 :         ZEND_PARSE_PARAMETERS_START(1, 1)
     367          59 :                 Z_PARAM_ZVAL(arg)
     368          62 :         ZEND_PARSE_PARAMETERS_END();
     369             : #endif
     370             : 
     371         118 :         switch (Z_TYPE_P(arg)) {
     372             :                 case IS_FALSE:
     373             :                 case IS_TRUE:
     374             :                 case IS_DOUBLE:
     375             :                 case IS_LONG:
     376             :                 case IS_STRING:
     377          37 :                         RETURN_TRUE;
     378             :                         break;
     379             : 
     380             :                 default:
     381          22 :                         RETURN_FALSE;
     382             :                         break;
     383             :         }
     384             : }
     385             : /* }}} */
     386             : 
     387             : /* {{{ proto bool is_callable(mixed var [, bool syntax_only [, string callable_name]]) 
     388             :    Returns true if var is callable. */
     389         641 : PHP_FUNCTION(is_callable)
     390             : {
     391         641 :         zval *var, *callable_name = NULL;
     392             :         zend_string *name;
     393             :         char *error;
     394             :         zend_bool retval;
     395         641 :         zend_bool syntax_only = 0;
     396         641 :         int check_flags = 0;
     397             : 
     398         641 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|bz/", &var,
     399             :                                                           &syntax_only, &callable_name) == FAILURE) {
     400           2 :                 return;
     401             :         }
     402             :         
     403         639 :         if (syntax_only) {
     404         233 :                 check_flags |= IS_CALLABLE_CHECK_SYNTAX_ONLY;
     405             :         }
     406         639 :         if (ZEND_NUM_ARGS() > 2) {
     407         236 :                 retval = zend_is_callable_ex(var, NULL, check_flags, &name, NULL, &error TSRMLS_CC);
     408         236 :                 zval_dtor(callable_name);
     409             :                 //??? is it necessary to be consistent with old PHP ("\0" support)
     410         236 :                 if (UNEXPECTED(name->len) != strlen(name->val)) {
     411         432 :                         ZVAL_STRINGL(callable_name, name->val, strlen(name->val));
     412         216 :                         zend_string_release(name);
     413             :                 } else {
     414          20 :                         ZVAL_STR(callable_name, name);
     415             :                 }
     416             :         } else {
     417         403 :                 retval = zend_is_callable_ex(var, NULL, check_flags, NULL, NULL, &error TSRMLS_CC);
     418             :         }
     419         639 :         if (error) {
     420             :                 /* ignore errors */
     421         376 :                 efree(error);
     422             :         }
     423             : 
     424         639 :         RETURN_BOOL(retval);
     425             : }
     426             : /* }}} */
     427             : 
     428             : /*
     429             :  * Local variables:
     430             :  * tab-width: 4
     431             :  * c-basic-offset: 4
     432             :  * End:
     433             :  * vim600: sw=4 ts=4 fdm=marker
     434             :  * vim<600: sw=4 ts=4
     435             :  */

Generated by: LCOV version 1.10

Generated at Sat, 27 Sep 2014 16:43:19 +0000 (4 days ago)

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