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: 147 158 93.0 %
Date: 2015-02-21 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        2686 : PHP_FUNCTION(gettype)
      27             : {
      28             :         zval *arg;
      29             : 
      30        2686 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &arg) == FAILURE) {
      31           2 :                 return;
      32             :         }
      33             : 
      34        5368 :         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         160 :                         RETVAL_STRING("boolean");
      42          80 :                         break;
      43             : 
      44             :                 case IS_LONG:
      45         560 :                         RETVAL_STRING("integer");
      46         280 :                         break;
      47             : 
      48             :                 case IS_DOUBLE:
      49         124 :                         RETVAL_STRING("double");
      50          62 :                         break;
      51             : 
      52             :                 case IS_STRING:
      53        3338 :                         RETVAL_STRING("string");
      54        1669 :                         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));
      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       39609 : PHP_FUNCTION(settype)
      94             : {
      95             :         zval *var;
      96             :         char *type;
      97       39609 :         size_t type_len = 0;
      98             : 
      99       39609 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "zs", &var, &type, &type_len) == FAILURE) {
     100           2 :                 return;
     101             :         }
     102             : 
     103       79214 :         ZVAL_DEREF(var);
     104      118235 :         SEPARATE_ZVAL_NOREF(var);
     105       39607 :         if (!strcasecmp(type, "integer")) {
     106          25 :                 convert_to_long(var);
     107       39582 :         } else if (!strcasecmp(type, "int")) {
     108          27 :                 convert_to_long(var);
     109       39555 :         } else if (!strcasecmp(type, "float")) {
     110          14 :                 convert_to_double(var);
     111       39541 :         } else if (!strcasecmp(type, "double")) { /* deprecated */
     112          38 :                 convert_to_double(var);
     113       39503 :         } else if (!strcasecmp(type, "string")) {
     114       78596 :                 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, E_WARNING, "Cannot convert to resource type");
     127          38 :                 RETURN_FALSE;
     128             :         } else {
     129           1 :                 php_error_docref(NULL, E_WARNING, "Invalid type");
     130           1 :                 RETURN_FALSE;
     131             :         }
     132       39568 :         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        3503 : PHP_FUNCTION(intval)
     139             : {
     140             :         zval *num;
     141        3503 :         zend_long base = 10;
     142             : 
     143        3503 :         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(), "z|l", &num, &base) == FAILURE) {
     148             :                 return;
     149             :         }
     150             : #else
     151        3503 :         ZEND_PARSE_PARAMETERS_START(1, 2)
     152        3503 :                 Z_PARAM_ZVAL(num)
     153        3503 :                 Z_PARAM_OPTIONAL
     154        3561 :                 Z_PARAM_LONG(base)
     155        3503 :         ZEND_PARSE_PARAMETERS_END();
     156             : #endif
     157             : 
     158        7006 :         RETVAL_ZVAL(num, 1, 0);
     159        3503 :         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(), "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(), "z", &val) == FAILURE) {
     185           0 :                 return;
     186             :         }
     187             : 
     188          11 :         RETURN_BOOL(zend_is_true(val));
     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             : 
     198             : #ifndef FAST_ZPP
     199             :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &num) == FAILURE) {
     200             :                 return;
     201             :         }
     202             : #else
     203      100729 :         ZEND_PARSE_PARAMETERS_START(1, 1)
     204      100725 :                 Z_PARAM_ZVAL(num)
     205      100729 :         ZEND_PARSE_PARAMETERS_END();
     206             : #endif
     207             : 
     208      201449 :         RETVAL_STR(zval_get_string(num));
     209             : }
     210             : /* }}} */
     211             : 
     212          30 : static inline void php_is_type(INTERNAL_FUNCTION_PARAMETERS, int type)
     213             : {
     214             :         zval *arg;
     215             : 
     216             : #ifndef FAST_ZPP
     217             :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &arg) == FAILURE) {
     218             :                 RETURN_FALSE;
     219             :         }
     220             :         ZVAL_DEREF(arg);
     221             : #else
     222          30 :         ZEND_PARSE_PARAMETERS_START(1, 1)
     223          16 :                 Z_PARAM_ZVAL_DEREF(arg)
     224          30 :         ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
     225             : #endif
     226             : 
     227          16 :         if (Z_TYPE_P(arg) == type) {
     228           6 :                 if (type == IS_OBJECT) {
     229           0 :                         zend_class_entry *ce = Z_OBJCE_P(arg);
     230           0 :                         if (ce->name->len == sizeof(INCOMPLETE_CLASS) - 1
     231           0 :                                         && !strncmp(ce->name->val, INCOMPLETE_CLASS, ce->name->len)) {
     232           0 :                                 RETURN_FALSE;
     233             :                         }
     234           6 :                 } else if (type == IS_RESOURCE) {
     235           0 :                         const char *type_name = zend_rsrc_list_get_rsrc_type(Z_RES_P(arg));
     236           0 :                         if (!type_name) {
     237           0 :                                 RETURN_FALSE;
     238             :                         }
     239             :                 }
     240           6 :                 RETURN_TRUE;
     241             :         } else {
     242           2 :                 RETURN_FALSE;
     243             :         }
     244             : }
     245             : 
     246             : 
     247             : /* {{{ proto bool is_null(mixed var)
     248             :    Returns true if variable is null
     249             :    Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
     250           2 : PHP_FUNCTION(is_null)
     251             : {
     252           2 :         php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_NULL);
     253           2 : }
     254             : /* }}} */
     255             : 
     256             : /* {{{ proto bool is_resource(mixed var)
     257             :    Returns true if variable is a resource
     258             :    Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
     259           2 : PHP_FUNCTION(is_resource)
     260             : {
     261           2 :         php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_RESOURCE);
     262           2 : }
     263             : /* }}} */
     264             : 
     265             : /* {{{ proto bool is_bool(mixed var)
     266             :    Returns true if variable is a boolean
     267             :    Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
     268           2 : PHP_FUNCTION(is_bool)
     269             : {
     270             :         zval *arg;
     271             : 
     272           2 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &arg) == FAILURE) {
     273           2 :                 RETURN_FALSE;
     274             :         }
     275             : 
     276           0 :         ZVAL_DEREF(arg);
     277           0 :         RETURN_BOOL(Z_TYPE_P(arg) == IS_FALSE || Z_TYPE_P(arg) == IS_TRUE);
     278             : }
     279             : /* }}} */
     280             : 
     281             : /* {{{ proto bool is_int(mixed var)
     282             :    Returns true if variable is an integer
     283             :    Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
     284          14 : PHP_FUNCTION(is_int)
     285             : {
     286          14 :         php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_LONG);
     287          14 : }
     288             : /* }}} */
     289             : 
     290             : /* {{{ proto bool is_float(mixed var)
     291             :    Returns true if variable is float point
     292             :    Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
     293           6 : PHP_FUNCTION(is_float)
     294             : {
     295           6 :         php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_DOUBLE);
     296           6 : }
     297             : /* }}} */
     298             : 
     299             : /* {{{ proto bool is_string(mixed var)
     300             :    Returns true if variable is a string
     301             :    Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
     302           2 : PHP_FUNCTION(is_string)
     303             : {
     304           2 :         php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_STRING);
     305           2 : }
     306             : /* }}} */
     307             : 
     308             : /* {{{ proto bool is_array(mixed var)
     309             :    Returns true if variable is an array
     310             :    Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
     311           2 : PHP_FUNCTION(is_array)
     312             : {
     313           2 :         php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_ARRAY);
     314           2 : }
     315             : /* }}} */
     316             : 
     317             : /* {{{ proto bool is_object(mixed var)
     318             :    Returns true if variable is an object
     319             :    Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
     320           2 : PHP_FUNCTION(is_object)
     321             : {
     322           2 :         php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_OBJECT);
     323           2 : }
     324             : /* }}} */
     325             : 
     326             : /* {{{ proto bool is_numeric(mixed value)
     327             :    Returns true if value is a number or a numeric string */
     328         130 : PHP_FUNCTION(is_numeric)
     329             : {
     330             :         zval *arg;
     331             : 
     332             : #ifndef FAST_ZPP
     333             :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &arg) == FAILURE) {
     334             :                 return;
     335             :         }
     336             : #else
     337         130 :         ZEND_PARSE_PARAMETERS_START(1, 1)
     338         124 :                 Z_PARAM_ZVAL(arg)
     339         130 :         ZEND_PARSE_PARAMETERS_END();
     340             : #endif
     341             : 
     342         248 :         switch (Z_TYPE_P(arg)) {
     343             :                 case IS_LONG:
     344             :                 case IS_DOUBLE:
     345          62 :                         RETURN_TRUE;
     346             :                         break;
     347             : 
     348             :                 case IS_STRING:
     349          98 :                         if (is_numeric_string(Z_STRVAL_P(arg), Z_STRLEN_P(arg), NULL, NULL, 0)) {
     350          18 :                                 RETURN_TRUE;
     351             :                         } else {
     352          31 :                                 RETURN_FALSE;
     353             :                         }
     354             :                         break;
     355             : 
     356             :                 default:
     357          13 :                         RETURN_FALSE;
     358             :                         break;
     359             :         }
     360             : }
     361             : /* }}} */
     362             : 
     363             : /* {{{ proto bool is_scalar(mixed value)
     364             :    Returns true if value is a scalar */
     365          62 : PHP_FUNCTION(is_scalar)
     366             : {
     367             :         zval *arg;
     368             : 
     369             : #ifndef FAST_ZPP
     370             :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &arg) == FAILURE) {
     371             :                 return;
     372             :         }
     373             : #else
     374          62 :         ZEND_PARSE_PARAMETERS_START(1, 1)
     375          59 :                 Z_PARAM_ZVAL(arg)
     376          62 :         ZEND_PARSE_PARAMETERS_END();
     377             : #endif
     378             : 
     379         118 :         switch (Z_TYPE_P(arg)) {
     380             :                 case IS_FALSE:
     381             :                 case IS_TRUE:
     382             :                 case IS_DOUBLE:
     383             :                 case IS_LONG:
     384             :                 case IS_STRING:
     385          37 :                         RETURN_TRUE;
     386             :                         break;
     387             : 
     388             :                 default:
     389          22 :                         RETURN_FALSE;
     390             :                         break;
     391             :         }
     392             : }
     393             : /* }}} */
     394             : 
     395             : /* {{{ proto bool is_callable(mixed var [, bool syntax_only [, string callable_name]])
     396             :    Returns true if var is callable. */
     397         641 : PHP_FUNCTION(is_callable)
     398             : {
     399         641 :         zval *var, *callable_name = NULL;
     400             :         zend_string *name;
     401             :         char *error;
     402             :         zend_bool retval;
     403         641 :         zend_bool syntax_only = 0;
     404         641 :         int check_flags = 0;
     405             : 
     406         641 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|bz/", &var,
     407             :                                                           &syntax_only, &callable_name) == FAILURE) {
     408           2 :                 return;
     409             :         }
     410             : 
     411         639 :         if (syntax_only) {
     412         233 :                 check_flags |= IS_CALLABLE_CHECK_SYNTAX_ONLY;
     413             :         }
     414         639 :         if (ZEND_NUM_ARGS() > 2) {
     415         236 :                 retval = zend_is_callable_ex(var, NULL, check_flags, &name, NULL, &error);
     416         236 :                 zval_dtor(callable_name);
     417             :                 //??? is it necessary to be consistent with old PHP ("\0" support)
     418         236 :                 if (UNEXPECTED(name->len) != strlen(name->val)) {
     419         432 :                         ZVAL_STRINGL(callable_name, name->val, strlen(name->val));
     420         216 :                         zend_string_release(name);
     421             :                 } else {
     422          20 :                         ZVAL_STR(callable_name, name);
     423             :                 }
     424             :         } else {
     425         403 :                 retval = zend_is_callable_ex(var, NULL, check_flags, NULL, NULL, &error);
     426             :         }
     427         639 :         if (error) {
     428             :                 /* ignore errors */
     429         376 :                 efree(error);
     430             :         }
     431             : 
     432         639 :         RETURN_BOOL(retval);
     433             : }
     434             : /* }}} */
     435             : 
     436             : /*
     437             :  * Local variables:
     438             :  * tab-width: 4
     439             :  * c-basic-offset: 4
     440             :  * End:
     441             :  * vim600: sw=4 ts=4 fdm=marker
     442             :  * vim<600: sw=4 ts=4
     443             :  */

Generated by: LCOV version 1.10

Generated at Sat, 21 Feb 2015 16:01:59 +0000 (7 days ago)

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