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: 153 172 89.0 %
Date: 2022-01-21 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           2 : PHP_FUNCTION(gettype)
      27             : {
      28             :         zval *arg;
      29             :         zend_string *type;
      30             : 
      31           2 :         ZEND_PARSE_PARAMETERS_START(1, 1)
      32           0 :                 Z_PARAM_ZVAL(arg)
      33           2 :         ZEND_PARSE_PARAMETERS_END();
      34             : 
      35           0 :         type = zend_zval_get_type(arg);
      36           0 :         if (EXPECTED(type)) {
      37           0 :                 RETURN_INTERNED_STR(type);
      38             :         } else {
      39           0 :                 RETURN_STRING("unknown type");
      40             :         }
      41             : }
      42             : /* }}} */
      43             : 
      44             : /* {{{ proto bool settype(mixed &var, string type)
      45             :    Set the type of the variable */
      46       47875 : PHP_FUNCTION(settype)
      47             : {
      48             :         zval *var;
      49             :         char *type;
      50       47875 :         size_t type_len = 0;
      51             : 
      52       47875 :         ZEND_PARSE_PARAMETERS_START(2, 2)
      53       95746 :                 Z_PARAM_ZVAL_DEREF(var)
      54       95746 :                 Z_PARAM_STRING(type, type_len)
      55       47916 :         ZEND_PARSE_PARAMETERS_END();
      56             : 
      57       47873 :         if (!strcasecmp(type, "integer")) {
      58          25 :                 convert_to_long(var);
      59       47848 :         } else if (!strcasecmp(type, "int")) {
      60          27 :                 convert_to_long(var);
      61       47821 :         } else if (!strcasecmp(type, "float")) {
      62          14 :                 convert_to_double(var);
      63       47807 :         } else if (!strcasecmp(type, "double")) { /* deprecated */
      64          38 :                 convert_to_double(var);
      65       47769 :         } else if (!strcasecmp(type, "string")) {
      66       95128 :                 convert_to_string(var);
      67         205 :         } else if (!strcasecmp(type, "array")) {
      68          38 :                 convert_to_array(var);
      69         167 :         } else if (!strcasecmp(type, "object")) {
      70          38 :                 convert_to_object(var);
      71         129 :         } else if (!strcasecmp(type, "bool")) {
      72          27 :                 convert_to_boolean(var);
      73         102 :         } else if (!strcasecmp(type, "boolean")) {
      74          25 :                 convert_to_boolean(var);
      75          77 :         } else if (!strcasecmp(type, "null")) {
      76          38 :                 convert_to_null(var);
      77          39 :         } else if (!strcasecmp(type, "resource")) {
      78          38 :                 php_error_docref(NULL, E_WARNING, "Cannot convert to resource type");
      79          38 :                 RETURN_FALSE;
      80             :         } else {
      81           1 :                 php_error_docref(NULL, E_WARNING, "Invalid type");
      82           1 :                 RETURN_FALSE;
      83             :         }
      84       47834 :         RETVAL_TRUE;
      85             : }
      86             : /* }}} */
      87             : 
      88             : /* {{{ proto int intval(mixed var [, int base])
      89             :    Get the integer value of a variable using the optional base for the conversion */
      90          61 : PHP_FUNCTION(intval)
      91             : {
      92             :         zval *num;
      93          61 :         zend_long base = 10;
      94             : 
      95          61 :         if (ZEND_NUM_ARGS() != 1 && ZEND_NUM_ARGS() != 2) {
      96          53 :                 WRONG_PARAM_COUNT;
      97             :         }
      98          61 :         ZEND_PARSE_PARAMETERS_START(1, 2)
      99          61 :                 Z_PARAM_ZVAL(num)
     100          61 :                 Z_PARAM_OPTIONAL
     101         122 :                 Z_PARAM_LONG(base)
     102          61 :         ZEND_PARSE_PARAMETERS_END();
     103             : 
     104         122 :         if (Z_TYPE_P(num) != IS_STRING || base == 10) {
     105          58 :                 RETVAL_LONG(zval_get_long(num));
     106          29 :                 return;
     107             :         }
     108             : 
     109             : 
     110          32 :         if (base == 0 || base == 2) {
     111          32 :                 char *strval = Z_STRVAL_P(num);
     112          32 :                 size_t strlen = Z_STRLEN_P(num);
     113             : 
     114         100 :                 while (isspace(*strval) && strlen) {
     115          36 :                         strval++;
     116          36 :                         strlen--;
     117             :                 }
     118             : 
     119             :                 /* Length of 3+ covers "0b#" and "-0b" (which results in 0) */
     120          32 :                 if (strlen > 2) {
     121          28 :                         int offset = 0;
     122          28 :                         if (strval[0] == '-' || strval[0] == '+') {
     123          10 :                                 offset = 1;
     124             :                         }
     125             : 
     126          28 :                         if (strval[offset] == '0' && (strval[offset + 1] == 'b' || strval[offset + 1] == 'B')) {
     127             :                                 char *tmpval;
     128          24 :                                 strlen -= 2; /* Removing "0b" */
     129          24 :                                 tmpval = emalloc(strlen + 1);
     130             : 
     131             :                                 /* Place the unary symbol at pos 0 if there was one */
     132          24 :                                 if (offset) {
     133           8 :                                         tmpval[0] = strval[0];
     134             :                                 }
     135             : 
     136             :                                 /* Copy the data from after "0b" to the end of the buffer */
     137          24 :                                 memcpy(tmpval + offset, strval + offset + 2, strlen - offset);
     138          24 :                                 tmpval[strlen] = 0;
     139             : 
     140          24 :                                 RETVAL_LONG(ZEND_STRTOL(tmpval, NULL, 2));
     141          24 :                                 efree(tmpval);
     142          24 :                                 return;
     143             :                         }
     144             :                 }
     145             :         }
     146             : 
     147           8 :         RETVAL_LONG(ZEND_STRTOL(Z_STRVAL_P(num), NULL, base));
     148             : }
     149             : /* }}} */
     150             : 
     151             : /* {{{ proto float floatval(mixed var)
     152             :    Get the float value of a variable */
     153           8 : PHP_FUNCTION(floatval)
     154             : {
     155             :         zval *num;
     156             : 
     157           8 :         ZEND_PARSE_PARAMETERS_START(1, 1)
     158           0 :                 Z_PARAM_ZVAL(num)
     159           8 :         ZEND_PARSE_PARAMETERS_END();
     160             : 
     161           0 :         RETURN_DOUBLE(zval_get_double(num));
     162             : }
     163             : /* }}} */
     164             : 
     165             : /* {{{ proto bool boolval(mixed var)
     166             :    Get the boolean value of a variable */
     167           0 : PHP_FUNCTION(boolval)
     168             : {
     169             :         zval *val;
     170             : 
     171           0 :         ZEND_PARSE_PARAMETERS_START(1, 1)
     172           0 :                 Z_PARAM_ZVAL(val)
     173           0 :         ZEND_PARSE_PARAMETERS_END();
     174             : 
     175           0 :         RETURN_BOOL(zend_is_true(val));
     176             : }
     177             : /* }}} */
     178             : 
     179             : /* {{{ proto string strval(mixed var)
     180             :    Get the string value of a variable */
     181           4 : PHP_FUNCTION(strval)
     182             : {
     183             :         zval *num;
     184             : 
     185           4 :         ZEND_PARSE_PARAMETERS_START(1, 1)
     186           0 :                 Z_PARAM_ZVAL(num)
     187           8 :         ZEND_PARSE_PARAMETERS_END();
     188             : 
     189           0 :         RETVAL_STR(zval_get_string(num));
     190             : }
     191             : /* }}} */
     192             : 
     193          30 : static inline void php_is_type(INTERNAL_FUNCTION_PARAMETERS, int type)
     194             : {
     195             :         zval *arg;
     196             : 
     197          30 :         ZEND_PARSE_PARAMETERS_START(1, 1)
     198           8 :                 Z_PARAM_ZVAL(arg)
     199          30 :         ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
     200             : 
     201          16 :         if (Z_TYPE_P(arg) == type) {
     202           6 :                 if (type == IS_RESOURCE) {
     203           0 :                         const char *type_name = zend_rsrc_list_get_rsrc_type(Z_RES_P(arg));
     204           0 :                         if (!type_name) {
     205           0 :                                 RETURN_FALSE;
     206             :                         }
     207             :                 }
     208           6 :                 RETURN_TRUE;
     209             :         } else {
     210           2 :                 RETURN_FALSE;
     211             :         }
     212             : }
     213             : 
     214             : 
     215             : /* {{{ proto bool is_null(mixed var)
     216             :    Returns true if variable is null
     217             :    Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
     218           2 : PHP_FUNCTION(is_null)
     219             : {
     220           2 :         php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_NULL);
     221           2 : }
     222             : /* }}} */
     223             : 
     224             : /* {{{ proto bool is_resource(mixed var)
     225             :    Returns true if variable is a resource
     226             :    Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
     227           2 : PHP_FUNCTION(is_resource)
     228             : {
     229           2 :         php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_RESOURCE);
     230           2 : }
     231             : /* }}} */
     232             : 
     233             : /* {{{ proto bool is_bool(mixed var)
     234             :    Returns true if variable is a boolean
     235             :    Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
     236           2 : PHP_FUNCTION(is_bool)
     237             : {
     238             :         zval *arg;
     239             : 
     240           2 :         ZEND_PARSE_PARAMETERS_START(1, 1)
     241           0 :                 Z_PARAM_ZVAL(arg)
     242           2 :         ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
     243             : 
     244           0 :         RETURN_BOOL(Z_TYPE_P(arg) == IS_FALSE || Z_TYPE_P(arg) == IS_TRUE);
     245             : }
     246             : /* }}} */
     247             : 
     248             : /* {{{ proto bool is_int(mixed var)
     249             :    Returns true if variable is an integer
     250             :    Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
     251          14 : PHP_FUNCTION(is_int)
     252             : {
     253          14 :         php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_LONG);
     254          14 : }
     255             : /* }}} */
     256             : 
     257             : /* {{{ proto bool is_float(mixed var)
     258             :    Returns true if variable is float point
     259             :    Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
     260           6 : PHP_FUNCTION(is_float)
     261             : {
     262           6 :         php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_DOUBLE);
     263           6 : }
     264             : /* }}} */
     265             : 
     266             : /* {{{ proto bool is_string(mixed var)
     267             :    Returns true if variable is a string
     268             :    Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
     269           2 : PHP_FUNCTION(is_string)
     270             : {
     271           2 :         php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_STRING);
     272           2 : }
     273             : /* }}} */
     274             : 
     275             : /* {{{ proto bool is_array(mixed var)
     276             :    Returns true if variable is an array
     277             :    Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
     278           2 : PHP_FUNCTION(is_array)
     279             : {
     280           2 :         php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_ARRAY);
     281           2 : }
     282             : /* }}} */
     283             : 
     284             : /* {{{ proto bool is_object(mixed var)
     285             :    Returns true if variable is an object
     286             :    Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
     287           2 : PHP_FUNCTION(is_object)
     288             : {
     289           2 :         php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_OBJECT);
     290           2 : }
     291             : /* }}} */
     292             : 
     293             : /* {{{ proto bool is_numeric(mixed value)
     294             :    Returns true if value is a number or a numeric string */
     295         130 : PHP_FUNCTION(is_numeric)
     296             : {
     297             :         zval *arg;
     298             : 
     299         130 :         ZEND_PARSE_PARAMETERS_START(1, 1)
     300         124 :                 Z_PARAM_ZVAL(arg)
     301         130 :         ZEND_PARSE_PARAMETERS_END();
     302             : 
     303         248 :         switch (Z_TYPE_P(arg)) {
     304          62 :                 case IS_LONG:
     305             :                 case IS_DOUBLE:
     306          62 :                         RETURN_TRUE;
     307             :                         break;
     308             : 
     309          49 :                 case IS_STRING:
     310          98 :                         if (is_numeric_string(Z_STRVAL_P(arg), Z_STRLEN_P(arg), NULL, NULL, 0)) {
     311          18 :                                 RETURN_TRUE;
     312             :                         } else {
     313          31 :                                 RETURN_FALSE;
     314             :                         }
     315             :                         break;
     316             : 
     317          13 :                 default:
     318          13 :                         RETURN_FALSE;
     319             :                         break;
     320             :         }
     321             : }
     322             : /* }}} */
     323             : 
     324             : /* {{{ proto bool is_scalar(mixed value)
     325             :    Returns true if value is a scalar */
     326         110 : PHP_FUNCTION(is_scalar)
     327             : {
     328             :         zval *arg;
     329             : 
     330         110 :         ZEND_PARSE_PARAMETERS_START(1, 1)
     331         107 :                 Z_PARAM_ZVAL(arg)
     332         110 :         ZEND_PARSE_PARAMETERS_END();
     333             : 
     334         214 :         switch (Z_TYPE_P(arg)) {
     335          69 :                 case IS_FALSE:
     336             :                 case IS_TRUE:
     337             :                 case IS_DOUBLE:
     338             :                 case IS_LONG:
     339             :                 case IS_STRING:
     340          69 :                         RETURN_TRUE;
     341             :                         break;
     342             : 
     343          38 :                 default:
     344          38 :                         RETURN_FALSE;
     345             :                         break;
     346             :         }
     347             : }
     348             : /* }}} */
     349             : 
     350             : /* {{{ proto bool is_callable(mixed var [, bool syntax_only [, string &callable_name]])
     351             :    Returns true if var is callable. */
     352         647 : PHP_FUNCTION(is_callable)
     353             : {
     354         647 :         zval *var, *callable_name = NULL;
     355             :         zend_string *name;
     356             :         char *error;
     357             :         zend_bool retval;
     358         647 :         zend_bool syntax_only = 0;
     359         647 :         int check_flags = 0;
     360             : 
     361         647 :         ZEND_PARSE_PARAMETERS_START(1, 3)
     362         645 :                 Z_PARAM_ZVAL(var)
     363         645 :                 Z_PARAM_OPTIONAL
     364        1111 :                 Z_PARAM_BOOL(syntax_only)
     365         703 :                 Z_PARAM_ZVAL_DEREF(callable_name)
     366         647 :         ZEND_PARSE_PARAMETERS_END();
     367             : 
     368         645 :         if (syntax_only) {
     369         233 :                 check_flags |= IS_CALLABLE_CHECK_SYNTAX_ONLY;
     370             :         }
     371         645 :         if (ZEND_NUM_ARGS() > 2) {
     372         237 :                 retval = zend_is_callable_ex(var, NULL, check_flags, &name, NULL, &error);
     373         237 :                 zval_ptr_dtor(callable_name);
     374         237 :                 ZVAL_STR(callable_name, name);
     375             :         } else {
     376         408 :                 retval = zend_is_callable_ex(var, NULL, check_flags, NULL, NULL, &error);
     377             :         }
     378         645 :         if (error) {
     379             :                 /* ignore errors */
     380         379 :                 efree(error);
     381             :         }
     382             : 
     383         645 :         RETURN_BOOL(retval);
     384             : }
     385             : /* }}} */
     386             : 
     387             : /* {{{ proto bool is_iterable(mixed var)
     388             :    Returns true if var is iterable (array or instance of Traversable). */
     389           6 : PHP_FUNCTION(is_iterable)
     390             : {
     391             :         zval *var;
     392             : 
     393           6 :         ZEND_PARSE_PARAMETERS_START(1, 1)
     394           6 :                 Z_PARAM_ZVAL(var)
     395           6 :         ZEND_PARSE_PARAMETERS_END();
     396             : 
     397           6 :         RETURN_BOOL(zend_is_iterable(var));
     398             : }
     399             : /* }}} */
     400             : 
     401             : /*
     402             :  * Local variables:
     403             :  * tab-width: 4
     404             :  * c-basic-offset: 4
     405             :  * End:
     406             :  * vim600: sw=4 ts=4 fdm=marker
     407             :  * vim<600: sw=4 ts=4
     408             :  */

Generated by: LCOV version 1.10

Generated at Fri, 21 Jan 2022 14:15:23 +0000 (30 hours ago)

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