Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Remove exceptions from #4734 #4761

Merged
merged 4 commits into from Jul 19, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 19 additions & 8 deletions library/Zend/Filter/BaseName.php
Expand Up @@ -14,19 +14,30 @@ class BaseName extends AbstractFilter
/** /**
* Defined by Zend\Filter\FilterInterface * 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 * @param string $value
* @return string * @return string|mixed
*/ */
public function filter($value) public function filter($value)
{ {
if(!is_scalar($value)){ if (null === $value) {
throw new Exception\InvalidArgumentException(sprintf( return null;
'%s expects parameter to be scalar, "%s" given', }
__METHOD__,
(is_object($value) ? get_class($value) : gettype($value)) if (!is_scalar($value)){
)); trigger_error(
sprintf(
'%s expects parameter to be scalar, "%s" given; cannot filter',
__METHOD__,
(is_object($value) ? get_class($value) : gettype($value))
),
E_USER_WARNING
);
return $value;
} }


return basename((string) $value); return basename((string) $value);
Expand Down
25 changes: 18 additions & 7 deletions library/Zend/Filter/Digits.php
Expand Up @@ -18,17 +18,28 @@ class Digits extends AbstractFilter
* *
* Returns the string $value, removing all but digit characters * 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 * @param string $value
* @return string * @return string|mixed
*/ */
public function filter($value) public function filter($value)
{ {
if(!is_scalar($value)){ if (null === $value) {
throw new Exception\InvalidArgumentException(sprintf( return null;
'%s expects parameter to be scalar, "%s" given', }
__METHOD__,
(is_object($value) ? get_class($value) : gettype($value)) if (!is_scalar($value)){
)); trigger_error(
sprintf(
'%s expects parameter to be scalar, "%s" given; cannot filter',
__METHOD__,
(is_object($value) ? get_class($value) : gettype($value))
),
E_USER_WARNING
);
return $value;
} }


if (!StringUtils::hasPcreUnicodeSupport()) { if (!StringUtils::hasPcreUnicodeSupport()) {
Expand Down
27 changes: 19 additions & 8 deletions library/Zend/Filter/HtmlEntities.php
Expand Up @@ -173,18 +173,29 @@ public function setDoubleQuote($doubleQuote)
* Returns the string $value, converting characters to their corresponding HTML entity * Returns the string $value, converting characters to their corresponding HTML entity
* equivalents where they exist * 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 * @param string $value
* @throws Exception\DomainException * @return string|mixed
* @return string * @throws Exception\DomainException on encoding mismatches
*/ */
public function filter($value) public function filter($value)
{ {
if(!is_scalar($value)){ if (null === $value) {
throw new Exception\InvalidArgumentException(sprintf( return null;
'%s expects parameter to be scalar, "%s" given', }
__METHOD__,
(is_object($value) ? get_class($value) : gettype($value)) if (!is_scalar($value)){
)); trigger_error(
sprintf(
'%s expects parameter to be scalar, "%s" given; cannot filter',
__METHOD__,
(is_object($value) ? get_class($value) : gettype($value))
),
E_USER_WARNING
);
return $value;
} }


$filtered = htmlentities((string) $value, $this->getQuoteStyle(), $this->getEncoding(), $this->getDoubleQuote()); $filtered = htmlentities((string) $value, $this->getQuoteStyle(), $this->getEncoding(), $this->getDoubleQuote());
Expand Down
25 changes: 18 additions & 7 deletions library/Zend/Filter/Int.php
Expand Up @@ -16,17 +16,28 @@ class Int extends AbstractFilter
* *
* Returns (int) $value * 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 * @param string $value
* @return int * @return int|mixed
*/ */
public function filter($value) public function filter($value)
{ {
if(!is_scalar($value)){ if (null === $value) {
throw new Exception\InvalidArgumentException(sprintf( return null;
'%s expects parameter to be scalar, "%s" given', }
__METHOD__,
(is_object($value) ? get_class($value) : gettype($value)) if (!is_scalar($value)){
)); trigger_error(
sprintf(
'%s expects parameter to be scalar, "%s" given; cannot filter',
__METHOD__,
(is_object($value) ? get_class($value) : gettype($value))
),
E_USER_WARNING
);
return $value;
} }


return (int) ((string) $value); return (int) ((string) $value);
Expand Down
25 changes: 18 additions & 7 deletions library/Zend/Filter/RealPath.php
Expand Up @@ -66,17 +66,28 @@ public function getExists()
* *
* Returns realpath($value) * 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 * @param string $value
* @return string * @return string|mixed
*/ */
public function filter($value) public function filter($value)
{ {
if(!is_scalar($value)){ if (null === $value) {
throw new Exception\InvalidArgumentException(sprintf( return null;
'%s expects parameter to be scalar, "%s" given', }
__METHOD__,
(is_object($value) ? get_class($value) : gettype($value)) if (!is_scalar($value)){
)); trigger_error(
sprintf(
'%s expects parameter to be scalar, "%s" given; cannot filter',
__METHOD__,
(is_object($value) ? get_class($value) : gettype($value))
),
E_USER_WARNING
);
return $value;
} }


$path = (string) $value; $path = (string) $value;
Expand Down
25 changes: 18 additions & 7 deletions library/Zend/Filter/StringToLower.php
Expand Up @@ -41,17 +41,28 @@ public function __construct($encodingOrOptions = null)
* *
* Returns the string $value, converting characters to lowercase as necessary * 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 * @param string $value
* @return string * @return string|mixed
*/ */
public function filter($value) public function filter($value)
{ {
if(!is_scalar($value)){ if (null === $value) {
throw new Exception\InvalidArgumentException(sprintf( return null;
'%s expects parameter to be scalar, "%s" given', }
__METHOD__,
(is_object($value) ? get_class($value) : gettype($value)) if (!is_scalar($value)){
)); trigger_error(
sprintf(
'%s expects parameter to be scalar, "%s" given; cannot filter',
__METHOD__,
(is_object($value) ? get_class($value) : gettype($value))
),
E_USER_WARNING
);
return $value;
} }


if ($this->options['encoding'] !== null) { if ($this->options['encoding'] !== null) {
Expand Down
27 changes: 19 additions & 8 deletions library/Zend/Filter/StringToUpper.php
Expand Up @@ -39,19 +39,30 @@ public function __construct($encodingOrOptions = null)
/** /**
* Defined by Zend\Filter\FilterInterface * 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 * @param string $value
* @return string * @return string|mixed
*/ */
public function filter($value) public function filter($value)
{ {
if(!is_scalar($value)){ if (null === $value) {
throw new Exception\InvalidArgumentException(sprintf( return null;
'%s expects parameter to be scalar, "%s" given', }
__METHOD__,
(is_object($value) ? get_class($value) : gettype($value)) if (!is_scalar($value)){
)); trigger_error(
sprintf(
'%s expects parameter to be scalar, "%s" given; cannot filter',
__METHOD__,
(is_object($value) ? get_class($value) : gettype($value))
),
E_USER_WARNING
);
return $value;
} }


if ($this->options['encoding'] !== null) { if ($this->options['encoding'] !== null) {
Expand Down
26 changes: 18 additions & 8 deletions library/Zend/Filter/StripTags.php
Expand Up @@ -166,19 +166,29 @@ public function setAttributesAllowed($attributesAllowed)
/** /**
* Defined by Zend\Filter\FilterInterface * 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 * @param string $value
* @return string * @return string|mixed
*/ */
public function filter($value) public function filter($value)
{ {
if(!is_scalar($value)){ if (null === $value) {
throw new Exception\InvalidArgumentException(sprintf( return null;
'%s expects parameter to be scalar, "%s" given', }
__METHOD__,
(is_object($value) ? get_class($value) : gettype($value)) if (!is_scalar($value)){
)); trigger_error(
sprintf(
'%s expects parameter to be scalar, "%s" given; cannot filter',
__METHOD__,
(is_object($value) ? get_class($value) : gettype($value))
),
E_USER_WARNING
);
return $value;
} }


$value = (string) $value; $value = (string) $value;
Expand Down
27 changes: 19 additions & 8 deletions tests/ZendTest/Filter/BaseNameTest.php
Expand Up @@ -11,6 +11,7 @@
namespace ZendTest\Filter; namespace ZendTest\Filter;


use Zend\Filter\BaseName as BaseNameFilter; use Zend\Filter\BaseName as BaseNameFilter;
use Zend\Stdlib\ErrorHandler;


/** /**
* @category Zend * @category Zend
Expand Down Expand Up @@ -38,21 +39,31 @@ public function testBasic()
} }


/** /**
* Ensures that an InvalidArgumentException is raised if array is used * Ensures that a warning is raised if array is used
* *
* @return void * @return void
*/ */
public function testExceptionRaisedIfArrayUsed() public function testWarningIsRaisedIfArrayUsed()
{ {
$filter = new BaseNameFilter(); $filter = new BaseNameFilter();
$input = array('/path/to/filename', '/path/to/filename.ext'); $input = array('/path/to/filename', '/path/to/filename.ext');


try { ErrorHandler::start(E_USER_WARNING);
$filter->filter($input); $filtered = $filter->filter($input);
} catch (\Zend\Filter\Exception\InvalidArgumentException $expected) { $err = ErrorHandler::stop();
return;
} $this->assertEquals($input, $filtered);
$this->assertInstanceOf('ErrorException', $err);
$this->assertContains('cannot filter', $err->getMessage());
}


$this->fail('An expected InvalidArgumentException has not been raised.'); /**
* @return void
*/
public function testReturnsNullIfNullIsUsed()
{
$filter = new BaseNameFilter();
$filtered = $filter->filter(null);
$this->assertNull($filtered);
} }
} }
27 changes: 19 additions & 8 deletions tests/ZendTest/Filter/DigitsTest.php
Expand Up @@ -11,6 +11,7 @@
namespace ZendTest\Filter; namespace ZendTest\Filter;


use Zend\Filter\Digits as DigitsFilter; use Zend\Filter\Digits as DigitsFilter;
use Zend\Stdlib\ErrorHandler;


/** /**
* @category Zend * @category Zend
Expand Down Expand Up @@ -87,21 +88,31 @@ public function testBasic()
} }


/** /**
* Ensures that an InvalidArgumentException is raised if array is used * Ensures that an error is raised if array is used
* *
* @return void * @return void
*/ */
public function testExceptionRaisedIfArrayUsed() public function testWarningIsRaisedIfArrayUsed()
{ {
$filter = new DigitsFilter(); $filter = new DigitsFilter();
$input = array('abc123', 'abc 123'); $input = array('abc123', 'abc 123');


try { ErrorHandler::start(E_USER_WARNING);
$filter->filter($input); $filtered = $filter->filter($input);
} catch (\Zend\Filter\Exception\InvalidArgumentException $expected) { $err = ErrorHandler::stop();
return;
} $this->assertEquals($input, $filtered);
$this->assertInstanceOf('ErrorException', $err);
$this->assertContains('cannot filter', $err->getMessage());
}


$this->fail('An expected InvalidArgumentException has not been raised.'); /**
* @return void
*/
public function testReturnsNullIfNullIsUsed()
{
$filter = new DigitsFilter();
$filtered = $filter->filter(null);
$this->assertNull($filtered);
} }
} }