Skip to content

Commit

Permalink
Replace is_callable checks with type hints
Browse files Browse the repository at this point in the history
Also removes tests checking the exceptions thrown from
the removed is_callable checks.
  • Loading branch information
mpajunen authored and nicolas-grekas committed Oct 5, 2015
1 parent 88e2d70 commit 4e0c6e1
Show file tree
Hide file tree
Showing 13 changed files with 11 additions and 139 deletions.
6 changes: 1 addition & 5 deletions src/Symfony/Component/Console/Command/Command.php
Expand Up @@ -273,12 +273,8 @@ public function run(InputInterface $input, OutputInterface $output)
*
* @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()) {
Expand Down
10 changes: 0 additions & 10 deletions src/Symfony/Component/Console/Tests/Command/CommandTest.php
Expand Up @@ -332,16 +332,6 @@ public function testSetCodeWithNonClosureCallable()
$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...');
Expand Down
7 changes: 1 addition & 6 deletions src/Symfony/Component/Debug/ErrorHandler.php
Expand Up @@ -235,14 +235,9 @@ public function setLoggers(array $loggers)
* @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;

Expand Down
5 changes: 1 addition & 4 deletions src/Symfony/Component/Debug/ExceptionHandler.php
Expand Up @@ -72,11 +72,8 @@ public static function register($debug = true, $charset = null, $fileLinkFormat
*
* @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;

Expand Down
11 changes: 1 addition & 10 deletions src/Symfony/Component/Form/CallbackTransformer.php
Expand Up @@ -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;
}
Expand Down
8 changes: 1 addition & 7 deletions src/Symfony/Component/Form/ChoiceList/ArrayChoiceList.php
Expand Up @@ -11,8 +11,6 @@

namespace Symfony\Component\Form\ChoiceList;

use Symfony\Component\Form\Exception\UnexpectedTypeException;

/**
* A list of choices with arbitrary data types.
*
Expand Down Expand Up @@ -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);
}
Expand Down
18 changes: 0 additions & 18 deletions src/Symfony/Component/Form/Tests/CallbackTransformerTest.php
Expand Up @@ -25,22 +25,4 @@ function ($value) { return $value.' has reversely been transformed'; }
$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),
);
}
}
Expand Up @@ -42,14 +42,6 @@ protected function getValues()
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) {
Expand Down
7 changes: 1 addition & 6 deletions src/Symfony/Component/HttpFoundation/StreamedResponse.php
Expand Up @@ -64,14 +64,9 @@ public static function create($callback = null, $status = 200, $headers = array(
* 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;
}

Expand Down
Expand Up @@ -87,15 +87,6 @@ public function testSendContentWithNonCallable()
$response->sendContent();
}

/**
* @expectedException \LogicException
*/
public function testSetCallbackNonCallable()
{
$response = new StreamedResponse(null);
$response->setCallback(null);
}

/**
* @expectedException \LogicException
*/
Expand Down
41 changes: 1 addition & 40 deletions src/Symfony/Component/HttpKernel/Event/FilterControllerEvent.php
Expand Up @@ -58,47 +58,8 @@ public function getController()
*
* @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;
}
}
Expand Up @@ -86,15 +86,9 @@ public function setCircularReferenceLimit($circularReferenceLimit)
* @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;
Expand Down
12 changes: 3 additions & 9 deletions src/Symfony/Component/Translation/PluralizationRules.php
Expand Up @@ -189,12 +189,10 @@ public static function get($number, $locale)
/**
* 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
Expand All @@ -205,10 +203,6 @@ public static function set($rule, $locale)
$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;
}
}

0 comments on commit 4e0c6e1

Please sign in to comment.