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 156 92.9 %
Date: 2014-10-30 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        2704 : PHP_FUNCTION(gettype)
      27             : {
      28             :         zval *arg;
      29             : 
      30        2704 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &arg) == FAILURE) {
      31           2 :                 return;
      32             :         }
      33             : 
      34        5404 :         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        3352 :                         RETVAL_STRING("string");
      54        1676 :                         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       38912 : PHP_FUNCTION(settype)
      94             : {
      95             :         zval *var;
      96             :         char *type;
      97       38912 :         size_t type_len = 0;
      98             : 
      99       38912 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zs", &var, &type, &type_len) == FAILURE) {
     100           2 :                 return;
     101             :         }
     102             : 
     103       77820 :         ZVAL_DEREF(var);
     104      116144 :         SEPARATE_ZVAL_NOREF(var);
     105       38910 :         if (!strcasecmp(type, "integer")) {
     106          25 :                 convert_to_long(var);
     107       38885 :         } else if (!strcasecmp(type, "int")) {
     108          27 :                 convert_to_long(var);
     109       38858 :         } else if (!strcasecmp(type, "float")) {
     110          14 :                 convert_to_double(var);
     111       38844 :         } else if (!strcasecmp(type, "double")) { /* deprecated */
     112          38 :                 convert_to_double(var);
     113       38806 :         } else if (!strcasecmp(type, "string")) {
     114       77202 :                 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       38871 :         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, (int)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           0 :                         zend_class_entry *ce = Z_OBJCE_P(arg);
     223           0 :                         if (ce->name->len == sizeof(INCOMPLETE_CLASS) - 1 
     224           0 :                                         && !strncmp(ce->name->val, INCOMPLETE_CLASS, ce->name->len)) {
     225           0 :                                 RETURN_FALSE;
     226             :                         }
     227           6 :                 } else if (type == IS_RESOURCE) {
     228           0 :                         const char *type_name = zend_rsrc_list_get_rsrc_type(Z_RES_P(arg) TSRMLS_CC);
     229           0 :                         if (!type_name) {
     230           0 :                                 RETURN_FALSE;
     231             :                         }
     232             :                 }
     233           6 :                 RETURN_TRUE;
     234             :         } else {
     235           2 :                 RETURN_FALSE;
     236             :         }
     237             : }
     238             : 
     239             : 
     240             : /* {{{ proto bool is_null(mixed var)
     241             :    Returns true if variable is null
     242             :    Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
     243           2 : PHP_FUNCTION(is_null)
     244             : {
     245           2 :         php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_NULL);
     246           2 : }
     247             : /* }}} */
     248             : 
     249             : /* {{{ proto bool is_resource(mixed var)
     250             :    Returns true if variable is a resource
     251             :    Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
     252           2 : PHP_FUNCTION(is_resource)
     253             : {
     254           2 :         php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_RESOURCE);
     255           2 : }
     256             : /* }}} */
     257             : 
     258             : /* {{{ proto bool is_bool(mixed var)
     259             :    Returns true if variable is a boolean
     260             :    Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
     261           2 : PHP_FUNCTION(is_bool)
     262             : {
     263             :         zval *arg;
     264             : 
     265           2 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &arg) == FAILURE) {
     266           2 :                 RETURN_FALSE;
     267             :         }
     268             : 
     269           0 :         ZVAL_DEREF(arg);
     270           0 :         RETURN_BOOL(Z_TYPE_P(arg) == IS_FALSE || Z_TYPE_P(arg) == IS_TRUE);
     271             : }
     272             : /* }}} */
     273             : 
     274             : /* {{{ proto bool is_long(mixed var)
     275             :    Returns true if variable is a long (integer)
     276             :    Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
     277          14 : PHP_FUNCTION(is_long)
     278             : {
     279          14 :         php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_LONG);
     280          14 : }
     281             : /* }}} */
     282             : 
     283             : /* {{{ proto bool is_float(mixed var)
     284             :    Returns true if variable is float point
     285             :    Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
     286           6 : PHP_FUNCTION(is_float)
     287             : {
     288           6 :         php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_DOUBLE);
     289           6 : }
     290             : /* }}} */
     291             : 
     292             : /* {{{ proto bool is_string(mixed var)
     293             :    Returns true if variable is a string
     294             :    Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
     295           2 : PHP_FUNCTION(is_string)
     296             : {
     297           2 :         php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_STRING);
     298           2 : }
     299             : /* }}} */
     300             : 
     301             : /* {{{ proto bool is_array(mixed var)
     302             :    Returns true if variable is an array
     303             :    Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
     304           2 : PHP_FUNCTION(is_array)
     305             : {
     306           2 :         php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_ARRAY);
     307           2 : }
     308             : /* }}} */
     309             : 
     310             : /* {{{ proto bool is_object(mixed var)
     311             :    Returns true if variable is an object
     312             :    Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
     313           2 : PHP_FUNCTION(is_object)
     314             : {
     315           2 :         php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_OBJECT);
     316           2 : }
     317             : /* }}} */
     318             : 
     319             : /* {{{ proto bool is_numeric(mixed value)
     320             :    Returns true if value is a number or a numeric string */
     321         141 : PHP_FUNCTION(is_numeric)
     322             : {
     323             :         zval *arg;
     324             : 
     325         141 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &arg) == FAILURE) {
     326           6 :                 return;
     327             :         }
     328             : 
     329         270 :         switch (Z_TYPE_P(arg)) {
     330             :                 case IS_LONG:
     331             :                 case IS_DOUBLE:
     332          72 :                         RETURN_TRUE;
     333             :                         break;
     334             : 
     335             :                 case IS_STRING:
     336         100 :                         if (is_numeric_string(Z_STRVAL_P(arg), Z_STRLEN_P(arg), NULL, NULL, 0)) {
     337          20 :                                 RETURN_TRUE;
     338             :                         } else {
     339          30 :                                 RETURN_FALSE;
     340             :                         }
     341             :                         break;
     342             : 
     343             :                 default:
     344          13 :                         RETURN_FALSE;
     345             :                         break;
     346             :         }
     347             : }
     348             : /* }}} */
     349             : 
     350             : /* {{{ proto bool is_scalar(mixed value)
     351             :    Returns true if value is a scalar */
     352          62 : PHP_FUNCTION(is_scalar)
     353             : {
     354             :         zval *arg;
     355             : 
     356             : #ifndef FAST_ZPP
     357             :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &arg) == FAILURE) {
     358             :                 return;
     359             :         }
     360             : #else
     361          62 :         ZEND_PARSE_PARAMETERS_START(1, 1)
     362          59 :                 Z_PARAM_ZVAL(arg)
     363          62 :         ZEND_PARSE_PARAMETERS_END();
     364             : #endif
     365             : 
     366         118 :         switch (Z_TYPE_P(arg)) {
     367             :                 case IS_FALSE:
     368             :                 case IS_TRUE:
     369             :                 case IS_DOUBLE:
     370             :                 case IS_LONG:
     371             :                 case IS_STRING:
     372          37 :                         RETURN_TRUE;
     373             :                         break;
     374             : 
     375             :                 default:
     376          22 :                         RETURN_FALSE;
     377             :                         break;
     378             :         }
     379             : }
     380             : /* }}} */
     381             : 
     382             : /* {{{ proto bool is_callable(mixed var [, bool syntax_only [, string callable_name]]) 
     383             :    Returns true if var is callable. */
     384         641 : PHP_FUNCTION(is_callable)
     385             : {
     386         641 :         zval *var, *callable_name = NULL;
     387             :         zend_string *name;
     388             :         char *error;
     389             :         zend_bool retval;
     390         641 :         zend_bool syntax_only = 0;
     391         641 :         int check_flags = 0;
     392             : 
     393         641 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|bz/", &var,
     394             :                                                           &syntax_only, &callable_name) == FAILURE) {
     395           2 :                 return;
     396             :         }
     397             :         
     398         639 :         if (syntax_only) {
     399         233 :                 check_flags |= IS_CALLABLE_CHECK_SYNTAX_ONLY;
     400             :         }
     401         639 :         if (ZEND_NUM_ARGS() > 2) {
     402         236 :                 retval = zend_is_callable_ex(var, NULL, check_flags, &name, NULL, &error TSRMLS_CC);
     403         236 :                 zval_dtor(callable_name);
     404             :                 //??? is it necessary to be consistent with old PHP ("\0" support)
     405         236 :                 if (UNEXPECTED(name->len) != strlen(name->val)) {
     406         432 :                         ZVAL_STRINGL(callable_name, name->val, strlen(name->val));
     407         216 :                         zend_string_release(name);
     408             :                 } else {
     409          20 :                         ZVAL_STR(callable_name, name);
     410             :                 }
     411             :         } else {
     412         403 :                 retval = zend_is_callable_ex(var, NULL, check_flags, NULL, NULL, &error TSRMLS_CC);
     413             :         }
     414         639 :         if (error) {
     415             :                 /* ignore errors */
     416         376 :                 efree(error);
     417             :         }
     418             : 
     419         639 :         RETURN_BOOL(retval);
     420             : }
     421             : /* }}} */
     422             : 
     423             : /*
     424             :  * Local variables:
     425             :  * tab-width: 4
     426             :  * c-basic-offset: 4
     427             :  * End:
     428             :  * vim600: sw=4 ts=4 fdm=marker
     429             :  * vim<600: sw=4 ts=4
     430             :  */

Generated by: LCOV version 1.10

Generated at Thu, 30 Oct 2014 07:41:41 +0000 (37 hours ago)

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