Add more callable type hints

This commit is contained in:
Nicolas Grekas 2015-10-05 18:52:37 +02:00
parent 4e0c6e1b55
commit 7685cddf95
23 changed files with 36 additions and 54 deletions

View File

@ -149,7 +149,7 @@ EOF
return; return;
} }
$refl = new \ReflectionMethod($cb[0], $cb[1]); $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'); $refl = new \ReflectionMethod($cb, '__invoke');
} elseif (function_exists($cb)) { } elseif (function_exists($cb)) {
$refl = new \ReflectionFunction($cb); $refl = new \ReflectionFunction($cb);

View File

@ -12,7 +12,7 @@
<service id="debug.debug_handlers_listener" class="Symfony\Component\HttpKernel\EventListener\DebugHandlersListener"> <service id="debug.debug_handlers_listener" class="Symfony\Component\HttpKernel\EventListener\DebugHandlersListener">
<tag name="kernel.event_subscriber" /> <tag name="kernel.event_subscriber" />
<tag name="monolog.logger" channel="php" /> <tag name="monolog.logger" channel="php" />
<argument /><!-- Exception handler --> <argument>null</argument><!-- Exception handler -->
<argument type="service" id="logger" on-invalid="null" /> <argument type="service" id="logger" on-invalid="null" />
<argument>null</argument><!-- Log levels map for enabled error levels --> <argument>null</argument><!-- Log levels map for enabled error levels -->
<argument>null</argument> <argument>null</argument>

View File

@ -36,7 +36,7 @@ class ProcessHelper extends Helper
* *
* @return Process The process that ran * @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) { if ($output instanceof ConsoleOutputInterface) {
$output = $output->getErrorOutput(); $output = $output->getErrorOutput();
@ -92,7 +92,7 @@ class ProcessHelper extends Helper
* *
* @see run() * @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); $process = $this->run($output, $cmd, $error, $callback);
@ -112,7 +112,7 @@ class ProcessHelper extends Helper
* *
* @return callable * @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) { if ($output instanceof ConsoleOutputInterface) {
$output = $output->getErrorOutput(); $output = $output->getErrorOutput();

View File

@ -86,7 +86,7 @@ class ProgressBar
* @param string $name The placeholder name (including the delimiter char like %) * @param string $name The placeholder name (including the delimiter char like %)
* @param callable $callable A PHP callable * @param callable $callable A PHP callable
*/ */
public static function setPlaceholderFormatterDefinition($name, $callable) public static function setPlaceholderFormatterDefinition($name, callable $callable)
{ {
if (!self::$formatters) { if (!self::$formatters) {
self::$formatters = self::initPlaceholderFormatters(); self::$formatters = self::initPlaceholderFormatters();

View File

@ -377,7 +377,7 @@ class QuestionHelper extends Helper
* *
* @throws \Exception In case the max number of attempts has been reached and no valid response has been given * @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; $error = null;
$attempts = $question->getMaxAttempts(); $attempts = $question->getMaxAttempts();

View File

@ -164,7 +164,7 @@ class Question
* *
* @return Question The current instance * @return Question The current instance
*/ */
public function setValidator($validator) public function setValidator(callable $validator = null)
{ {
$this->validator = $validator; $this->validator = $validator;
@ -220,11 +220,11 @@ class Question
* *
* The normalizer can be a callable (a string), a closure or a class implementing __invoke. * 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 * @return Question The current instance
*/ */
public function setNormalizer($normalizer) public function setNormalizer(callable $normalizer)
{ {
$this->normalizer = $normalizer; $this->normalizer = $normalizer;
@ -236,7 +236,7 @@ class Question
* *
* The normalizer can ba a callable (a string), a closure or a class implementing __invoke. * The normalizer can ba a callable (a string), a closure or a class implementing __invoke.
* *
* @return string|\Closure * @return callable
*/ */
public function getNormalizer() public function getNormalizer()
{ {

View File

@ -41,7 +41,7 @@ class ExpressionFunction
* @param callable $compiler A callable able to compile the function * @param callable $compiler A callable able to compile the function
* @param callable $evaluator A callable able to evaluate 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->name = $name;
$this->compiler = $compiler; $this->compiler = $compiler;

View File

@ -112,7 +112,7 @@ class ExpressionLanguage
* *
* @see ExpressionFunction * @see ExpressionFunction
*/ */
public function register($name, $compiler, $evaluator) public function register($name, callable $compiler, callable $evaluator)
{ {
$this->functions[$name] = array('compiler' => $compiler, 'evaluator' => $evaluator); $this->functions[$name] = array('compiler' => $compiler, 'evaluator' => $evaluator);
} }

View File

@ -26,8 +26,8 @@ class CustomFilterIterator extends FilterIterator
/** /**
* Constructor. * Constructor.
* *
* @param \Iterator $iterator The Iterator to filter * @param \Iterator $iterator The Iterator to filter
* @param array $filters An array of PHP callbacks * @param callable[] $filters An array of PHP callbacks
* *
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
*/ */

View File

@ -101,7 +101,7 @@ class ArrayKeyChoiceList extends ArrayChoiceList
* the keys of the values or if any of the * the keys of the values or if any of the
* choices is not scalar * 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 // If no values are given, use the choices as values
// Since the choices are stored in the collection keys, i.e. they are // Since the choices are stored in the collection keys, i.e. they are

View File

@ -59,7 +59,7 @@ class LazyChoiceList implements ChoiceListInterface
* @param null|callable $value The callable generating the choice * @param null|callable $value The callable generating the choice
* values * values
*/ */
public function __construct(ChoiceLoaderInterface $loader, $value = null) public function __construct(ChoiceLoaderInterface $loader, callable $value = null)
{ {
$this->loader = $loader; $this->loader = $loader;
$this->value = $value; $this->value = $value;

View File

@ -36,7 +36,7 @@ class StreamedResponse extends Response
* @param int $status The response status code * @param int $status The response status code
* @param array $headers An array of response headers * @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); parent::__construct(null, $status, $headers);

View File

@ -131,7 +131,7 @@ class ControllerResolver implements ControllerResolverInterface
* *
* @param string $controller A Controller string * @param string $controller A Controller string
* *
* @return mixed A PHP callable * @return callable A PHP callable
* *
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
*/ */

View File

@ -29,12 +29,10 @@ class FilterControllerEvent extends KernelEvent
{ {
/** /**
* The current controller. * The current controller.
*
* @var callable
*/ */
private $controller; 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); parent::__construct($kernel, $request, $requestType);

View File

@ -45,7 +45,7 @@ class DebugHandlersListener implements EventSubscriberInterface
* @param bool $scream Enables/disables screaming mode, where even silenced errors are logged * @param bool $scream Enables/disables screaming mode, where even silenced errors are logged
* @param string $fileLinkFormat The format for links to source files * @param string $fileLinkFormat The format for links to source files
*/ */
public function __construct($exceptionHandler, LoggerInterface $logger = null, $levels = null, $throwAt = -1, $scream = true, $fileLinkFormat = null) public function __construct(callable $exceptionHandler = null, LoggerInterface $logger = null, $levels = null, $throwAt = -1, $scream = true, $fileLinkFormat = null)
{ {
$this->exceptionHandler = $exceptionHandler; $this->exceptionHandler = $exceptionHandler;
$this->logger = $logger; $this->logger = $logger;

View File

@ -154,17 +154,6 @@ class HttpKernelTest extends \PHPUnit_Framework_TestCase
$kernel->handle(new Request()); $kernel->handle(new Request());
} }
/**
* @expectedException \LogicException
*/
public function testHandleWhenTheControllerIsNotACallable()
{
$dispatcher = new EventDispatcher();
$kernel = new HttpKernel($dispatcher, $this->getResolver('foobar'));
$kernel->handle(new Request());
}
public function testHandleWhenTheControllerIsAClosure() public function testHandleWhenTheControllerIsAClosure()
{ {
$response = new Response('foo'); $response = new Response('foo');

View File

@ -54,7 +54,7 @@ class PhpProcess extends Process
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function start($callback = null) public function start(callable $callback = null)
{ {
if (null === $this->getCommandLine()) { if (null === $this->getCommandLine()) {
throw new RuntimeException('Unable to find the PHP executable.'); throw new RuntimeException('Unable to find the PHP executable.');

View File

@ -217,7 +217,7 @@ class Process
* @throws RuntimeException if PHP was compiled with --enable-sigchild and the enhanced sigchild compatibility mode is not enabled * @throws RuntimeException if PHP was compiled with --enable-sigchild and the enhanced sigchild compatibility mode is not enabled
* @throws ProcessFailedException if the process didn't terminate successfully * @throws ProcessFailedException if the process didn't terminate successfully
*/ */
public function mustRun($callback = null) public function mustRun(callable $callback = null)
{ {
if ($this->isSigchildEnabled() && !$this->enhanceSigchildCompatibility) { if ($this->isSigchildEnabled() && !$this->enhanceSigchildCompatibility) {
throw new RuntimeException('This PHP has been compiled with --enable-sigchild. You must use setEnhanceSigchildCompatibility() to use this method.'); throw new RuntimeException('This PHP has been compiled with --enable-sigchild. You must use setEnhanceSigchildCompatibility() to use this method.');
@ -252,7 +252,7 @@ class Process
* @throws RuntimeException When process is already running * @throws RuntimeException When process is already running
* @throws LogicException In case a callback is provided and output has been disabled * @throws LogicException In case a callback is provided and output has been disabled
*/ */
public function start($callback = null) public function start(callable $callback = null)
{ {
if ($this->isRunning()) { if ($this->isRunning()) {
throw new RuntimeException('Process is already running'); throw new RuntimeException('Process is already running');
@ -310,7 +310,7 @@ class Process
* *
* @see start() * @see start()
*/ */
public function restart($callback = null) public function restart(callable $callback = null)
{ {
if ($this->isRunning()) { if ($this->isRunning()) {
throw new RuntimeException('Process is already running'); throw new RuntimeException('Process is already running');
@ -337,7 +337,7 @@ class Process
* @throws RuntimeException When process stopped after receiving signal * @throws RuntimeException When process stopped after receiving signal
* @throws LogicException When process is not yet started * @throws LogicException When process is not yet started
*/ */
public function wait($callback = null) public function wait(callable $callback = null)
{ {
$this->requireProcessIsStarted(__FUNCTION__); $this->requireProcessIsStarted(__FUNCTION__);
@ -1232,7 +1232,7 @@ class Process
* *
* @param callable|null $callback The user defined PHP callback * @param callable|null $callback The user defined PHP callback
* *
* @return callable A PHP callable * @return \Closure A PHP closure
*/ */
protected function buildCallback($callback) protected function buildCallback($callback)
{ {

View File

@ -97,7 +97,7 @@ abstract class AbstractNormalizer extends SerializerAwareNormalizer implements N
/** /**
* Set normalization callbacks. * Set normalization callbacks.
* *
* @param array $callbacks help normalize the result * @param callable[] $callbacks help normalize the result
* *
* @return self * @return self
* *

View File

@ -350,10 +350,10 @@ class PhpEngine implements EngineInterface, \ArrayAccess
/** /**
* Adds an escaper for the given context. * Adds an escaper for the given context.
* *
* @param string $context The escaper context (html, js, ...) * @param string $context The escaper context (html, js, ...)
* @param mixed $escaper A PHP callable * @param callable $escaper A PHP callable
*/ */
public function setEscaper($context, $escaper) public function setEscaper($context, callable $escaper)
{ {
$this->escapers[$context] = $escaper; $this->escapers[$context] = $escaper;
self::$escaperCache[$context] = array(); self::$escaperCache[$context] = array();
@ -364,7 +364,7 @@ class PhpEngine implements EngineInterface, \ArrayAccess
* *
* @param string $context The context name * @param string $context The context name
* *
* @return mixed $escaper A PHP callable * @return callable $escaper A PHP callable
* *
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
*/ */

View File

@ -304,7 +304,7 @@ abstract class AbstractCloner implements ClonerInterface
$a = $cast; $a = $cast;
} }
} catch (\Exception $e) { } catch (\Exception $e) {
$a[(Stub::TYPE_OBJECT === $stub->type ? Caster::PREFIX_VIRTUAL : '').'⚠'] = new ThrowingCasterException($callback, $e); $a[(Stub::TYPE_OBJECT === $stub->type ? Caster::PREFIX_VIRTUAL : '').'⚠'] = new ThrowingCasterException($e);
} }
return $a; return $a;

View File

@ -17,10 +17,9 @@ namespace Symfony\Component\VarDumper\Exception;
class ThrowingCasterException extends \Exception class ThrowingCasterException extends \Exception
{ {
/** /**
* @param callable $caster The failing caster * @param \Exception $prev The exception thrown from the caster
* @param \Exception $prev The exception thrown from the caster
*/ */
public function __construct($caster, \Exception $prev) public function __construct(\Exception $prev)
{ {
parent::__construct('Unexpected '.get_class($prev).' thrown from a caster: '.$prev->getMessage(), 0, $prev); parent::__construct('Unexpected '.get_class($prev).' thrown from a caster: '.$prev->getMessage(), 0, $prev);
} }

View File

@ -38,12 +38,8 @@ class VarDumper
return call_user_func(self::$handler, $var); return call_user_func(self::$handler, $var);
} }
public static function setHandler($callable) public static function setHandler(callable $callable = null)
{ {
if (null !== $callable && !is_callable($callable, true)) {
throw new \InvalidArgumentException('Invalid PHP callback.');
}
$prevHandler = self::$handler; $prevHandler = self::$handler;
self::$handler = $callable; self::$handler = $callable;