]> granicus.if.org Git - php/commitdiff
Promote warnings in ext/xsl
authorNikita Popov <nikita.ppv@gmail.com>
Mon, 28 Sep 2020 13:55:22 +0000 (15:55 +0200)
committerNikita Popov <nikita.ppv@gmail.com>
Mon, 28 Sep 2020 14:02:31 +0000 (16:02 +0200)
ext/xsl/tests/xsltprocessor_importStylesheet-invalidparam.phpt
ext/xsl/tests/xsltprocessor_transformToDoc_no_stylesheet.phpt [new file with mode: 0644]
ext/xsl/xsltprocessor.c

index 91bd07cf8a1216c8ca927fe4ab35be187332cac6..3849f69428aff37f9576b93a5edb4a2251e523ec 100644 (file)
@@ -11,9 +11,12 @@ if (!extension_loaded('xsl')) {
 
 $xslt = new XSLTProcessor();
 $dummy = new stdClass();
-var_dump($xslt->importStylesheet($dummy));
+try {
+    var_dump($xslt->importStylesheet($dummy));
+} catch (ValueError $e) {
+    echo $e->getMessage(), "\n";
+}
 
 ?>
---EXPECTF--
-Warning: Invalid Document in %s on line %d
-bool(false) 
+--EXPECT--
+XSLTProcessor::importStylesheet(): Argument #1 ($stylesheet) must be a valid XML node
diff --git a/ext/xsl/tests/xsltprocessor_transformToDoc_no_stylesheet.phpt b/ext/xsl/tests/xsltprocessor_transformToDoc_no_stylesheet.phpt
new file mode 100644 (file)
index 0000000..c590b69
--- /dev/null
@@ -0,0 +1,17 @@
+--TEST--
+Calling XSLTProcessor::transformToDoc() without stylesheet
+--FILE--
+<?php
+
+$doc = new DOMDocument('1.0', 'utf-8');
+
+$xsl = new XSLTProcessor;
+try {
+    $xsl->transformToDoc($doc);
+} catch (Error $e) {
+    echo $e->getMessage(), "\n";
+}
+
+?>
+--EXPECT--
+XSLTProcessor::transformToDoc() can only be called after a stylesheet has been imported
index d900d951033cb0179f708324f03ad831b63e1ac8..9498ba576c6e2571110403c51ca1e04c12c309cb 100644 (file)
@@ -66,27 +66,22 @@ static char **php_xsl_xslt_make_params(HashTable *parht, int xpath_params)
        memset((char *)params, 0, parsize);
 
        ZEND_HASH_FOREACH_STR_KEY_VAL(parht, string_key, value) {
-               if (string_key == NULL) {
-                       php_error_docref(NULL, E_WARNING, "Invalid argument or parameter array");
-                       efree(params);
-                       return NULL;
-               } else {
-                       if (Z_TYPE_P(value) != IS_STRING) {
-                               if (!try_convert_to_string(value)) {
-                                       efree(params);
-                                       return NULL;
-                               }
+               ZEND_ASSERT(string_key != NULL);
+               if (Z_TYPE_P(value) != IS_STRING) {
+                       if (!try_convert_to_string(value)) {
+                               efree(params);
+                               return NULL;
                        }
+               }
 
-                       if (!xpath_params) {
-                               xpath_expr = php_xsl_xslt_string_to_xpathexpr(Z_STRVAL_P(value));
-                       } else {
-                               xpath_expr = estrndup(Z_STRVAL_P(value), Z_STRLEN_P(value));
-                       }
-                       if (xpath_expr) {
-                               params[i++] = estrndup(ZSTR_VAL(string_key), ZSTR_LEN(string_key));
-                               params[i++] = xpath_expr;
-                       }
+               if (!xpath_params) {
+                       xpath_expr = php_xsl_xslt_string_to_xpathexpr(Z_STRVAL_P(value));
+               } else {
+                       xpath_expr = estrndup(Z_STRVAL_P(value), Z_STRLEN_P(value));
+               }
+               if (xpath_expr) {
+                       params[i++] = estrndup(ZSTR_VAL(string_key), ZSTR_LEN(string_key));
+                       params[i++] = xpath_expr;
                }
        } ZEND_HASH_FOREACH_END();
 
@@ -336,8 +331,8 @@ PHP_METHOD(XSLTProcessor, importStylesheet)
                doc = nodep->doc;
        }
        if (doc == NULL) {
-               php_error(E_WARNING, "Invalid Document");
-               RETURN_FALSE;
+               zend_argument_value_error(1, "must be a valid XML node");
+               RETURN_THROWS();
        }
 
        /* libxslt uses _private, so we must copy the imported
@@ -417,13 +412,17 @@ static xmlDocPtr php_xsl_apply_stylesheet(zval *id, xsl_object *intern, xsltStyl
        if (node) {
                doc = node->doc;
        }
+
        if (doc == NULL) {
-               php_error_docref(NULL, E_WARNING, "Invalid Document");
+               zend_argument_value_error(1, "must be a valid XML node");
                return NULL;
        }
 
        if (style == NULL) {
-               php_error_docref(NULL, E_WARNING, "No stylesheet associated to this object");
+               zend_string *name = get_active_function_or_method_name();
+               zend_throw_error(NULL, "%s() can only be called after a stylesheet has been imported",
+                       ZSTR_VAL(name));
+               zend_string_release(name);
                return NULL;
        }