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: 175 192 91.1 %
Date: 2022-01-16 Functions: 18 19 94.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 7                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1997-2018 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        2722 : PHP_FUNCTION(gettype)
      27             : {
      28             :         zval *arg;
      29             : 
      30        2722 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &arg) == FAILURE) {
      31           2 :                 return;
      32             :         }
      33             : 
      34        5440 :         switch (Z_TYPE_P(arg)) {
      35         425 :                 case IS_NULL:
      36         850 :                         RETVAL_STRING("NULL");
      37         425 :                         break;
      38             : 
      39          80 :                 case IS_FALSE:
      40             :                 case IS_TRUE:
      41         160 :                         RETVAL_STRING("boolean");
      42          80 :                         break;
      43             : 
      44         289 :                 case IS_LONG:
      45         578 :                         RETVAL_STRING("integer");
      46         289 :                         break;
      47             : 
      48          62 :                 case IS_DOUBLE:
      49         124 :                         RETVAL_STRING("double");
      50          62 :                         break;
      51             : 
      52        1714 :                 case IS_STRING:
      53        3428 :                         RETVAL_STRING("string");
      54        1714 :                         break;
      55             : 
      56         111 :                 case IS_ARRAY:
      57         222 :                         RETVAL_STRING("array");
      58         111 :                         break;
      59             : 
      60          29 :                 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          10 :                 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       47005 : PHP_FUNCTION(settype)
      94             : {
      95             :         zval *var;
      96             :         char *type;
      97       47005 :         size_t type_len = 0;
      98             : 
      99       47005 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "zs", &var, &type, &type_len) == FAILURE) {
     100          43 :                 return;
     101             :         }
     102             : 
     103       94006 :         ZVAL_DEREF(var);
     104       47003 :         if (!strcasecmp(type, "integer")) {
     105          25 :                 convert_to_long(var);
     106       46978 :         } else if (!strcasecmp(type, "int")) {
     107          27 :                 convert_to_long(var);
     108       46951 :         } else if (!strcasecmp(type, "float")) {
     109          14 :                 convert_to_double(var);
     110       46937 :         } else if (!strcasecmp(type, "double")) { /* deprecated */
     111          38 :                 convert_to_double(var);
     112       46899 :         } else if (!strcasecmp(type, "string")) {
     113       93388 :                 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       46964 :         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          61 : PHP_FUNCTION(intval)
     138             : {
     139             :         zval *num;
     140          61 :         zend_long base = 10;
     141             : 
     142          61 :         if (ZEND_NUM_ARGS() != 1 && ZEND_NUM_ARGS() != 2) {
     143          53 :                 WRONG_PARAM_COUNT;
     144             :         }
     145          61 :         ZEND_PARSE_PARAMETERS_START(1, 2)
     146          61 :                 Z_PARAM_ZVAL(num)
     147          61 :                 Z_PARAM_OPTIONAL
     148         183 :                 Z_PARAM_LONG(base)
     149          61 :         ZEND_PARSE_PARAMETERS_END();
     150             : 
     151         122 :         if (Z_TYPE_P(num) != IS_STRING || base == 10) {
     152          58 :                 RETVAL_LONG(zval_get_long(num));
     153          29 :                 return;
     154             :         }
     155             : 
     156             : 
     157          32 :         if (base == 0 || base == 2) {
     158          32 :                 char *strval = Z_STRVAL_P(num);
     159          32 :                 size_t strlen = Z_STRLEN_P(num);
     160             : 
     161         100 :                 while (isspace(*strval) && strlen) {
     162          36 :                         strval++;
     163          36 :                         strlen--;
     164             :                 }
     165             : 
     166             :                 /* Length of 3+ covers "0b#" and "-0b" (which results in 0) */
     167          32 :                 if (strlen > 2) {
     168          28 :                         int offset = 0;
     169          28 :                         if (strval[0] == '-' || strval[0] == '+') {
     170          10 :                                 offset = 1;
     171             :                         }
     172             : 
     173          28 :                         if (strval[offset] == '0' && (strval[offset + 1] == 'b' || strval[offset + 1] == 'B')) {
     174             :                                 char *tmpval;
     175          24 :                                 strlen -= 2; /* Removing "0b" */
     176          24 :                                 tmpval = emalloc(strlen + 1);
     177             : 
     178             :                                 /* Place the unary symbol at pos 0 if there was one */
     179          24 :                                 if (offset) {
     180           8 :                                         tmpval[0] = strval[0];
     181             :                                 }
     182             : 
     183             :                                 /* Copy the data from after "0b" to the end of the buffer */
     184          24 :                                 memcpy(tmpval + offset, strval + offset + 2, strlen - offset);
     185          24 :                                 tmpval[strlen] = 0;
     186             : 
     187          24 :                                 RETVAL_LONG(ZEND_STRTOL(tmpval, NULL, 2));
     188          24 :                                 efree(tmpval);
     189          24 :                                 return;
     190             :                         }
     191             :                 }
     192             :         }
     193             : 
     194           8 :         RETVAL_LONG(ZEND_STRTOL(Z_STRVAL_P(num), NULL, base));
     195             : }
     196             : /* }}} */
     197             : 
     198             : /* {{{ proto float floatval(mixed var)
     199             :    Get the float value of a variable */
     200           8 : PHP_FUNCTION(floatval)
     201             : {
     202             :         zval *num;
     203             : 
     204           8 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &num) == FAILURE) {
     205           8 :                 return;
     206             :         }
     207             : 
     208           0 :         RETURN_DOUBLE(zval_get_double(num));
     209             : }
     210             : /* }}} */
     211             : 
     212             : /* {{{ proto bool boolval(mixed var)
     213             :    Get the boolean value of a variable */
     214           0 : PHP_FUNCTION(boolval)
     215             : {
     216             :         zval *val;
     217             : 
     218           0 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &val) == FAILURE) {
     219           0 :                 return;
     220             :         }
     221             : 
     222           0 :         RETURN_BOOL(zend_is_true(val));
     223             : }
     224             : /* }}} */
     225             : 
     226             : /* {{{ proto string strval(mixed var)
     227             :    Get the string value of a variable */
     228           4 : PHP_FUNCTION(strval)
     229             : {
     230             :         zval *num;
     231             : 
     232           4 :         ZEND_PARSE_PARAMETERS_START(1, 1)
     233           0 :                 Z_PARAM_ZVAL(num)
     234           8 :         ZEND_PARSE_PARAMETERS_END();
     235             : 
     236           0 :         RETVAL_STR(zval_get_string(num));
     237             : }
     238             : /* }}} */
     239             : 
     240          30 : static inline void php_is_type(INTERNAL_FUNCTION_PARAMETERS, int type)
     241             : {
     242             :         zval *arg;
     243             : 
     244          30 :         ZEND_PARSE_PARAMETERS_START(1, 1)
     245          16 :                 Z_PARAM_ZVAL_DEREF(arg)
     246          30 :         ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
     247             : 
     248          16 :         if (Z_TYPE_P(arg) == type) {
     249           6 :                 if (type == IS_OBJECT) {
     250           0 :                         zend_class_entry *ce = Z_OBJCE_P(arg);
     251           0 :                         if (ZSTR_LEN(ce->name) == sizeof(INCOMPLETE_CLASS) - 1
     252           0 :                                         && !memcmp(ZSTR_VAL(ce->name), INCOMPLETE_CLASS, sizeof(INCOMPLETE_CLASS) - 1)) {
     253           0 :                                 RETURN_FALSE;
     254             :                         }
     255           6 :                 } else if (type == IS_RESOURCE) {
     256           0 :                         const char *type_name = zend_rsrc_list_get_rsrc_type(Z_RES_P(arg));
     257           0 :                         if (!type_name) {
     258           0 :                                 RETURN_FALSE;
     259             :                         }
     260             :                 }
     261           6 :                 RETURN_TRUE;
     262             :         } else {
     263           2 :                 RETURN_FALSE;
     264             :         }
     265             : }
     266             : 
     267             : 
     268             : /* {{{ proto bool is_null(mixed var)
     269             :    Returns true if variable is null
     270             :    Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
     271           2 : PHP_FUNCTION(is_null)
     272             : {
     273           2 :         php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_NULL);
     274           2 : }
     275             : /* }}} */
     276             : 
     277             : /* {{{ proto bool is_resource(mixed var)
     278             :    Returns true if variable is a resource
     279             :    Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
     280           2 : PHP_FUNCTION(is_resource)
     281             : {
     282           2 :         php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_RESOURCE);
     283           2 : }
     284             : /* }}} */
     285             : 
     286             : /* {{{ proto bool is_bool(mixed var)
     287             :    Returns true if variable is a boolean
     288             :    Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
     289           2 : PHP_FUNCTION(is_bool)
     290             : {
     291             :         zval *arg;
     292             : 
     293           2 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &arg) == FAILURE) {
     294           2 :                 RETURN_FALSE;
     295             :         }
     296             : 
     297           0 :         ZVAL_DEREF(arg);
     298           0 :         RETURN_BOOL(Z_TYPE_P(arg) == IS_FALSE || Z_TYPE_P(arg) == IS_TRUE);
     299             : }
     300             : /* }}} */
     301             : 
     302             : /* {{{ proto bool is_int(mixed var)
     303             :    Returns true if variable is an integer
     304             :    Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
     305          14 : PHP_FUNCTION(is_int)
     306             : {
     307          14 :         php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_LONG);
     308          14 : }
     309             : /* }}} */
     310             : 
     311             : /* {{{ proto bool is_float(mixed var)
     312             :    Returns true if variable is float point
     313             :    Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
     314           6 : PHP_FUNCTION(is_float)
     315             : {
     316           6 :         php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_DOUBLE);
     317           6 : }
     318             : /* }}} */
     319             : 
     320             : /* {{{ proto bool is_string(mixed var)
     321             :    Returns true if variable is a string
     322             :    Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
     323           2 : PHP_FUNCTION(is_string)
     324             : {
     325           2 :         php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_STRING);
     326           2 : }
     327             : /* }}} */
     328             : 
     329             : /* {{{ proto bool is_array(mixed var)
     330             :    Returns true if variable is an array
     331             :    Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
     332           2 : PHP_FUNCTION(is_array)
     333             : {
     334           2 :         php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_ARRAY);
     335           2 : }
     336             : /* }}} */
     337             : 
     338             : /* {{{ proto bool is_object(mixed var)
     339             :    Returns true if variable is an object
     340             :    Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
     341           2 : PHP_FUNCTION(is_object)
     342             : {
     343           2 :         php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_OBJECT);
     344           2 : }
     345             : /* }}} */
     346             : 
     347             : /* {{{ proto bool is_numeric(mixed value)
     348             :    Returns true if value is a number or a numeric string */
     349         130 : PHP_FUNCTION(is_numeric)
     350             : {
     351             :         zval *arg;
     352             : 
     353         130 :         ZEND_PARSE_PARAMETERS_START(1, 1)
     354         124 :                 Z_PARAM_ZVAL(arg)
     355         130 :         ZEND_PARSE_PARAMETERS_END();
     356             : 
     357         248 :         switch (Z_TYPE_P(arg)) {
     358          62 :                 case IS_LONG:
     359             :                 case IS_DOUBLE:
     360          62 :                         RETURN_TRUE;
     361             :                         break;
     362             : 
     363          49 :                 case IS_STRING:
     364          98 :                         if (is_numeric_string(Z_STRVAL_P(arg), Z_STRLEN_P(arg), NULL, NULL, 0)) {
     365          18 :                                 RETURN_TRUE;
     366             :                         } else {
     367          31 :                                 RETURN_FALSE;
     368             :                         }
     369             :                         break;
     370             : 
     371          13 :                 default:
     372          13 :                         RETURN_FALSE;
     373             :                         break;
     374             :         }
     375             : }
     376             : /* }}} */
     377             : 
     378             : /* {{{ proto bool is_scalar(mixed value)
     379             :    Returns true if value is a scalar */
     380          62 : PHP_FUNCTION(is_scalar)
     381             : {
     382             :         zval *arg;
     383             : 
     384          62 :         ZEND_PARSE_PARAMETERS_START(1, 1)
     385          59 :                 Z_PARAM_ZVAL(arg)
     386          62 :         ZEND_PARSE_PARAMETERS_END();
     387             : 
     388         118 :         switch (Z_TYPE_P(arg)) {
     389          37 :                 case IS_FALSE:
     390             :                 case IS_TRUE:
     391             :                 case IS_DOUBLE:
     392             :                 case IS_LONG:
     393             :                 case IS_STRING:
     394          37 :                         RETURN_TRUE;
     395             :                         break;
     396             : 
     397          22 :                 default:
     398          22 :                         RETURN_FALSE;
     399             :                         break;
     400             :         }
     401             : }
     402             : /* }}} */
     403             : 
     404             : /* {{{ proto bool is_callable(mixed var [, bool syntax_only [, string callable_name]])
     405             :    Returns true if var is callable. */
     406         647 : PHP_FUNCTION(is_callable)
     407             : {
     408         647 :         zval *var, *callable_name = NULL;
     409             :         zend_string *name;
     410             :         char *error;
     411             :         zend_bool retval;
     412         647 :         zend_bool syntax_only = 0;
     413         647 :         int check_flags = 0;
     414             : 
     415         647 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|bz/", &var,
     416             :                                                           &syntax_only, &callable_name) == FAILURE) {
     417           2 :                 return;
     418             :         }
     419             : 
     420         645 :         if (syntax_only) {
     421         233 :                 check_flags |= IS_CALLABLE_CHECK_SYNTAX_ONLY;
     422             :         }
     423         645 :         if (ZEND_NUM_ARGS() > 2) {
     424         237 :                 retval = zend_is_callable_ex(var, NULL, check_flags, &name, NULL, &error);
     425         237 :                 zval_dtor(callable_name);
     426         237 :                 ZVAL_STR(callable_name, name);
     427             :         } else {
     428         408 :                 retval = zend_is_callable_ex(var, NULL, check_flags, NULL, NULL, &error);
     429             :         }
     430         645 :         if (error) {
     431             :                 /* ignore errors */
     432         379 :                 efree(error);
     433             :         }
     434             : 
     435         645 :         RETURN_BOOL(retval);
     436             : }
     437             : /* }}} */
     438             : 
     439             : /* {{{ proto bool is_iterable(mixed var)
     440             :    Returns true if var is iterable (array or instance of Traversable). */
     441           6 : PHP_FUNCTION(is_iterable)
     442             : {
     443             :         zval *var;
     444             : 
     445           6 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &var) == FAILURE) {
     446           0 :                 return;
     447             :         }
     448             : 
     449           6 :         RETURN_BOOL(zend_is_iterable(var));
     450             : }
     451             : /* }}} */
     452             : 
     453             : /*
     454             :  * Local variables:
     455             :  * tab-width: 4
     456             :  * c-basic-offset: 4
     457             :  * End:
     458             :  * vim600: sw=4 ts=4 fdm=marker
     459             :  * vim<600: sw=4 ts=4
     460             :  */

Generated by: LCOV version 1.10

Generated at Sun, 16 Jan 2022 08:19:23 +0000 (6 days ago)

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