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: 2015-07-31 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        2642 : PHP_FUNCTION(gettype)
      27             : {
      28             :         zval *arg;
      29             : 
      30        2642 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &arg) == FAILURE) {
      31           2 :                 return;
      32             :         }
      33             : 
      34        5280 :         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        3280 :                         RETVAL_STRING("string");
      54        1640 :                         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       40009 : PHP_FUNCTION(settype)
      94             : {
      95             :         zval *var;
      96             :         char *type;
      97       40009 :         size_t type_len = 0;
      98             : 
      99       40009 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "zs", &var, &type, &type_len) == FAILURE) {
     100           2 :                 return;
     101             :         }
     102             : 
     103       80014 :         ZVAL_DEREF(var);
     104       40007 :         if (!strcasecmp(type, "integer")) {
     105          25 :                 convert_to_long(var);
     106       39982 :         } else if (!strcasecmp(type, "int")) {
     107          27 :                 convert_to_long(var);
     108       39955 :         } else if (!strcasecmp(type, "float")) {
     109          14 :                 convert_to_double(var);
     110       39941 :         } else if (!strcasecmp(type, "double")) { /* deprecated */
     111          38 :                 convert_to_double(var);
     112       39903 :         } else if (!strcasecmp(type, "string")) {
     113       79396 :                 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       39968 :         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        4504 : PHP_FUNCTION(intval)
     138             : {
     139             :         zval *num;
     140        4504 :         zend_long base = 10;
     141             : 
     142        4504 :         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        4504 :         ZEND_PARSE_PARAMETERS_START(1, 2)
     151        4504 :                 Z_PARAM_ZVAL(num)
     152        4504 :                 Z_PARAM_OPTIONAL
     153        4562 :                 Z_PARAM_LONG(base)
     154        4504 :         ZEND_PARSE_PARAMETERS_END();
     155             : #endif
     156             : 
     157        4504 :         ZVAL_DUP(return_value, num);
     158        4504 :         convert_to_long_base(return_value, (int)base);
     159             : }
     160             : /* }}} */
     161             : 
     162             : /* {{{ proto float floatval(mixed var)
     163             :    Get the float value of a variable */
     164         153 : PHP_FUNCTION(floatval)
     165             : {
     166             :         zval *num;
     167             : 
     168         153 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &num) == FAILURE) {
     169           8 :                 return;
     170             :         }
     171             : 
     172         290 :         RETURN_DOUBLE(zval_get_double(num));
     173             : }
     174             : /* }}} */
     175             : 
     176             : /* {{{ proto bool boolval(mixed var)
     177             :    Get the boolean value of a variable */
     178          11 : PHP_FUNCTION(boolval)
     179             : {
     180             :         zval *val;
     181             : 
     182          11 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &val) == FAILURE) {
     183           0 :                 return;
     184             :         }
     185             : 
     186          11 :         RETURN_BOOL(zend_is_true(val));
     187             : }
     188             : /* }}} */
     189             : 
     190             : /* {{{ proto string strval(mixed var)
     191             :    Get the string value of a variable */
     192      100729 : PHP_FUNCTION(strval)
     193             : {
     194             :         zval *num;
     195             : 
     196             : #ifndef FAST_ZPP
     197             :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &num) == FAILURE) {
     198             :                 return;
     199             :         }
     200             : #else
     201      100729 :         ZEND_PARSE_PARAMETERS_START(1, 1)
     202      100725 :                 Z_PARAM_ZVAL(num)
     203      100729 :         ZEND_PARSE_PARAMETERS_END();
     204             : #endif
     205             : 
     206      201449 :         RETVAL_STR(zval_get_string(num));
     207             : }
     208             : /* }}} */
     209             : 
     210          30 : static inline void php_is_type(INTERNAL_FUNCTION_PARAMETERS, int type)
     211             : {
     212             :         zval *arg;
     213             : 
     214             : #ifndef FAST_ZPP
     215             :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &arg) == FAILURE) {
     216             :                 RETURN_FALSE;
     217             :         }
     218             :         ZVAL_DEREF(arg);
     219             : #else
     220          30 :         ZEND_PARSE_PARAMETERS_START(1, 1)
     221          16 :                 Z_PARAM_ZVAL_DEREF(arg)
     222          30 :         ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
     223             : #endif
     224             : 
     225          16 :         if (Z_TYPE_P(arg) == type) {
     226           6 :                 if (type == IS_OBJECT) {
     227           0 :                         zend_class_entry *ce = Z_OBJCE_P(arg);
     228           0 :                         if (ce->name->len == sizeof(INCOMPLETE_CLASS) - 1
     229           0 :                                         && !memcmp(ce->name->val, INCOMPLETE_CLASS, sizeof(INCOMPLETE_CLASS) - 1)) {
     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));
     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(), "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_int(mixed var)
     280             :    Returns true if variable is an integer
     281             :    Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
     282          14 : PHP_FUNCTION(is_int)
     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         130 : PHP_FUNCTION(is_numeric)
     327             : {
     328             :         zval *arg;
     329             : 
     330             : #ifndef FAST_ZPP
     331             :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &arg) == FAILURE) {
     332             :                 return;
     333             :         }
     334             : #else
     335         130 :         ZEND_PARSE_PARAMETERS_START(1, 1)
     336         124 :                 Z_PARAM_ZVAL(arg)
     337         130 :         ZEND_PARSE_PARAMETERS_END();
     338             : #endif
     339             : 
     340         248 :         switch (Z_TYPE_P(arg)) {
     341             :                 case IS_LONG:
     342             :                 case IS_DOUBLE:
     343          62 :                         RETURN_TRUE;
     344             :                         break;
     345             : 
     346             :                 case IS_STRING:
     347          98 :                         if (is_numeric_string(Z_STRVAL_P(arg), Z_STRLEN_P(arg), NULL, NULL, 0)) {
     348          18 :                                 RETURN_TRUE;
     349             :                         } else {
     350          31 :                                 RETURN_FALSE;
     351             :                         }
     352             :                         break;
     353             : 
     354             :                 default:
     355          13 :                         RETURN_FALSE;
     356             :                         break;
     357             :         }
     358             : }
     359             : /* }}} */
     360             : 
     361             : /* {{{ proto bool is_scalar(mixed value)
     362             :    Returns true if value is a scalar */
     363          62 : PHP_FUNCTION(is_scalar)
     364             : {
     365             :         zval *arg;
     366             : 
     367             : #ifndef FAST_ZPP
     368             :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &arg) == FAILURE) {
     369             :                 return;
     370             :         }
     371             : #else
     372          62 :         ZEND_PARSE_PARAMETERS_START(1, 1)
     373          59 :                 Z_PARAM_ZVAL(arg)
     374          62 :         ZEND_PARSE_PARAMETERS_END();
     375             : #endif
     376             : 
     377         118 :         switch (Z_TYPE_P(arg)) {
     378             :                 case IS_FALSE:
     379             :                 case IS_TRUE:
     380             :                 case IS_DOUBLE:
     381             :                 case IS_LONG:
     382             :                 case IS_STRING:
     383          37 :                         RETURN_TRUE;
     384             :                         break;
     385             : 
     386             :                 default:
     387          22 :                         RETURN_FALSE;
     388             :                         break;
     389             :         }
     390             : }
     391             : /* }}} */
     392             : 
     393             : /* {{{ proto bool is_callable(mixed var [, bool syntax_only [, string callable_name]])
     394             :    Returns true if var is callable. */
     395         642 : PHP_FUNCTION(is_callable)
     396             : {
     397         642 :         zval *var, *callable_name = NULL;
     398             :         zend_string *name;
     399             :         char *error;
     400             :         zend_bool retval;
     401         642 :         zend_bool syntax_only = 0;
     402         642 :         int check_flags = 0;
     403             : 
     404         642 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|bz/", &var,
     405             :                                                           &syntax_only, &callable_name) == FAILURE) {
     406           2 :                 return;
     407             :         }
     408             : 
     409         640 :         if (syntax_only) {
     410         233 :                 check_flags |= IS_CALLABLE_CHECK_SYNTAX_ONLY;
     411             :         }
     412         640 :         if (ZEND_NUM_ARGS() > 2) {
     413         236 :                 retval = zend_is_callable_ex(var, NULL, check_flags, &name, NULL, &error);
     414         236 :                 zval_dtor(callable_name);
     415             :                 //??? is it necessary to be consistent with old PHP ("\0" support)
     416         236 :                 if (UNEXPECTED(name->len) != strlen(name->val)) {
     417         432 :                         ZVAL_STRINGL(callable_name, name->val, strlen(name->val));
     418         216 :                         zend_string_release(name);
     419             :                 } else {
     420          20 :                         ZVAL_STR(callable_name, name);
     421             :                 }
     422             :         } else {
     423         404 :                 retval = zend_is_callable_ex(var, NULL, check_flags, NULL, NULL, &error);
     424             :         }
     425         640 :         if (error) {
     426             :                 /* ignore errors */
     427         376 :                 efree(error);
     428             :         }
     429             : 
     430         640 :         RETURN_BOOL(retval);
     431             : }
     432             : /* }}} */
     433             : 
     434             : /*
     435             :  * Local variables:
     436             :  * tab-width: 4
     437             :  * c-basic-offset: 4
     438             :  * End:
     439             :  * vim600: sw=4 ts=4 fdm=marker
     440             :  * vim<600: sw=4 ts=4
     441             :  */

Generated by: LCOV version 1.10

Generated at Fri, 31 Jul 2015 08:58:59 +0000 (20 hours ago)

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