8000 Replace is_callable checks with type hints by nicolas-grekas · Pull Request #16125 · symfony/symfony · GitHub
[go: up one dir, main page]

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 1 commit
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
Prev Previous commit
Add more callable type hints
  • Loading branch information
nicolas-grekas committed Oct 5, 2015
commit 7685cddf9583bb7992b80e27dae423e742bce105
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: 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
9E88
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
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
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
2 changes: 1 addition & 1 deletion 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
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
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 Down
Original file line number Diff line number Diff line change
Expand Up @@ F438 -45,7 +45,7 @@ class DebugHandlersListener implements EventSubscriberInterface
* @param bool $scream Enables/disables screaming mode, where even silenced errors are logged
* @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->logger = $logger;
Expand Down
11 changes: 0 additions & 11 deletions src/Symfony/Component/HttpKernel/Tests/HttpKernelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,17 +154,6 @@ public function testHandleWhenNoControllerIsFound()
$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()
{
$response = new Response('foo');
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Process/PhpProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function setPhpBinary($php)
/**
* {@inheritdoc}
*/
public function start($callback = null)
public function start(callable $callback = null)
{
if (null === $this->getCommandLine()) {
throw new RuntimeException('Unable to find the PHP executable.');
Expand Down
10 changes: 5 additions & 5 deletions src/Symfony/Component/Process/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public function run($callback = null)
* @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
*/
public function mustRun($callback = null)
public function mustRun(callable $callback = null)
{
if ($this->isSigchildEnabled() && !$this->enhanceSigchildCompatibility) {
throw new RuntimeException('This PHP has been compiled with --enable-sigchild. You must use setEnhanceSigchildCompatibility() to use this method.');
Expand Down Expand Up @@ -252,7 +252,7 @@ public function mustRun($callback = null)
* @throws RuntimeException When process is already running
* @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()) {
throw new RuntimeException('Process is already running');
Expand Down Expand Up @@ -310,7 +310,7 @@ public function start($callback = null)
*
* @see start()
*/
public function restart($callback = null)
public function restart(callable $callback = null)
{
if ($this->isRunning()) {
throw new RuntimeException('Process is already running');
Expand All @@ -337,7 +337,7 @@ public function restart($callback = null)
* @throws RuntimeException When process stopped after receiving signal
* @throws LogicException When process is not yet started
*/
public function wait($callback = null)
public function wait(callable $callback = null)
{
$this->requireProcessIsStarted(__FUNCTION__);

Expand Down Expand Up @@ -1232,7 +1232,7 @@ private function getDescriptors()
*
* @param callable|null $callback The user defined PHP callback
*
* @return callable A PHP callable
* @return \Closure A PHP closure
*/
protected function buildCallback($callback)
{
Expand Down
Original file line number Diff line number< 10000 /th> Diff line change
Expand Up @@ -97,7 +97,7 @@ public function setCircularReferenceHandler(callable $circularReferenceHandler)
/**
* Set normalization callbacks.
*
* @param array $callbacks help normalize the result
* @param callable[] $callbacks help normalize the result
*
* @return self
*
Expand Down
8 changes: 4 additions & 4 deletions src/Symfony/Component/Templating/PhpEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,10 +350,10 @@ public function getCharset()
/**
* Adds an escaper for the given context.
*
* @param string $context The escaper context (html, js, ...)
* @param mixed $escaper A PHP callable
* @param string $context The escaper context (html, js, ...)
* @param callable $escaper A PHP callable
*/
public function setEscaper($context, $escaper)
public function setEscaper($context, callable $escaper)
{
$this->escapers[$context] = $escaper;
self::$escaperCache[$context] = array();
Expand All @@ -364,7 +364,7 @@ public function setEscaper($context, $escaper)
*
* @param string $context The context name
*
* @return mixed $escaper A PHP callable
* @return callable $escaper A PHP callable
*
* @throws \InvalidArgumentException
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ private function callCaster($callback, $obj, $a, $stub, $isNested)
$a = $cast;
}
} 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this be changed in older branches actually ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, see #16129

{
parent::__construct('Unexpected '.get_class($prev).' thrown from a caster: '.$prev->getMessage(), 0, $prev);
}
Expand Down
6 changes: 1 addition & 5 deletions src/Symfony/Component/VarDumper/VarDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,8 @@ public static function dump($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;
self::$handler = $callable;

Expand Down
0