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"; } diff --git a/tests/MethodTest.php b/tests/MethodTest.php index f077ffb..883b887 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 methods default values + */ + public function testDefaultSpecialChars() + { + $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; + } }