Skip to content

Commit cdd8368

Browse files
carusogabrielpetk
authored andcommitted
Clean up unnecessary ternary expressions and simplify some returns
- Simplify conditions - Use ZEND_HASH_APPLY_* instead of hard-coded booleans - Use ZEND_NORMALIZE_BOOL - Drop sign in favor of ZEND_NORMALIZE_BOOL
1 parent 6c16f9b commit cdd8368

26 files changed

+39
-63
lines changed

Zend/zend_compile.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1576,7 +1576,7 @@ int ZEND_FASTCALL zendlex(zend_parser_stack_elem *elem) /* {{{ */
15761576

15771577
ZEND_API void zend_initialize_class_data(zend_class_entry *ce, zend_bool nullify_handlers) /* {{{ */
15781578
{
1579-
zend_bool persistent_hashes = (ce->type == ZEND_INTERNAL_CLASS) ? 1 : 0;
1579+
zend_bool persistent_hashes = ce->type == ZEND_INTERNAL_CLASS;
15801580

15811581
ce->refcount = 1;
15821582
ce->ce_flags = ZEND_ACC_CONSTANTS_UPDATED;

Zend/zend_constants.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ static int clean_module_constant(zval *el, void *arg)
8383
int module_number = *(int *)arg;
8484

8585
if (ZEND_CONSTANT_MODULE_NUMBER(c) == module_number) {
86-
return 1;
86+
return ZEND_HASH_APPLY_REMOVE;
8787
} else {
88-
return 0;
88+
return ZEND_HASH_APPLY_KEEP;
8989
}
9090
}
9191

Zend/zend_execute.c

+1-4
Original file line numberDiff line numberDiff line change
@@ -845,10 +845,7 @@ static zend_bool zend_verify_weak_scalar_type_hint(zend_uchar type_hint, zval *a
845845
zend_string *dest;
846846

847847
/* on success "arg" is converted to IS_STRING */
848-
if (!zend_parse_arg_str_weak(arg, &dest)) {
849-
return 0;
850-
}
851-
return 1;
848+
return zend_parse_arg_str_weak(arg, &dest);
852849
}
853850
default:
854851
return 0;

Zend/zend_ini.c

+2-5
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,8 @@ static int zend_remove_ini_entries(zval *el, void *arg) /* {{{ */
3636
{
3737
zend_ini_entry *ini_entry = (zend_ini_entry *)Z_PTR_P(el);
3838
int module_number = *(int *)arg;
39-
if (ini_entry->module_number == module_number) {
40-
return 1;
41-
} else {
42-
return 0;
43-
}
39+
40+
return ini_entry->module_number == module_number;
4441
}
4542
/* }}} */
4643

Zend/zend_list.c

+2-5
Original file line numberDiff line numberDiff line change
@@ -245,11 +245,8 @@ void zend_destroy_rsrc_list(HashTable *ht)
245245
static int clean_module_resource(zval *zv, void *arg)
246246
{
247247
int resource_id = *(int *)arg;
248-
if (Z_RES_TYPE_P(zv) == resource_id) {
249-
return 1;
250-
} else {
251-
return 0;
252-
}
248+
249+
return Z_RES_TYPE_P(zv) == resource_id;
253250
}
254251

255252

Zend/zend_stream.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ ZEND_API int zend_stream_fixup(zend_file_handle *file_handle, char **buf, size_t
186186
return FAILURE;
187187
}
188188
memset(&file_handle->handle.stream.mmap, 0, sizeof(zend_mmap));
189-
file_handle->handle.stream.isatty = isatty(fileno((FILE *)file_handle->handle.stream.handle)) ? 1 : 0;
189+
file_handle->handle.stream.isatty = isatty(fileno((FILE *)file_handle->handle.stream.handle));
190190
file_handle->handle.stream.reader = (zend_stream_reader_t)zend_stream_stdio_reader;
191191
file_handle->handle.stream.closer = (zend_stream_closer_t)zend_stream_stdio_closer;
192192
file_handle->handle.stream.fsizer = (zend_stream_fsizer_t)zend_stream_stdio_fsizer;

Zend/zend_virtual_cwd.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,7 @@ static size_t tsrm_realpath_r(char *path, size_t start, size_t len, int *ll, tim
665665

666666
if(pbuffer->ReparseTag == IO_REPARSE_TAG_SYMLINK) {
667667
reparsetarget = pbuffer->SymbolicLinkReparseBuffer.ReparseTarget;
668-
isabsolute = (pbuffer->SymbolicLinkReparseBuffer.Flags == 0) ? 1 : 0;
668+
isabsolute = pbuffer->SymbolicLinkReparseBuffer.Flags == 0;
669669
#if VIRTUAL_CWD_DEBUG
670670
printname = php_win32_ioutil_w_to_any(reparsetarget + pbuffer->MountPointReparseBuffer.PrintNameOffset / sizeof(WCHAR));
671671
if (!printname) {

ext/bcmath/libbcmath/src/num2str.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ zend_string
5050
int index, signch;
5151

5252
/* Allocate the string memory. */
53-
signch = ( num->n_sign == PLUS ? 0 : 1 ); /* Number of sign chars. */
53+
signch = num->n_sign != PLUS; /* Number of sign chars. */
5454
if (scale > 0)
5555
str = zend_string_alloc(num->n_len + scale + signch + 1, 0);
5656
else

ext/curl/multi.c

+2-5
Original file line numberDiff line numberDiff line change
@@ -595,11 +595,8 @@ static int _php_curl_multi_setopt(php_curlm *mh, zend_long option, zval *zvalue,
595595
}
596596

597597
SAVE_CURLM_ERROR(mh, error);
598-
if (error != CURLM_OK) {
599-
return 1;
600-
} else {
601-
return 0;
602-
}
598+
599+
return error != CURLM_OK;
603600
}
604601
/* }}} */
605602

ext/curl/share.c

+2-5
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,8 @@ static int _php_curl_share_setopt(php_curlsh *sh, zend_long option, zval *zvalue
8686
}
8787

8888
SAVE_CURLSH_ERROR(sh, error);
89-
if (error != CURLSHE_OK) {
90-
return 1;
91-
} else {
92-
return 0;
93-
}
89+
90+
return error != CURLSHE_OK;
9491
}
9592
/* }}} */
9693

ext/filter/tests/030.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ $ipv6_test = array(
5151
);
5252
foreach ($ipv6_test as $ip => $exp) {
5353
$out = filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);
54-
$out = (int) ($out === false ? 0 : 1);
54+
$out = $out !== false;
5555
if ($exp != $out) {
5656
echo "$ip failed (expected ", $exp?"true":"false", ", got ",
5757
$out?"true":"false", ")\n";

ext/imap/php_imap.c

+2-5
Original file line numberDiff line numberDiff line change
@@ -4074,11 +4074,8 @@ int _php_imap_mail(char *to, char *subject, char *message, char *headers, char *
40744074
}
40754075
fprintf(sendmail, "\n%s\n", message);
40764076
ret = pclose(sendmail);
4077-
if (ret == -1) {
4078-
return 0;
4079-
} else {
4080-
return 1;
4081-
}
4077+
4078+
return ret != -1;
40824079
} else {
40834080
php_error_docref(NULL, E_WARNING, "Could not execute mail delivery program");
40844081
return 0;

ext/interbase/interbase.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -869,7 +869,7 @@ int _php_ibase_attach_db(char **args, size_t *len, zend_long *largs, isc_db_hand
869869
buf_len -= dpb_len;
870870
}
871871
if (largs[SYNC] && buf_len > 0) {
872-
dpb_len = slprintf(dpb, buf_len, "%c\1%c", isc_dpb_force_write, largs[SYNC] == isc_spb_prp_wm_sync ? 1 : 0);
872+
dpb_len = slprintf(dpb, buf_len, "%c\1%c", isc_dpb_force_write, largs[SYNC] == isc_spb_prp_wm_sync);
873873
dpb += dpb_len;
874874
buf_len -= dpb_len;
875875
}

ext/intl/dateformat/dateformat_parse.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ static void internal_parse_to_localtime(IntlDateFormatter_object *dfo, char* tex
116116
/* Is in DST? */
117117
isInDST = ucal_inDaylightTime(parsed_calendar , &INTL_DATA_ERROR_CODE(dfo));
118118
INTL_METHOD_CHECK_STATUS( dfo, "Date parsing - localtime failed : while checking if currently in DST." );
119-
add_assoc_long( return_value, CALENDAR_ISDST,(isInDST==1?1:0));
119+
add_assoc_long( return_value, CALENDAR_ISDST,isInDST==1);
120120
}
121121
/* }}} */
122122

ext/mysqlnd/mysqlnd_connection.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1578,7 +1578,7 @@ MYSQLND_METHOD(mysqlnd_conn_data, change_user)(MYSQLND_CONN_DATA * const conn,
15781578

15791579
DBG_ENTER("mysqlnd_conn_data::change_user");
15801580
DBG_INF_FMT("conn=%llu user=%s passwd=%s db=%s silent=%u",
1581-
conn->thread_id, user?user:"", passwd?"***":"null", db?db:"", (silent == TRUE)?1:0 );
1581+
conn->thread_id, user?user:"", passwd?"***":"null", db?db:"", silent == TRUE);
15821582

15831583
if (PASS != conn->m->local_tx_start(conn, this_func)) {
15841584
goto end;

ext/pdo/pdo_stmt.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -2004,7 +2004,7 @@ static PHP_METHOD(PDOStatement, setFetchMode)
20042004

20052005
RETVAL_BOOL(
20062006
pdo_stmt_setup_fetch_mode(INTERNAL_FUNCTION_PARAM_PASSTHRU,
2007-
stmt, 0) == SUCCESS ? 1 : 0
2007+
stmt, 0) == SUCCESS
20082008
);
20092009
}
20102010
/* }}} */

ext/pdo_mysql/mysql_driver.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ static int pdo_mysql_set_attribute(pdo_dbh_t *dbh, zend_long attr, zval *val)
388388
PDO_DBG_RETURN(1);
389389

390390
case PDO_ATTR_DEFAULT_STR_PARAM:
391-
((pdo_mysql_db_handle *)dbh->driver_data)->assume_national_character_set_strings = lval == PDO_PARAM_STR_NATL ? 1 : 0;
391+
((pdo_mysql_db_handle *)dbh->driver_data)->assume_national_character_set_strings = lval == PDO_PARAM_STR_NATL;
392392
PDO_DBG_RETURN(1);
393393

394394
case PDO_MYSQL_ATTR_USE_BUFFERED_QUERY:
@@ -617,7 +617,7 @@ static int pdo_mysql_handle_factory(pdo_dbh_t *dbh, zval *driver_options)
617617
PDO_ATTR_EMULATE_PREPARES, H->emulate_prepare);
618618

619619
H->assume_national_character_set_strings = pdo_attr_lval(driver_options,
620-
PDO_ATTR_DEFAULT_STR_PARAM, 0) == PDO_PARAM_STR_NATL ? 1 : 0;
620+
PDO_ATTR_DEFAULT_STR_PARAM, 0) == PDO_PARAM_STR_NATL;
621621

622622
#ifndef PDO_USE_MYSQLND
623623
H->max_buffer_size = pdo_attr_lval(driver_options, PDO_MYSQL_ATTR_MAX_BUFFER_SIZE, H->max_buffer_size);

ext/pdo_pgsql/pgsql_statement.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ static int pgsql_stmt_get_col(pdo_stmt_t *stmt, int colno, char **ptr, zend_ulon
553553
break;
554554

555555
case PDO_PARAM_BOOL:
556-
S->cols[colno].boolval = **ptr == 't' ? 1: 0;
556+
S->cols[colno].boolval = **ptr == 't';
557557
*ptr = (char *) &(S->cols[colno].boolval);
558558
*len = sizeof(zend_bool);
559559
break;

ext/phar/phar_object.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -3618,7 +3618,7 @@ static void phar_add_file(phar_archive_data **pphar, char *filename, size_t file
36183618
php_stream_statbuf ssb;
36193619

36203620
if (filename_len >= sizeof(".phar")-1) {
3621-
start_pos = ('/' == filename[0] ? 1 : 0); /* account for any leading slash: multiple-leads handled elsewhere */
3621+
start_pos = '/' == filename[0]; /* account for any leading slash: multiple-leads handled elsewhere */
36223622
if (!memcmp(&filename[start_pos], ".phar", sizeof(".phar")-1) && (filename[start_pos+5] == '/' || filename[start_pos+5] == '\\' || filename[start_pos+5] == '\0')) {
36233623
zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Cannot create any files in magic \".phar\" directory");
36243624
return;

ext/spl/spl_heap.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ static int spl_ptr_heap_zval_max_cmp(zval *a, zval *b, zval *object) { /* {{{ */
178178
/* exception or call failure */
179179
return 0;
180180
}
181-
return lval > 0 ? 1 : (lval < 0 ? -1 : 0);
181+
return ZEND_NORMALIZE_BOOL(lval);
182182
}
183183
}
184184

@@ -202,7 +202,7 @@ static int spl_ptr_heap_zval_min_cmp(zval *a, zval *b, zval *object) { /* {{{ */
202202
/* exception or call failure */
203203
return 0;
204204
}
205-
return lval > 0 ? 1 : (lval < 0 ? -1 : 0);
205+
return ZEND_NORMALIZE_BOOL(lval);
206206
}
207207
}
208208

@@ -230,7 +230,7 @@ static int spl_ptr_pqueue_elem_cmp(zval *a_zv, zval *b_zv, zval *object) { /* {{
230230
/* exception or call failure */
231231
return 0;
232232
}
233-
return lval > 0 ? 1 : (lval < 0 ? -1 : 0);
233+
return ZEND_NORMALIZE_BOOL(lval);
234234
}
235235
}
236236

ext/spl/spl_observer.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ static int spl_object_storage_compare_info(zval *e1, zval *e2) /* {{{ */
352352
return 1;
353353
}
354354

355-
return Z_LVAL(result) > 0 ? 1 : (Z_LVAL(result) < 0 ? -1 : 0);
355+
return ZEND_NORMALIZE_BOOL(Z_LVAL(result));
356356
}
357357
/* }}} */
358358

ext/standard/array.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ static int php_array_key_compare(const void *a, const void *b) /* {{{ */
170170
}
171171
}
172172
}
173-
return l1 > l2 ? 1 : (l1 < l2 ? -1 : 0);
173+
return ZEND_NORMALIZE_BOOL(l1 > l2);
174174
}
175175
/* }}} */
176176

@@ -978,7 +978,7 @@ static int php_array_user_compare(const void *a, const void *b) /* {{{ */
978978
zval_ptr_dtor(&retval);
979979
zval_ptr_dtor(&args[1]);
980980
zval_ptr_dtor(&args[0]);
981-
return ret < 0 ? -1 : ret > 0 ? 1 : 0;
981+
return ZEND_NORMALIZE_BOOL(ret);
982982
} else {
983983
zval_ptr_dtor(&args[1]);
984984
zval_ptr_dtor(&args[0]);
@@ -1101,7 +1101,7 @@ static int php_array_user_key_compare(const void *a, const void *b) /* {{{ */
11011101
zval_ptr_dtor(&args[0]);
11021102
zval_ptr_dtor(&args[1]);
11031103

1104-
return result < 0 ? -1 : result > 0 ? 1 : 0;
1104+
return ZEND_NORMALIZE_BOOL(result);
11051105
}
11061106
/* }}} */
11071107

@@ -4583,7 +4583,7 @@ static int zval_user_compare(zval *a, zval *b) /* {{{ */
45834583
if (zend_call_function(&BG(user_compare_fci), &BG(user_compare_fci_cache)) == SUCCESS && Z_TYPE(retval) != IS_UNDEF) {
45844584
zend_long ret = zval_get_long(&retval);
45854585
zval_ptr_dtor(&retval);
4586-
return ret < 0 ? -1 : ret > 0 ? 1 : 0;
4586+
return ZEND_NORMALIZE_BOOL(ret);
45874587
} else {
45884588
return 0;
45894589
}

ext/standard/basic_functions.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -4340,7 +4340,7 @@ static int parse_opts(char * opts, opt_struct ** result)
43404340
(*opts >= 97 && *opts <= 122) /* a - z */
43414341
) {
43424342
paras->opt_char = *opts;
4343-
paras->need_param = (*(++opts) == ':') ? 1 : 0;
4343+
paras->need_param = *(++opts) == ':';
43444344
paras->opt_name = NULL;
43454345
if (paras->need_param == 1) {
43464346
opts++;

ext/standard/versioning.c

+2-4
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
#include "php.h"
2525
#include "php_versioning.h"
2626

27-
#define sign(n) ((n)<0?-1:((n)>0?1:0))
28-
2927
/* {{{ php_canonicalize_version() */
3028

3129
PHPAPI char *
@@ -115,7 +113,7 @@ compare_special_version_forms(char *form1, char *form2)
115113
break;
116114
}
117115
}
118-
return sign(found1 - found2);
116+
return ZEND_NORMALIZE_BOOL(found1 - found2);
119117
}
120118

121119
/* }}} */
@@ -160,7 +158,7 @@ php_version_compare(const char *orig_ver1, const char *orig_ver2)
160158
/* compare element numerically */
161159
l1 = strtol(p1, NULL, 10);
162160
l2 = strtol(p2, NULL, 10);
163-
compare = sign(l1 - l2);
161+
compare = ZEND_NORMALIZE_BOOL(l1 - l2);
164162
} else if (!isdigit(*p1) && !isdigit(*p2)) {
165163
/* compare element names */
166164
compare = compare_special_version_forms(p1, p2);

main/rfc1867.c

+1-5
Original file line numberDiff line numberDiff line change
@@ -281,11 +281,7 @@ static int fill_buffer(multipart_buffer *self)
281281
/* eof if we are out of bytes, or if we hit the final boundary */
282282
static int multipart_buffer_eof(multipart_buffer *self)
283283
{
284-
if ( (self->bytes_in_buffer == 0 && fill_buffer(self) < 1) ) {
285-
return 1;
286-
} else {
287-
return 0;
288-
}
284+
return self->bytes_in_buffer == 0 && fill_buffer(self) < 1;
289285
}
290286

291287
/* create new multipart_buffer structure */

main/streams/mmap.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ PHPAPI char *_php_stream_mmap_range(php_stream *stream, size_t offset, size_t le
4646

4747
PHPAPI int _php_stream_mmap_unmap(php_stream *stream)
4848
{
49-
return php_stream_set_option(stream, PHP_STREAM_OPTION_MMAP_API, PHP_STREAM_MMAP_UNMAP, NULL) == PHP_STREAM_OPTION_RETURN_OK ? 1 : 0;
49+
return php_stream_set_option(stream, PHP_STREAM_OPTION_MMAP_API, PHP_STREAM_MMAP_UNMAP, NULL) == PHP_STREAM_OPTION_RETURN_OK;
5050
}
5151

5252
PHPAPI int _php_stream_mmap_unmap_ex(php_stream *stream, zend_off_t readden)

0 commit comments

Comments
 (0)