Skip to content

Commit 0a130ca

Browse files
committed
ext/tidy: refactor php_tidy_set_tidy_opt()
Bring closer to unique call site and return value of it
1 parent 01dbee8 commit 0a130ca

File tree

3 files changed

+54
-41
lines changed

3 files changed

+54
-41
lines changed

ext/tidy/tests/031.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ tidy
77
--FILE--
88
<?php
99
$buffer = '<html></html>';
10-
$config = array('doctype' => 'php');
10+
$config = array('doctype' => 'auto');
1111

1212
$tidy = tidy_parse_string($buffer, $config);
1313
var_dump(tidy_config_count($tidy));

ext/tidy/tests/tidy_error1.phpt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,15 @@ try {
3232
echo $e::class, ": ", $e->getMessage(), PHP_EOL;
3333
}
3434

35-
$config = ['doctype' => 'php', 0 => 'value2'];
35+
$config = ['doctype' => 'php'];
36+
37+
try {
38+
var_dump($tidy->parseString($buffer, $config));
39+
} catch (\TypeError $e) {
40+
echo $e::class, ": ", $e->getMessage(), PHP_EOL;
41+
}
42+
43+
$config = ['doctype' => 'auto', 0 => 'value2'];
3644

3745
try {
3846
var_dump($tidy->parseString($buffer, $config));
@@ -45,4 +53,5 @@ try {
4553
ValueError: tidy::parseString(): Argument #2 ($config) Unknown Tidy configuration option "bogus"
4654
TypeError: tidy::parseString(): Argument #2 ($config) must be of type array with keys as string
4755
ValueError: tidy::parseString(): Argument #2 ($config) Attempting to set read-only option "doctype-mode"
56+
TypeError: tidy::parseString(): Argument #2 ($config) option "doctype" does not accept "php" as a value
4857
TypeError: tidy::parseString(): Argument #2 ($config) must be of type array with keys as string

ext/tidy/tidy.c

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ static void tidy_doc_update_properties(PHPTidyObj *);
124124
static void tidy_add_node_default_properties(PHPTidyObj *);
125125
static void *php_tidy_get_opt_val(const PHPTidyDoc *, TidyOption, TidyOptionType *);
126126
static void php_tidy_create_node(INTERNAL_FUNCTION_PARAMETERS, tidy_base_nodetypes);
127-
static zend_result _php_tidy_set_tidy_opt(TidyDoc, const char *, zval *, uint32_t arg);
128127
static zend_result _php_tidy_apply_config_array(TidyDoc doc, const HashTable *ht_options, uint32_t arg);
129128
static PHP_INI_MH(php_tidy_set_clean_output);
130129
static void php_tidy_clean_output_start(const char *name, size_t name_len);
@@ -216,42 +215,6 @@ static zend_result php_tidy_apply_config(TidyDoc doc, const zend_string *str_str
216215
return SUCCESS;
217216
}
218217

219-
static zend_result _php_tidy_set_tidy_opt(TidyDoc doc, const char *optname, zval *value, uint32_t arg)
220-
{
221-
TidyOption opt = tidyGetOptionByName(doc, optname);
222-
zend_long lval;
223-
224-
if (!opt) {
225-
zend_argument_value_error(arg, "Unknown Tidy configuration option \"%s\"", optname);
226-
return FAILURE;
227-
}
228-
229-
#if defined(HAVE_TIDYOPTGETCATEGORY)
230-
if (tidyOptGetCategory(opt) == TidyInternalCategory) {
231-
#else
232-
if (tidyOptIsReadOnly(opt)) {
233-
#endif
234-
zend_argument_value_error(arg, "Attempting to set read-only option \"%s\"", optname);
235-
return FAILURE;
236-
}
237-
238-
TidyOptionType type = tidyOptGetType(opt);
239-
if (type == TidyString) {
240-
zend_string *tmp_str;
241-
const zend_string *str = zval_get_tmp_string(value, &tmp_str);
242-
const bool result = tidyOptSetValue(doc, tidyOptGetId(opt), ZSTR_VAL(str));
243-
zend_tmp_string_release(tmp_str);
244-
return result ? SUCCESS : FAILURE;
245-
} else if (type == TidyInteger) {
246-
lval = zval_get_long(value);
247-
return tidyOptSetInt(doc, tidyOptGetId(opt), lval) ? SUCCESS : FAILURE;
248-
} else {
249-
ZEND_ASSERT(type == TidyBoolean);
250-
lval = zval_get_long(value);
251-
return tidyOptSetBool(doc, tidyOptGetId(opt), lval) ? SUCCESS : FAILURE;
252-
}
253-
}
254-
255218
static void tidy_create_node_object(zval *zv, PHPTidyDoc *ptdoc, TidyNode node)
256219
{
257220
object_init_ex(zv, tidy_ce_node);
@@ -751,20 +714,61 @@ static void php_tidy_create_node(INTERNAL_FUNCTION_PARAMETERS, tidy_base_nodetyp
751714
tidy_create_node_object(return_value, obj->ptdoc, node);
752715
}
753716

717+
718+
static bool php_tidy_set_tidy_opt(TidyDoc doc, const char *optname, zval *value, uint32_t arg)
719+
{
720+
TidyOption opt = tidyGetOptionByName(doc, optname);
721+
zend_long lval;
722+
723+
if (!opt) {
724+
zend_argument_value_error(arg, "Unknown Tidy configuration option \"%s\"", optname);
725+
return false;
726+
}
727+
728+
#if defined(HAVE_TIDYOPTGETCATEGORY)
729+
if (tidyOptGetCategory(opt) == TidyInternalCategory) {
730+
#else
731+
if (tidyOptIsReadOnly(opt)) {
732+
#endif
733+
zend_argument_value_error(arg, "Attempting to set read-only option \"%s\"", optname);
734+
return false;
735+
}
736+
737+
TidyOptionType type = tidyOptGetType(opt);
738+
if (type == TidyString) {
739+
zend_string *tmp_str;
740+
const zend_string *str = zval_get_tmp_string(value, &tmp_str);
741+
const bool result = tidyOptSetValue(doc, tidyOptGetId(opt), ZSTR_VAL(str));
742+
if (UNEXPECTED(!result)) {
743+
zend_argument_type_error(arg, "option \"%s\" does not accept \"%s\" as a value", optname, ZSTR_VAL(str));
744+
}
745+
zend_tmp_string_release(tmp_str);
746+
return result;
747+
} else if (type == TidyInteger) {
748+
lval = zval_get_long(value);
749+
return tidyOptSetInt(doc, tidyOptGetId(opt), lval);
750+
} else {
751+
ZEND_ASSERT(type == TidyBoolean);
752+
lval = zval_get_long(value);
753+
return tidyOptSetBool(doc, tidyOptGetId(opt), lval);
754+
}
755+
}
756+
754757
static zend_result _php_tidy_apply_config_array(TidyDoc doc, const HashTable *ht_options, uint32_t arg)
755758
{
756759
zval *opt_val;
757760
zend_string *opt_name;
758761

759762
if (!HT_IS_PACKED(ht_options)) {
763+
bool has_failures = false;
760764
ZEND_HASH_MAP_FOREACH_STR_KEY_VAL(ht_options, opt_name, opt_val) {
761765
if (opt_name == NULL) {
762766
zend_argument_type_error(arg, "must be of type array with keys as string");
763767
return FAILURE;
764768
}
765-
_php_tidy_set_tidy_opt(doc, ZSTR_VAL(opt_name), opt_val, arg);
769+
has_failures = has_failures || !php_tidy_set_tidy_opt(doc, ZSTR_VAL(opt_name), opt_val, arg);
766770
} ZEND_HASH_FOREACH_END();
767-
return SUCCESS;
771+
return has_failures ? FAILURE : SUCCESS;
768772
} else {
769773
zend_argument_type_error(arg, "must be of type array with keys as string");
770774
return FAILURE;

0 commit comments

Comments
 (0)