Replace is_callable checks with type hints

Also removes tests checking the exceptions thrown from
the removed is_callable checks.
This commit is contained in:
Mikael Pajunen 2015-04-12 22:00:20 +03:00 committed by Nicolas Grekas
parent 88e2d70df0
commit 4e0c6e1b55
13 changed files with 11 additions and 139 deletions

View File

@ -273,12 +273,8 @@ class Command
*
* @see execute()
*/
public function setCode($code)
public function setCode(callable $code)
{
if (!is_callable($code)) {
throw new InvalidArgumentException('Invalid callable provided to Command::setCode.');
}
if ($code instanceof \Closure) {
$r = new \ReflectionFunction($code);
if (null === $r->getClosureThis()) {

View File

@ -332,16 +332,6 @@ class CommandTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('interact called'.PHP_EOL.'from the code...'.PHP_EOL, $tester->getDisplay());
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Invalid callable provided to Command::setCode.
*/
public function testSetCodeWithNonCallable()
{
$command = new \TestCommand();
$command->setCode(array($this, 'nonExistentMethod'));
}
public function callableMethodCommand(InputInterface $input, OutputInterface $output)
{
$output->writeln('from the code...');

View File

@ -235,14 +235,9 @@ class ErrorHandler
* @param callable $handler A handler that will be called on Exception
*
* @return callable|null The previous exception handler
*
* @throws \InvalidArgumentException
*/
public function setExceptionHandler($handler)
public function setExceptionHandler(callable $handler = null)
{
if (null !== $handler && !is_callable($handler)) {
throw new \LogicException('The exception handler must be a valid PHP callable.');
}
$prev = $this->exceptionHandler;
$this->exceptionHandler = $handler;

View File

@ -72,11 +72,8 @@ class ExceptionHandler
*
* @return callable|null The previous exception handler if any
*/
public function setHandler($handler)
public function setHandler(callable $handler = null)
{
if (null !== $handler && !is_callable($handler)) {
throw new \LogicException('The exception handler must be a valid PHP callable.');
}
$old = $this->handler;
$this->handler = $handler;

View File

@ -35,18 +35,9 @@ class CallbackTransformer implements DataTransformerInterface
*
* @param callable $transform The forward transform callback
* @param callable $reverseTransform The reverse transform callback
*
* @throws \InvalidArgumentException when the given callbacks is invalid
*/
public function __construct($transform, $reverseTransform)
public function __construct(callable $transform, callable $reverseTransform)
{
if (!is_callable($transform)) {
throw new \InvalidArgumentException('Argument 1 should be a callable');
}
if (!is_callable($reverseTransform)) {
throw new \InvalidArgumentException('Argument 2 should be a callable');
}
$this->transform = $transform;
$this->reverseTransform = $reverseTransform;
}

View File

@ -11,8 +11,6 @@
namespace Symfony\Component\Form\ChoiceList;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
/**
* A list of choices with arbitrary data types.
*
@ -64,12 +62,8 @@ class ArrayChoiceList implements ChoiceListInterface
* incrementing integers are used as
* values
*/
public function __construct($choices, $value = null)
public function __construct($choices, callable $value = null)
{
if (null !== $value && !is_callable($value)) {
throw new UnexpectedTypeException($value, 'null or callable');
}
if ($choices instanceof \Traversable) {
$choices = iterator_to_array($choices);
}

View File

@ -25,22 +25,4 @@ class CallbackTransformerTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('foo has been transformed', $transformer->transform('foo'));
$this->assertEquals('bar has reversely been transformed', $transformer->reverseTransform('bar'));
}
/**
* @dataProvider invalidCallbacksProvider
*
* @expectedException \InvalidArgumentException
*/
public function testConstructorWithInvalidCallbacks($transformCallback, $reverseTransformCallback)
{
new CallbackTransformer($transformCallback, $reverseTransformCallback);
}
public function invalidCallbacksProvider()
{
return array(
array(null, function () {}),
array(function () {}, null),
);
}
}

View File

@ -42,14 +42,6 @@ class ArrayChoiceListTest extends AbstractChoiceListTest
return array('0', '1', '2', '3', '4', '5', '6');
}
/**
* @expectedException \Symfony\Component\Form\Exception\InvalidArgumentException
*/
public function testFailIfKeyMismatch()
{
new ArrayChoiceList(array(0 => 'a', 1 => 'b'), array(1 => 'a', 2 => 'b'));
}
public function testCreateChoiceListWithValueCallback()
{
$callback = function ($choice) {

View File

@ -64,14 +64,9 @@ class StreamedResponse extends Response
* Sets the PHP callback associated with this Response.
*
* @param callable $callback A valid PHP callback
*
* @throws \LogicException
*/
public function setCallback($callback)
public function setCallback(callable $callback)
{
if (!is_callable($callback)) {
throw new \LogicException('The Response callback must be a valid PHP callable.');
}
$this->callback = $callback;
}

View File

@ -87,15 +87,6 @@ class StreamedResponseTest extends \PHPUnit_Framework_TestCase
$response->sendContent();
}
/**
* @expectedException \LogicException
*/
public function testSetCallbackNonCallable()
{
$response = new StreamedResponse(null);
$response->setCallback(null);
}
/**
* @expectedException \LogicException
*/

View File

@ -58,47 +58,8 @@ class FilterControllerEvent extends KernelEvent
*
* @throws \LogicException
*/
public function setController($controller)
public function setController(callable $controller)
{
// controller must be a callable
if (!is_callable($controller)) {
throw new \LogicException(sprintf('The controller must be a callable (%s given).', $this->varToString($controller)));
}
$this->controller = $controller;
}
private function varToString($var)
{
if (is_object($var)) {
return sprintf('Object(%s)', get_class($var));
}
if (is_array($var)) {
$a = array();
foreach ($var as $k => $v) {
$a[] = sprintf('%s => %s', $k, $this->varToString($v));
}
return sprintf('Array(%s)', implode(', ', $a));
}
if (is_resource($var)) {
return sprintf('Resource(%s)', get_resource_type($var));
}
if (null === $var) {
return 'null';
}
if (false === $var) {
return 'false';
}
if (true === $var) {
return 'true';
}
return (string) $var;
}
}

View File

@ -86,15 +86,9 @@ abstract class AbstractNormalizer extends SerializerAwareNormalizer implements N
* @param callable $circularReferenceHandler
*
* @return self
*
* @throws InvalidArgumentException
*/
public function setCircularReferenceHandler($circularReferenceHandler)
public function setCircularReferenceHandler(callable $circularReferenceHandler)
{
if (!is_callable($circularReferenceHandler)) {
throw new InvalidArgumentException('The given circular reference handler is not callable.');
}
$this->circularReferenceHandler = $circularReferenceHandler;
return $this;

View File

@ -189,12 +189,10 @@ class PluralizationRules
/**
* Overrides the default plural rule for a given locale.
*
* @param string $rule A PHP callable
* @param string $locale The locale
*
* @throws \LogicException
* @param callable $rule A PHP callable
* @param string $locale The locale
*/
public static function set($rule, $locale)
public static function set(callable $rule, $locale)
{
if ('pt_BR' === $locale) {
// temporary set a locale for brazilian
@ -205,10 +203,6 @@ class PluralizationRules
$locale = substr($locale, 0, -strlen(strrchr($locale, '_')));
}
if (!is_callable($rule)) {
throw new \LogicException('The given rule can not be called');
}
self::$rules[$locale] = $rule;
}
}