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

Generated by: LCOV version 1.10

Generated at Sat, 29 Aug 2015 10:22:25 +0000 (3 days ago)

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