Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4e0c6e1

Browse files
mpajunennicolas-grekas
authored andcommitted
Replace is_callable checks with type hints
Also removes tests checking the exceptions thrown from the removed is_callable checks.
1 parent 88e2d70 commit 4e0c6e1

File tree

13 files changed

+11
-139
lines changed

13 files changed

+11
-139
lines changed

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/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/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/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: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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/Event/FilterControllerEvent.php

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -58,47 +58,8 @@ public function getController()
5858
*
5959
* @throws \LogicException
6060
*/
61-
public function setController($controller)
61+
public function setController(callable $controller)
6262
{
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-
6863
$this->controller = $controller;
6964
}
70-
71-
private function varToString($var)
72-
{
73-
if (is_object($var)) {
74-
return sprintf('Object(%s)', get_class($var));
75-
}
76-
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-
}
10465
}

src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,9 @@ public function setCircularReferenceLimit($circularReferenceLimit)
8686
* @param callable $circularReferenceHandler
8787
*
8888
* @return self
89-
*
90-
* @throws InvalidArgumentException
9189
*/
92-
public function setCircularReferenceHandler($circularReferenceHandler)
90+
public function setCircularReferenceHandler(callable $circularReferenceHandler)
9391
{
94-
if (!is_callable($circularReferenceHandler)) {
95-
throw new InvalidArgumentException('The given circular reference handler is not callable.');
96-
}
97-
9892
$this->circularReferenceHandler = $circularReferenceHandler;
9993

10094
return $this;

src/Symfony/Component/Translation/PluralizationRules.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -189,12 +189,10 @@ public static function get($number, $locale)
189189
/**
190190
* Overrides the default plural rule for a given locale.
191191
*
192-
* @param string $rule A PHP callable
193-
* @param string $locale The locale
194-
*
195-
* @throws \LogicException
192+
* @param callable $rule A PHP callable
193+
* @param string $locale The locale
196194
*/
197-
public static function set($rule, $locale)
195+
public static function set(callable $rule, $locale)
198196
{
199197
if ('pt_BR' === $locale) {
200198
// temporary set a locale for brazilian
@@ -205,10 +203,6 @@ public static function set($rule, $locale)
205203
$locale = substr($locale, 0, -strlen(strrchr($locale, '_')));
206204
}
207205

208-
if (!is_callable($rule)) {
209-
throw new \LogicException('The given rule can not be called');
210-
}
211-
212206
self::$rules[$locale] = $rule;
213207
}
214208
}

0 commit comments

Comments
 (0)