8000 feature #16125 Replace is_callable checks with type hints (mpajunen, … · symfony/symfony@8b15af9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8b15af9

Browse files
committed
feature #16125 Replace is_callable checks with type hints (mpajunen, nicolas-grekas)
This PR was merged into the 3.0-dev branch. Discussion ---------- Replace is_callable checks with type hints | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #14330 | License | MIT | Doc PR | - Also removes tests checking the exceptions thrown from the removed is_callable checks. Commits ------- 7685cdd Add more callable type hints 4e0c6e1 Replace is_callable checks with type hints
2 parents 2b29602 + 7685cdd commit 8b15af9

33 files changed

+47
-193
lines changed

src/Symfony/Bridge/Twig/Command/DebugCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ private function getMetadata($type, $entity)
149149
return;
150150
}
151151
$refl = new \ReflectionMethod($cb[0], $cb[1]);
152-
} elseif (is_object($cb) && is_callable($cb)) {
152+
} elseif (is_object($cb) && method_exists($cb, '__invoke')) {
153153
$refl = new \ReflectionMethod($cb, '__invoke');
154154
} elseif (function_exists($cb)) {
155155
$refl = new \ReflectionFunction($cb);

src/Symfony/Bundle/FrameworkBundle/Resources/config/debug_prod.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<service id="debug.debug_handlers_listener" class="Symfony\Component\HttpKernel\EventListener\DebugHandlersListener">
1313
<tag name="kernel.event_subscriber" />
1414
<tag name="monolog.logger" channel="php" />
15-
<argument /><!-- Exception handler -->
15+
<argument>null</argument><!-- Exception handler -->
1616
<argument type="service" id="logger" on-invalid="null" />
1717
<argument>null</argument><!-- Log levels map for enabled error levels -->
1818
<argument>null</argument>

src/Symfony/Component/Console/Command/Command.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -273,12 +273,8 @@ public function run(InputInterface $input, OutputInterface $output)
273273
*
274274
* @see execute()
275275
*/
276-
public function setCode($code)
276+
public function setCode(callable $code)
277277
{
278-
if (!is_callable($code)) {
279-
throw new InvalidArgumentException('Invalid callable provided to Command::setCode.');
280-
}
281-
282278
if ($code instanceof \Closure) {
283279
$r = new \ReflectionFunction($code);
284280
if (null === $r->getClosureThis()) {

src/Symfony/Component/Console/Helper/ProcessHelper.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class ProcessHelper extends Helper
3636
*
3737
* @return Process The process that ran
3838
*/
39-
public function run(OutputInterface $output, $cmd, $error = null, $callback = null, $verbosity = OutputInterface::VERBOSITY_VERY_VERBOSE)
39+
public function run(OutputInterface $output, $cmd, $error = null, callable $callback = null, $verbosity = OutputInterface::VERBOSITY_VERY_VERBOSE)
4040
{
4141
if ($output instanceof ConsoleOutputInterface) {
4242
$output = $output->getErrorOutput();
@@ -92,7 +92,7 @@ public function run(OutputInterface $output, $cmd, $error = null, $callback = nu
9292
*
9393
* @see run()
9494
*/
95-
public function mustRun(OutputInterface $output, $cmd, $error = null, $callback = null)
95+
public function mustRun(OutputInterface $output, $cmd, $error = null, callable $callback = null)
9696
{
9797
$process = $this->run($output, $cmd, $error, $callback);
9898

@@ -112,7 +112,7 @@ public function mustRun(OutputInterface $output, $cmd, $error = null, $callback
112112
*
113113
* @return callable
114114
*/
115-
public function wrapCallback(OutputInterface $output, Process $process, $callback = null)
115+
public function wrapCallback(OutputInterface $output, Process $process, callable $callback = null)
116116
{
117117
if ($output instanceof ConsoleOutputInterface) {
118118
$output = $output->getErrorOutput();

src/Symfony/Component/Console/Helper/ProgressBar.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public function __construct(OutputInterface $output, $max = 0)
8686
* @param string $name The placeholder name (including the delimiter char like %)
8787
* @param callable $callable A PHP callable
8888
*/
89-
public static function setPlaceholderFormatterDefinition($name, $callable)
89+
public static function setPlaceholderFormatterDefinition($name, callable $callable)
9090
{
9191
if (!self::$formatters) {
9292
self::$formatters = self::initPlaceholderFormatters();

src/Symfony/Component/Console/Helper/QuestionHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ private function getHiddenResponse(OutputInterface $output, $inputStream)
377377
*
378378
* @throws \Exception In case the max number of attempts has been reached and no valid response has been given
379379
*/
380-
private function validateAttempts($interviewer, OutputInterface $output, Question $question)
380+
private function validateAttempts(callable $interviewer, OutputInterface $output, Question $question)
381381
{
382382
$error = null;
383383
$attempts = $question->getMaxAttempts();

src/Symfony/Component/Console/Question/Question.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public function setAutocompleterValues($values)
164164
*
165165
* @return Question The current instance
166166
*/
167-
public function setValidator($validator)
167+
public function setValidator(callable $validator = null)
168168
{
169169
$this->validator = $validator;
170170

@@ -220,11 +220,11 @@ public function getMaxAttempts()
220220
*
221221
* The normalizer can be a callable (a string), a closure or a class implementing __invoke.
222222
*
223-
* @param string|\Closure $normalizer
223+
* @param callable $normalizer
224224
*
225225
* @return Question The current instance
226226
*/
227-
public function setNormalizer($normalizer)
227+
public function setNormalizer(callable $normalizer)
228228
{
229229
$this->normalizer = $normalizer;
230230

@@ -236,7 +236,7 @@ public function setNormalizer($normalizer)
236236
*
237237
* The normalizer can ba a callable (a string), a closure or a class implementing __invoke.
238238
*
239-
* @return string|\Closure
239+
* @return callable
240240
*/
241241
public function getNormalizer()
242242
{

src/Symfony/Component/Console/Tests/Command/CommandTest.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -332,16 +332,6 @@ public function testSetCodeWithNonClosureCallable()
332332
$this->assertEquals('interact called'.PHP_EOL.'from the code...'.PHP_EOL, $tester->getDisplay());
333333
}
334334

335-
/**
336-
* @expectedException \InvalidArgumentException
337-
* @expectedExceptionMessage Invalid callable provided to Command::setCode.
338-
*/
339-
public function testSetCodeWithNonCallable()
340-
{
341-
$command = new \TestCommand();
342-
$command->setCode(array($this, 'nonExistentMethod'));
343-
}
344-
345335
public function callableMethodCommand(InputInterface $input, OutputInterface $output)
346336
{
347337
$output->writeln('from the code...');

src/Symfony/Component/Debug/ErrorHandler.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -235,14 +235,9 @@ public function setLoggers(array $loggers)
235235
* @param callable $handler A handler that will be called on Exception
236236
*
237237
* @return callable|null The previous exception handler
238-
*
239-
* @throws \InvalidArgumentException
240238
*/
241-
public function setExceptionHandler($handler)
239+
public function setExceptionHandler(callable $handler = null)
242240
{
243-
if (null !== $handler && !is_callable($handler)) {
244-
throw new \LogicException('The exception handler must be a valid PHP callable.');
245-
}
246241
$prev = $this->exceptionHandler;
247242
$this->exceptionHandler = $handler;
248243

src/Symfony/Component/Debug/ExceptionHandler.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,8 @@ public static function register($debug = true, $charset = null, $fileLinkFormat
7272
*
7373
* @return callable|null The previous exception handler if any
7474
*/
75-
public function setHandler($handler)
75+
public function setHandler(callable $handler = null)
7676
{
77-
if (null !== $handler && !is_callable($handler)) {
78-
throw new \LogicException('The exception handler must be a valid PHP callable.');
79-
}
8077
$old = $this->handler;
8178
$this->handler = $handler;
8279

src/Symfony/Component/ExpressionLanguage/ExpressionFunction.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class ExpressionFunction
4141
* @param callable $compiler A callable able to compile the function
4242
* @param callable $evaluator A callable able to evaluate the function
4343
*/
44-
public function __construct($name, $compiler, $evaluator)
44+
public function __construct($name, callable $compiler, callable $evaluator)
4545
{
4646
$this->name = $name;
4747
$this->compiler = $compiler;

src/Symfony/Component/ExpressionLanguage/ExpressionLanguage.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public function parse($expression, $names)
112112
*
113113
* @see ExpressionFunction
114114
*/
115-
public function register($name, $compiler, $evaluator)
115+
public function register($name, callable $compiler, callable $evaluator)
116116
{
117117
$this->functions[$name] = array('compiler' => $compiler, 'evaluator' => $evaluator);
118118
}

src/Symfony/Component/Finder/Iterator/CustomFilterIterator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ class CustomFilterIterator extends FilterIterator
2626
/**
2727
* Constructor.
2828
*
29-
* @param \Iterator $iterator The Iterator to filter
30-
* @param array $filters An array of PHP callbacks
29+
* @param \Iterator $iterator The Iterator to filter
30+
* @param callable[] $filters An array of PHP callbacks
3131
*
3232
* @throws \InvalidArgumentException
33 10000 33
*/

src/Symfony/Component/Form/CallbackTransformer.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,9 @@ class CallbackTransformer implements DataTransformerInterface
3535
*
3636
* @param callable $transform The forward transform callback
3737
* @param callable $reverseTransform The reverse transform callback
38-
*
39-
* @throws \InvalidArgumentException when the given callbacks is invalid
4038
*/
41-
public function __construct($transform, $reverseTransform)
39+
public function __construct(callable $transform, callable $reverseTransform)
4240
{
43-
if (!is_callable($transform)) {
44-
throw new \InvalidArgumentException('Argument 1 should be a callable');
45-
}
46-
if (!is_callable($reverseTransform)) {
47-
throw new \InvalidArgumentException('Argument 2 should be a callable');
48-
}
49-
5041
$this->transform = $transform;
5142
$this->reverseTransform = $reverseTransform;
5243
}

src/Symfony/Component/Form/ChoiceList/ArrayChoiceList.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111

1212
namespace Symfony\Component\Form\ChoiceList;
1313

14-
use Symfony\Component\Form\Exception\UnexpectedTypeException;
15-
1614
/**
1715
* A list of choices with arbitrary data types.
1816
*
@@ -64,12 +62,8 @@ class ArrayChoiceList implements ChoiceListInterface
6462
* incrementing integers are used as
6563
* values
6664
*/
67-
public function __construct($choices, $value = null)
65+
public function __construct($choices, callable $value = null)
6866
{
69-
if (null !== $value && !is_callable($value)) {
70-
throw new UnexpectedTypeException($value, 'null or callable');
71-
}
72-
7367
if ($choices instanceof \Traversable) {
7468
$choices = iterator_to_array($choices);
7569
}

src/Symfony/Component/Form/ChoiceList/ArrayKeyChoiceList.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public static function toArrayKey($choice)
101101
* the keys of the values or if any of the
102102
* choices is not scalar
103103
*/
104-
public function __construct($choices, $value = null)
104+
public function __construct($choices, callable $value = null)
105105
{
106106
// If no values are given, use the choices as values
107107
// Since the choices are stored in the collection keys, i.e. they are

src/Symfony/Component/Form/ChoiceList/LazyChoiceList.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class LazyChoiceList implements ChoiceListInterface
5959
* @param null|callable $value The callable generating the choice
6060
* values
6161
*/
62-
public function __construct(ChoiceLoaderInterface $loader, $value = null)
62+
public function __construct(ChoiceLoaderInterface $loader, callable $value = null)
6363
{
6464
$this->loader = $loader;
6565
$this->value = $value;

src/Symfony/Component/Form/Tests/CallbackTransformerTest.php

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,4 @@ function ($value) { return $value.' has reversely been transformed'; }
2525
$this->assertEquals('foo has been transformed', $transformer->transform('foo'));
2626
$this->assertEquals('bar has reversely been transformed', $transformer->reverseTransform('bar'));
2727
}
28-
29-
/**
30-
* @dataProvider invalidCallbacksProvider
31-
*
32-
* @expectedException \InvalidArgumentException
33-
*/
34-
public function testConstructorWithInvalidCallbacks($transformCallback, $reverseTransformCallback)
35-
{
36-
new CallbackTransformer($transformCallback, $reverseTransformCallback);
37-
}
38-
39-
public function invalidCallbacksProvider()
40-
{
41-
return array(
42-
array(null, function () {}),
43-
array(function () {}, null),
44-
);
45-
}
4628
}

src/Symfony/Component/Form/Tests/ChoiceList/ArrayChoiceListTest.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,6 @@ protected function getValues()
4242
return array('0', '1', '2', '3', '4', '5', '6');
4343
}
4444

45-
/**
46-
* @expectedException \Symfony\Component\Form\Exception\InvalidArgumentException
47-
*/
48-
public function testFailIfKeyMismatch()
49-
{
50-
new ArrayChoiceList(array(0 => 'a', 1 => 'b'), array(1 => 'a', 2 => 'b'));
51-
}
52-
5345
public function testCreateChoiceListWithValueCallback()
5446
{
5547
$callback = function ($choice) {

src/Symfony/Component/HttpFoundation/StreamedResponse.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class StreamedResponse extends Response
3636
* @param int $status The response status code
3737
* @param array $headers An array of response headers
3838
*/
39-
public function __construct($callback = null, $status = 200, $headers = array())
39+
public function __construct(callable $callback = null, $status = 200, $headers = array())
4040
{
4141
parent::__construct(null, $status, $headers);
4242

@@ -64,14 +64,9 @@ public static function create($callback = null, $status = 200, $headers = array(
6464
* Sets the PHP callback associated with this Response.
6565
*
6666
* @param callable $callback A valid PHP callback
67-
*
68-
* @throws \LogicException
6967
*/
70-
public function setCallback($callback)
68+
public function setCallback(callable $callback)
7169
{
72-
if (!is_callable($callback)) {
73-
throw new \LogicException('The Response callback must be a valid PHP callable.');
74-
}
7570
$this->callback = $callback;
7671
}
7772

src/Symfony/Component/HttpFoundation/Tests/StreamedResponseTest.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,6 @@ public function testSendContentWithNonCallable()
8787
$response->sendContent();
8888
}
8989

90-
/**
91-
* @expectedException \LogicException
92-
*/
93-
public function testSetCallbackNonCallable()
94-
{
95-
$response = new StreamedResponse(null);
96-
$response->setCallback(null);
97-
}
98-
9990
/**
10091
* @expectedException \LogicException
10192
*/

src/Symfony/Component/HttpKernel/Controller/ControllerResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ protected function doGetArguments(Request $request, $controller, array $paramete
131131
*
132132
* @param string $controller A Controller string
133133
*
134-
* @return mixed A PHP callable
134+
* @return callable A PHP callable
135135
*
136136
* @throws \InvalidArgumentException
137137
*/

src/Symfony/Component/HttpKernel/Event/FilterControllerEvent.php

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,10 @@ class FilterControllerEvent extends KernelEvent
2929
{
3030
/**
3131
* The current controller.
32-
*
33-
* @var callable
3432
*/
3533
private $controller;
3634

37-
public function __construct(HttpKernelInterface $kernel, $controller, Request $request, $requestType)
35+
public function __construct(HttpKernelInterface $kernel, callable $controller, Request $request, $requestType)
3836
{
3937
parent::__construct($kernel, $request, $requestType);
4038

@@ -58,47 +56,8 @@ public function getController()
5856
*
5957
* @throws \LogicException
6058
*/
61-
public function setController($controller)
59+
public function setController(callable $controller)
6260
{
63-
// controller must be a callable
64-
if (!is_callable($controller)) {
65-
throw new \LogicException(sprintf('The controller must be a callable (%s given).', $this->varToString($controller)));
66-
}
67-
6861
$this->controller = $controller;
6962
}
70-
71-
private function varToString($var)
72-
{
73-
if (is_object($var)) {
74-
return sprintf('Object(%s)', get_class($var));
75-
}
76 FA11 -
77-
if (is_array($var)) {
78-
$a = array();
79-
foreach ($var as $k => $v) {
80-
$a[] = sprintf('%s => %s', $k, $this->varToString($v));
81-
}
82-
83-
return sprintf('Array(%s)', implode(', ', $a));
84-
}
85-
86-
if (is_resource($var)) {
87-
return sprintf('Resource(%s)', get_resource_type($var));
88-
}
89-
90-
if (null === $var) {
91-
return 'null';
92-
}
93-
94-
if (false === $var) {
95-
return 'false';
96-
}
97-
98-
if (true === $var) {
99-
return 'true';
100-
}
101-
102-
return (string) $var;
103-
}
10463
}

0 commit comments

Comments
 (0)
0