From fe2890925eeacb2708c7f13204ea71fdca293183 Mon Sep 17 00:00:00 2001 From: Loilo Date: Wed, 25 Mar 2020 23:51:04 +0100 Subject: [PATCH 1/3] Prevent generation of invalid code for certain parameter default values --- src/Method.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Method.php b/src/Method.php index 028e1b7..7bc6b7e 100644 --- a/src/Method.php +++ b/src/Method.php @@ -329,7 +329,7 @@ class Method } elseif (is_resource($default)) { //skip to not fail } else { - $default = "'" . trim($default) . "'"; + $default = var_export($default, true); } $paramStr .= " = $default"; } From da59650fb2046ec7c3c5f36f0e246bb2ab166873 Mon Sep 17 00:00:00 2001 From: Loilo Date: Thu, 26 Mar 2020 09:37:33 +0100 Subject: [PATCH 2/3] Add test for method default values with special chars --- tests/MethodTest.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/MethodTest.php b/tests/MethodTest.php index f077ffb..80c2992 100644 --- a/tests/MethodTest.php +++ b/tests/MethodTest.php @@ -47,6 +47,21 @@ class ExampleTest extends TestCase $this->assertEquals(['$last', '$first = \'Barry\'', '...$middle'], $method->getParamsWithDefault(false)); $this->assertEquals(true, $method->shouldReturn()); } + + /** + * Test special characters in method + */ + public function testSpecialChars() + { + $reflectionClass = new \ReflectionClass(ExampleClass::class); + $reflectionMethod = $reflectionClass->getMethod('setSpecialChars'); + + $method = new Method($reflectionMethod, 'Example', $reflectionClass); + $this->assertEquals('$chars', $method->getParams(true)); + $this->assertEquals(['$chars'], $method->getParams(false)); + $this->assertEquals('$chars = \'$\\\'\\\\\'', $method->getParamsWithDefault(true)); + $this->assertEquals(['$chars = \'$\\\'\\\\\''], $method->getParamsWithDefault(false)); + } } class ExampleClass @@ -60,4 +75,9 @@ class ExampleClass { return; } + + public function setSpecialChars($chars = "\$'\\") + { + return; + } } From 3cc7d7ccf66ee14ef18496b43b65dff31863a995 Mon Sep 17 00:00:00 2001 From: Florian Reuschel Date: Tue, 31 Mar 2020 21:19:33 +0200 Subject: [PATCH 3/3] Improve test method doc block Co-Authored-By: Markus Podar --- tests/MethodTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/MethodTest.php b/tests/MethodTest.php index 80c2992..883b887 100644 --- a/tests/MethodTest.php +++ b/tests/MethodTest.php @@ -49,9 +49,9 @@ class ExampleTest extends TestCase } /** - * Test special characters in method + * Test special characters in methods default values */ - public function testSpecialChars() + public function testDefaultSpecialChars() { $reflectionClass = new \ReflectionClass(ExampleClass::class); $reflectionMethod = $reflectionClass->getMethod('setSpecialChars');