From 06b07aa7cf9631a04a76be708f9ff7a62c662e8a Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Mon, 1 Jul 2013 09:11:56 -0500 Subject: [PATCH 1/3] [zendframework/zf2#4761] Added tests for null values - null values should be filtered without warnings --- test/BaseNameTest.php | 10 ++++++++++ test/DigitsTest.php | 10 ++++++++++ test/HtmlEntitiesTest.php | 9 +++++++++ test/IntTest.php | 10 ++++++++++ test/RealPathTest.php | 9 +++++++++ test/StringToLowerTest.php | 9 +++++++++ test/StringToUpperTest.php | 9 +++++++++ test/StripTagsTest.php | 9 +++++++++ 8 files changed, 75 insertions(+) diff --git a/test/BaseNameTest.php b/test/BaseNameTest.php index 8bed8781..2b55bc40 100644 --- a/test/BaseNameTest.php +++ b/test/BaseNameTest.php @@ -56,4 +56,14 @@ public function testWarningIsRaisedIfArrayUsed() $this->assertInstanceOf('ErrorException', $err); $this->assertContains('cannot filter', $err->getMessage()); } + + /** + * @return void + */ + public function testReturnsNullIfNullIsUsed() + { + $filter = new BaseNameFilter(); + $filtered = $filter->filter(null); + $this->assertNull($filtered); + } } diff --git a/test/DigitsTest.php b/test/DigitsTest.php index a399869e..d5dd10d6 100644 --- a/test/DigitsTest.php +++ b/test/DigitsTest.php @@ -105,4 +105,14 @@ public function testWarningIsRaisedIfArrayUsed() $this->assertInstanceOf('ErrorException', $err); $this->assertContains('cannot filter', $err->getMessage()); } + + /** + * @return void + */ + public function testReturnsNullIfNullIsUsed() + { + $filter = new DigitsFilter(); + $filtered = $filter->filter(null); + $this->assertNull($filtered); + } } diff --git a/test/HtmlEntitiesTest.php b/test/HtmlEntitiesTest.php index 6e32f12d..6bff9684 100644 --- a/test/HtmlEntitiesTest.php +++ b/test/HtmlEntitiesTest.php @@ -277,6 +277,15 @@ public function testWarningIsRaisedIfArrayUsed() $this->assertContains('cannot filter', $err->getMessage()); } + /** + * @return void + */ + public function testReturnsNullIfNullIsUsed() + { + $filtered = $this->_filter->filter(null); + $this->assertNull($filtered); + } + /** * Null error handler; used when wanting to ignore specific error types */ diff --git a/test/IntTest.php b/test/IntTest.php index ff380c99..04217c2a 100644 --- a/test/IntTest.php +++ b/test/IntTest.php @@ -61,4 +61,14 @@ public function testWarningIsRaisedIfArrayUsed() $this->assertInstanceOf('ErrorException', $err); $this->assertContains('cannot filter', $err->getMessage()); } + + /** + * @return void + */ + public function testReturnsNullIfNullIsUsed() + { + $filter = new IntFilter(); + $filtered = $filter->filter(null); + $this->assertNull($filtered); + } } diff --git a/test/RealPathTest.php b/test/RealPathTest.php index 1786fce2..8e0a8ebd 100644 --- a/test/RealPathTest.php +++ b/test/RealPathTest.php @@ -129,4 +129,13 @@ public function testWarningIsRaisedIfArrayUsed() $this->assertInstanceOf('ErrorException', $err); $this->assertContains('cannot filter', $err->getMessage()); } + + /** + * @return void + */ + public function testReturnsNullIfNullIsUsed() + { + $filtered = $this->_filter->filter(null); + $this->assertNull($filtered); + } } diff --git a/test/StringToLowerTest.php b/test/StringToLowerTest.php index 9288ea36..0dfab8fd 100644 --- a/test/StringToLowerTest.php +++ b/test/StringToLowerTest.php @@ -177,4 +177,13 @@ public function testWarningIsRaisedIfArrayUsed() $this->assertInstanceOf('ErrorException', $err); $this->assertContains('cannot filter', $err->getMessage()); } + + /** + * @return void + */ + public function testReturnsNullIfNullIsUsed() + { + $filtered = $this->_filter->filter(null); + $this->assertNull($filtered); + } } diff --git a/test/StringToUpperTest.php b/test/StringToUpperTest.php index 08b2af6c..f8729d74 100644 --- a/test/StringToUpperTest.php +++ b/test/StringToUpperTest.php @@ -177,4 +177,13 @@ public function testWarningIsRaisedIfArrayUsed() $this->assertInstanceOf('ErrorException', $err); $this->assertContains('cannot filter', $err->getMessage()); } + + /** + * @return void + */ + public function testReturnsNullIfNullIsUsed() + { + $filtered = $this->_filter->filter(null); + $this->assertNull($filtered); + } } diff --git a/test/StripTagsTest.php b/test/StripTagsTest.php index 2807c0a4..506b7c18 100644 --- a/test/StripTagsTest.php +++ b/test/StripTagsTest.php @@ -557,4 +557,13 @@ public function testWarningIsRaisedIfArrayUsed() $this->assertInstanceOf('ErrorException', $err); $this->assertContains('cannot filter', $err->getMessage()); } + + /** + * @return void + */ + public function testReturnsNullIfNullIsUsed() + { + $filtered = $this->_filter->filter(null); + $this->assertNull($filtered); + } } From a0143d37d4a16dc0d3dd2e232cdfd0d0bcc0cac3 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Mon, 1 Jul 2013 09:14:04 -0500 Subject: [PATCH 2/3] [zendframework/zf2#4761] null values should not raise warnings - null values should not be filtered, and should not raise warnings --- src/BaseName.php | 6 +++++- src/Digits.php | 6 +++++- src/HtmlEntities.php | 6 +++++- src/Int.php | 6 +++++- src/RealPath.php | 6 +++++- src/StringToLower.php | 6 +++++- src/StringToUpper.php | 6 +++++- src/StripTags.php | 6 +++++- 8 files changed, 40 insertions(+), 8 deletions(-) diff --git a/src/BaseName.php b/src/BaseName.php index c8c71622..9d36c25c 100644 --- a/src/BaseName.php +++ b/src/BaseName.php @@ -21,7 +21,11 @@ class BaseName extends AbstractFilter */ public function filter($value) { - if(!is_scalar($value)){ + if (null === $value) { + return null; + } + + if (!is_scalar($value)){ trigger_error( sprintf( '%s expects parameter to be scalar, "%s" given; cannot filter', diff --git a/src/Digits.php b/src/Digits.php index 6cc351c7..33116d98 100644 --- a/src/Digits.php +++ b/src/Digits.php @@ -23,7 +23,11 @@ class Digits extends AbstractFilter */ public function filter($value) { - if(!is_scalar($value)){ + if (null === $value) { + return null; + } + + if (!is_scalar($value)){ trigger_error( sprintf( '%s expects parameter to be scalar, "%s" given; cannot filter', diff --git a/src/HtmlEntities.php b/src/HtmlEntities.php index 8a78ec90..8837d3fa 100644 --- a/src/HtmlEntities.php +++ b/src/HtmlEntities.php @@ -179,7 +179,11 @@ public function setDoubleQuote($doubleQuote) */ public function filter($value) { - if(!is_scalar($value)){ + if (null === $value) { + return null; + } + + if (!is_scalar($value)){ trigger_error( sprintf( '%s expects parameter to be scalar, "%s" given; cannot filter', diff --git a/src/Int.php b/src/Int.php index 01a8fb77..220157de 100644 --- a/src/Int.php +++ b/src/Int.php @@ -21,7 +21,11 @@ class Int extends AbstractFilter */ public function filter($value) { - if(!is_scalar($value)){ + if (null === $value) { + return null; + } + + if (!is_scalar($value)){ trigger_error( sprintf( '%s expects parameter to be scalar, "%s" given; cannot filter', diff --git a/src/RealPath.php b/src/RealPath.php index 1870c8be..c992b154 100644 --- a/src/RealPath.php +++ b/src/RealPath.php @@ -71,7 +71,11 @@ public function getExists() */ public function filter($value) { - if(!is_scalar($value)){ + if (null === $value) { + return null; + } + + if (!is_scalar($value)){ trigger_error( sprintf( '%s expects parameter to be scalar, "%s" given; cannot filter', diff --git a/src/StringToLower.php b/src/StringToLower.php index a4975670..21857e52 100644 --- a/src/StringToLower.php +++ b/src/StringToLower.php @@ -46,7 +46,11 @@ public function __construct($encodingOrOptions = null) */ public function filter($value) { - if(!is_scalar($value)){ + if (null === $value) { + return null; + } + + if (!is_scalar($value)){ trigger_error( sprintf( '%s expects parameter to be scalar, "%s" given; cannot filter', diff --git a/src/StringToUpper.php b/src/StringToUpper.php index 336ad3ea..920a5932 100644 --- a/src/StringToUpper.php +++ b/src/StringToUpper.php @@ -46,7 +46,11 @@ public function __construct($encodingOrOptions = null) */ public function filter($value) { - if(!is_scalar($value)){ + if (null === $value) { + return null; + } + + if (!is_scalar($value)){ trigger_error( sprintf( '%s expects parameter to be scalar, "%s" given; cannot filter', diff --git a/src/StripTags.php b/src/StripTags.php index 53d517d8..1cfa6294 100644 --- a/src/StripTags.php +++ b/src/StripTags.php @@ -173,7 +173,11 @@ public function setAttributesAllowed($attributesAllowed) */ public function filter($value) { - if(!is_scalar($value)){ + if (null === $value) { + return null; + } + + if (!is_scalar($value)){ trigger_error( sprintf( '%s expects parameter to be scalar, "%s" given; cannot filter', From d7a590bc85253e2c18d811c24a7fd86a67603c47 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Fri, 19 Jul 2013 08:22:01 -0500 Subject: [PATCH 3/3] Add info in docblock about warnings - Added a paragraph indicating inability to filter non-scalar values, and that a warning will be raised in such circumstances. - Updated return annotations to indicate a "mixed" possibility. --- src/BaseName.php | 7 +++++-- src/Digits.php | 5 ++++- src/HtmlEntities.php | 7 +++++-- src/Int.php | 5 ++++- src/RealPath.php | 5 ++++- src/StringToLower.php | 5 ++++- src/StringToUpper.php | 7 +++++-- src/StripTags.php | 6 ++++-- 8 files changed, 35 insertions(+), 12 deletions(-) diff --git a/src/BaseName.php b/src/BaseName.php index 9d36c25c..d3a0d0e0 100644 --- a/src/BaseName.php +++ b/src/BaseName.php @@ -14,10 +14,13 @@ class BaseName extends AbstractFilter /** * Defined by Zend\Filter\FilterInterface * - * Returns basename($value) + * Returns basename($value). + * + * If the value provided is non-scalar, the value will remain unfiltered + * and an E_USER_WARNING will be raised indicating it's unfilterable. * * @param string $value - * @return string + * @return string|mixed */ public function filter($value) { diff --git a/src/Digits.php b/src/Digits.php index 33116d98..15cfc55e 100644 --- a/src/Digits.php +++ b/src/Digits.php @@ -18,8 +18,11 @@ class Digits extends AbstractFilter * * Returns the string $value, removing all but digit characters * + * If the value provided is non-scalar, the value will remain unfiltered + * and an E_USER_WARNING will be raised indicating it's unfilterable. + * * @param string $value - * @return string + * @return string|mixed */ public function filter($value) { diff --git a/src/HtmlEntities.php b/src/HtmlEntities.php index 8837d3fa..c518e0f7 100644 --- a/src/HtmlEntities.php +++ b/src/HtmlEntities.php @@ -173,9 +173,12 @@ public function setDoubleQuote($doubleQuote) * Returns the string $value, converting characters to their corresponding HTML entity * equivalents where they exist * + * If the value provided is non-scalar, the value will remain unfiltered + * and an E_USER_WARNING will be raised indicating it's unfilterable. + * * @param string $value - * @return string - * @throws Exception\InvalidArgumentException + * @return string|mixed + * @throws Exception\DomainException on encoding mismatches */ public function filter($value) { diff --git a/src/Int.php b/src/Int.php index 220157de..e82e96d3 100644 --- a/src/Int.php +++ b/src/Int.php @@ -16,8 +16,11 @@ class Int extends AbstractFilter * * Returns (int) $value * + * If the value provided is non-scalar, the value will remain unfiltered + * and an E_USER_WARNING will be raised indicating it's unfilterable. + * * @param string $value - * @return int + * @return int|mixed */ public function filter($value) { diff --git a/src/RealPath.php b/src/RealPath.php index c992b154..2ac2564b 100644 --- a/src/RealPath.php +++ b/src/RealPath.php @@ -66,8 +66,11 @@ public function getExists() * * Returns realpath($value) * + * If the value provided is non-scalar, the value will remain unfiltered + * and an E_USER_WARNING will be raised indicating it's unfilterable. + * * @param string $value - * @return string + * @return string|mixed */ public function filter($value) { diff --git a/src/StringToLower.php b/src/StringToLower.php index 21857e52..5087ead3 100644 --- a/src/StringToLower.php +++ b/src/StringToLower.php @@ -41,8 +41,11 @@ public function __construct($encodingOrOptions = null) * * Returns the string $value, converting characters to lowercase as necessary * + * If the value provided is non-scalar, the value will remain unfiltered + * and an E_USER_WARNING will be raised indicating it's unfilterable. + * * @param string $value - * @return string + * @return string|mixed */ public function filter($value) { diff --git a/src/StringToUpper.php b/src/StringToUpper.php index 920a5932..07848165 100644 --- a/src/StringToUpper.php +++ b/src/StringToUpper.php @@ -39,10 +39,13 @@ public function __construct($encodingOrOptions = null) /** * Defined by Zend\Filter\FilterInterface * - * Returns the string $value, converting characters to lowercase as necessary + * Returns the string $value, converting characters to uppercase as necessary + * + * If the value provided is non-scalar, the value will remain unfiltered + * and an E_USER_WARNING will be raised indicating it's unfilterable. * * @param string $value - * @return string + * @return string|mixed */ public function filter($value) { diff --git a/src/StripTags.php b/src/StripTags.php index 1cfa6294..21073853 100644 --- a/src/StripTags.php +++ b/src/StripTags.php @@ -166,10 +166,12 @@ public function setAttributesAllowed($attributesAllowed) /** * Defined by Zend\Filter\FilterInterface * - * @todo improve docblock descriptions + * If the value provided is non-scalar, the value will remain unfiltered + * and an E_USER_WARNING will be raised indicating it's unfilterable. * + * @todo improve docblock descriptions * @param string $value - * @return string + * @return string|mixed */ public function filter($value) {