Skip to content

Replace is_callable checks with type hints #16125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 6, 2015
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Symfony/Bridge/Twig/Command/DebugCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ private function getMetadata($type, $entity)
return;
}
$refl = new \ReflectionMethod($cb[0], $cb[1]);
} elseif (is_object($cb) && is_callable($cb)) {
} elseif (is_object($cb) && method_exists($cb, '__invoke')) {
$refl = new \ReflectionMethod($cb, '__invoke');
} elseif (function_exists($cb)) {
$refl = new \ReflectionFunction($cb);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<service id="debug.debug_handlers_listener" class="Symfony\Component\HttpKernel\EventListener\DebugHandlersListener">
<tag name="kernel.event_subscriber" />
<tag name="monolog.logger" channel="php" />
<argument /><!-- Exception handler -->
<argument>null</argument><!-- Exception handler -->
<argument type="service" id="logger" on-invalid="null" />
<argument>null</argument><!-- Log levels map for enabled error levels -->
<argument>null</argument>
Expand Down
6 changes: 1 addition & 5 deletions src/Symfony/Component/Console/Command/Command.php
Original file line number Diff line number Diff line change
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
6 changes: 3 additions & 3 deletions src/Symfony/Component/Console/Helper/ProcessHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class ProcessHelper extends Helper
*
* @return Process The process that ran
*/
public function run(OutputInterface $output, $cmd, $error = null, $callback = null, $verbosity = OutputInterface::VERBOSITY_VERY_VERBOSE)
public function run(OutputInterface $output, $cmd, $error = null, callable $callback = null, $verbosity = OutputInterface::VERBOSITY_VERY_VERBOSE)
{
if ($output instanceof ConsoleOutputInterface) {
$output = $output->getErrorOutput();
Expand Down Expand Up @@ -92,7 +92,7 @@ public function run(OutputInterface $output, $cmd, $error = null, $callback = nu
*
* @see run()
*/
public function mustRun(OutputInterface $output, $cmd, $error = null, $callback = null)
public function mustRun(OutputInterface $output, $cmd, $error = null, callable $callback = null)
{
$process = $this->run($output, $cmd, $error, $callback);

Expand All @@ -112,7 +112,7 @@ public function mustRun(OutputInterface $output, $cmd, $error = null, $callback
*
* @return callable
*/
public function wrapCallback(OutputInterface $output, Process $process, $callback = null)
public function wrapCallback(OutputInterface $output, Process $process, callable $callback = null)
{
if ($output instanceof ConsoleOutputInterface) {
$output = $output->getErrorOutput();
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Console/Helper/ProgressBar.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function __construct(OutputInterface $output, $max = 0)
* @param string $name The placeholder name (including the delimiter char like %)
* @param callable $callable A PHP callable
*/
public static function setPlaceholderFormatterDefinition($name, $callable)
public static function setPlaceholderFormatterDefinition($name, callable $callable)
{
if (!self::$formatters) {
self::$formatters = self::initPlaceholderFormatters();
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Console/Helper/QuestionHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ private function getHiddenResponse(OutputInterface $output, $inputStream)
*
* @throws \Exception In case the max number of attempts has been reached and no valid response has been given
*/
private function validateAttempts($interviewer, OutputInterface $output, Question $question)
private function validateAttempts(callable $interviewer, OutputInterface $output, Question $question)
{
$error = null;
$attempts = $question->getMaxAttempts();
Expand Down
8 changes: 4 additions & 4 deletions src/Symfony/Component/Console/Question/Question.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public function setAutocompleterValues($values)
*
* @return Question The current instance
*/
public function setValidator($validator)
public function setValidator(callable $validator = null)
{
$this->validator = $validator;

Expand Down Expand Up @@ -220,11 +220,11 @@ public function getMaxAttempts()
*
* The normalizer can be a callable (a string), a closure or a class implementing __invoke.
*
* @param string|\Closure $normalizer
* @param callable $normalizer
*
* @return Question The current instance
*/
public function setNormalizer($normalizer)
public function setNormalizer(callable $normalizer)
{
$this->normalizer = $normalizer;

Expand All @@ -236,7 +236,7 @@ public function setNormalizer($normalizer)
*
* The normalizer can ba a callable (a string), a closure or a class implementing __invoke.
*
* @return string|\Closure
* @return callable
*/
public function getNormalizer()
{
Expand Down
10 changes: 0 additions & 10 deletions src/Symfony/Component/Console/Tests/Command/CommandTest.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class ExpressionFunction
* @param callable $compiler A callable able to compile the function
* @param callable $evaluator A callable able to evaluate the function
*/
public function __construct($name, $compiler, $evaluator)
public function __construct($name, callable $compiler, callable $evaluator)
{
$this->name = $name;
$this->compiler = $compiler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public function parse($expression, $names)
*
* @see ExpressionFunction
*/
public function register($name, $compiler, $evaluator)
public function register($name, callable $compiler, callable $evaluator)
{
$this->functions[$name] = array('compiler' => $compiler, 'evaluator' => $evaluator);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ class CustomFilterIterator extends FilterIterator
/**
* Constructor.
*
* @param \Iterator $iterator The Iterator to filter
* @param array $filters An array of PHP callbacks
* @param \Iterator $iterator The Iterator to filter
* @param callable[] $filters An array of PHP callbacks
*
* @throws \InvalidArgumentException
*/
Expand Down
11 changes: 1 addition & 10 deletions src/Symfony/Component/Form/CallbackTransformer.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public static function toArrayKey($choice)
* the keys of the values or if any of the
* choices is not scalar
*/
public function __construct($choices, $value = null)
public function __construct($choices, callable $value = null)
{
// If no values are given, use the choices as values
// Since the choices are stored in the collection keys, i.e. they are
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Form/ChoiceList/LazyChoiceList.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class LazyChoiceList implements ChoiceListInterface
* @param null|callable $value The callable generating the choice
* values
*/
public function __construct(ChoiceLoaderInterface $loader, $value = null)
public function __construct(ChoiceLoaderInterface $loader, callable $value = null)
{
$this->loader = $loader;
$this->value = $value;
Expand Down
18 changes: 0 additions & 18 deletions src/Symfony/Component/Form/Tests/CallbackTransformerTest.php
Original file line number Diff line number Diff line change
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),
);
}
}
Original file line number Diff line number Diff line change
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
9 changes: 2 additions & 7 deletions src/Symfony/Component/HttpFoundation/StreamedResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class StreamedResponse extends Response
* @param int $status The response status code
* @param array $headers An array of response headers
*/
public function __construct($callback = null, $status = 200, $headers = array())
public function __construct(callable $callback = null, $status = 200, $headers = array())
{
parent::__construct(null, $status, $headers);

Expand Down 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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ protected function doGetArguments(Request $request, $controller, array $paramete
*
* @param string $controller A Controller string
*
* @return mixed A PHP callable
* @return callable A PHP callable
*
* @throws \InvalidArgumentException
*/
Expand Down
45 changes: 2 additions & 43 deletions src/Symfony/Component/HttpKernel/Event/FilterControllerEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,10 @@ class FilterControllerEvent extends KernelEvent
{
/**
* The current controller.
*
* @var callable
*/
private $controller;

public function __construct(HttpKernelInterface $kernel, $controller, Request $request, $requestType)
public function __construct(HttpKernelInterface $kernel, callable $controller, Request $request, $requestType)
{
parent::__construct($kernel, $request, $requestType);

Expand All @@ -58,47 +56,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;
}
}
Loading