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

Skip to content

Commit 785b9cf

Browse files
committed
Fine tune constructor types
1 parent 8496003 commit 785b9cf

File tree

12 files changed

+32
-23
lines changed

12 files changed

+32
-23
lines changed

src/Symfony/Component/Config/Definition/BaseNode.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ abstract class BaseNode implements NodeInterface
4747
*/
4848
public function __construct(?string $name, NodeInterface $parent = null, string $pathSeparator = self::DEFAULT_PATH_SEPARATOR)
4949
{
50-
if (false !== strpos($name = (string) $name, $pathSeparator)) {
50+
if (null === $name) {
51+
$name = '';
52+
}
53+
if (false !== strpos($name, $pathSeparator)) {
5154
throw new \InvalidArgumentException('The name must not contain "'.$pathSeparator.'".');
5255
}
5356

src/Symfony/Component/Config/Definition/Builder/NodeDefinition.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ abstract class NodeDefinition implements NodeParentInterface
4141
public function __construct(?string $name, NodeParentInterface $parent = null)
4242
{
4343
$this->parent = $parent;
44-
$this->name = $name;
44+
$this->name = $name ?? '';
4545
}
4646

4747
/**

src/Symfony/Component/DomCrawler/AbstractUriElement.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ abstract class AbstractUriElement
2424
protected $node;
2525

2626
/**
27-
* @var string The method to use for the element
27+
* @var string|null The method to use for the element
2828
*/
2929
protected $method;
3030

@@ -36,7 +36,7 @@ abstract class AbstractUriElement
3636
/**
3737
* @param \DOMElement $node A \DOMElement instance
3838
* @param string $currentUri The URI of the page where the link is embedded (or the base href)
39-
* @param string $method The method to use for the link (GET by default)
39+
* @param string|null $method The method to use for the link (GET by default)
4040
*
4141
* @throws \InvalidArgumentException if the node is not a link
4242
*/
@@ -70,7 +70,7 @@ public function getNode()
7070
*/
7171
public function getMethod()
7272
{
73-
return $this->method;
73+
return $this->method ?? 'GET';
7474
}
7575

7676
/**

src/Symfony/Component/Form/Form.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ class Form implements \IteratorAggregate, FormInterface, ClearableErrorsInterfac
146146
private $lockSetData = false;
147147

148148
/**
149-
* @var string|int|null
149+
* @var string|null
150150
*/
151151
private $name;
152152

@@ -847,6 +847,8 @@ public function add($child, $type = null, array $options = [])
847847
throw new UnexpectedTypeException($child, 'string, integer or Symfony\Component\Form\FormInterface');
848848
}
849849

850+
$child = (string) $child;
851+
850852
if (null !== $type && !\is_string($type) && !$type instanceof FormTypeInterface) {
851853
throw new UnexpectedTypeException($type, 'string or Symfony\Component\Form\FormTypeInterface');
852854
}

src/Symfony/Component/Form/FormConfigBuilder.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,23 +107,23 @@ class FormConfigBuilder implements FormConfigBuilderInterface
107107
/**
108108
* Creates an empty form configuration.
109109
*
110-
* @param string|int $name The form name
110+
* @param string|null $name The form name
111111
* @param string|null $dataClass The class of the form's data
112112
* @param EventDispatcherInterface $dispatcher The event dispatcher
113113
* @param array $options The form options
114114
*
115115
* @throws InvalidArgumentException if the data class is not a valid class or if
116116
* the name contains invalid characters
117117
*/
118-
public function __construct($name, ?string $dataClass, EventDispatcherInterface $dispatcher, array $options = [])
118+
public function __construct(?string $name, ?string $dataClass, EventDispatcherInterface $dispatcher, array $options = [])
119119
{
120120
self::validateName($name);
121121

122122
if (null !== $dataClass && !class_exists($dataClass) && !interface_exists($dataClass, false)) {
123123
throw new InvalidArgumentException(sprintf('Class "%s" not found. Is the "data_class" form option set correctly?', $dataClass));
124124
}
125125

126-
$this->name = (string) $name;
126+
$this->name = $name ?? '';
127127
$this->dataClass = $dataClass;
128128
$this->dispatcher = $dispatcher;
129129
$this->options = $options;
@@ -767,15 +767,17 @@ public function getFormConfig()
767767
/**
768768
* Validates whether the given variable is a valid form name.
769769
*
770-
* @param string|int|null $name The tested form name
770+
* @param string|null $name The tested form name
771771
*
772772
* @throws UnexpectedTypeException if the name is not a string or an integer
773773
* @throws InvalidArgumentException if the name contains invalid characters
774+
*
775+
* @internal since Symfony 4.4
774776
*/
775777
public static function validateName($name)
776778
{
777-
if (null !== $name && !\is_string($name) && !\is_int($name)) {
778-
throw new UnexpectedTypeException($name, 'string, integer or null');
779+
if (null !== $name && !\is_string($name)) {
780+
throw new UnexpectedTypeException($name, 'string or null');
779781
}
780782

781783
if (!self::isValidName($name)) {

src/Symfony/Component/Form/FormError.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ class FormError
4747
*
4848
* @see \Symfony\Component\Translation\Translator
4949
*/
50-
public function __construct(?string $message, string $messageTemplate = null, array $messageParameters = [], int $messagePluralization = null, $cause = null)
50+
public function __construct(string $message, string $messageTemplate = null, array $messageParameters = [], int $messagePluralization = null, $cause = null)
5151
{
52-
$this->message = (string) $message;
52+
$this->message = $message;
5353
$this->messageTemplate = $messageTemplate ?: $message;
5454
$this->messageParameters = $messageParameters;
5555
$this->messagePluralization = $messagePluralization;

src/Symfony/Component/Form/FormFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function createNamedBuilder($name, $type = 'Symfony\Component\Form\Extens
7373

7474
$type = $this->registry->getType($type);
7575

76-
$builder = $type->createBuilder($this, $name, $options);
76+
$builder = $type->createBuilder($this, (string) $name, $options);
7777

7878
// Explicitly call buildForm() in order to be able to override either
7979
// createBuilder() or buildForm() in the resolved form type

src/Symfony/Component/HttpFoundation/RedirectResponse.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class RedirectResponse extends Response
3232
*
3333
* @see http://tools.ietf.org/html/rfc2616#section-10.3
3434
*/
35-
public function __construct(?string $url, int $status = 302, array $headers = [])
35+
public function __construct(string $url, int $status = 302, array $headers = [])
3636
{
3737
parent::__construct('', $status, $headers);
3838

@@ -82,7 +82,7 @@ public function getTargetUrl()
8282
*/
8383
public function setTargetUrl($url)
8484
{
85-
if (empty($url)) {
85+
if ('' == $url) {
8686
throw new \InvalidArgumentException('Cannot redirect to an empty URL.');
8787
}
8888

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ public function testGenerateMetaRedirect()
2828

2929
/**
3030
* @expectedException \InvalidArgumentException
31+
* @expectedExceptionMessage Cannot redirect to an empty URL.
3132
*/
32-
public function testRedirectResponseConstructorNullUrl()
33+
public function testRedirectResponseConstructorEmptyUrl()
3334
{
34-
$response = new RedirectResponse(null);
35+
$response = new RedirectResponse('');
3536
}
3637

3738
/**

src/Symfony/Component/Validator/ConstraintViolation.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ class ConstraintViolation implements ConstraintViolationInterface
4444
* violation
4545
* @param int|null $plural The number for determining the plural
4646
* form when translating the message
47-
* @param mixed $code The error code of the violation
47+
* @param string|null $code The error code of the violation
4848
* @param Constraint|null $constraint The constraint whose validation
4949
* caused the violation
5050
* @param mixed $cause The cause of the violation
5151
*/
52-
public function __construct(?string $message, ?string $messageTemplate, array $parameters, $root, ?string $propertyPath, $invalidValue, int $plural = null, $code = null, Constraint $constraint = null, $cause = null)
52+
public function __construct(string $message, ?string $messageTemplate, array $parameters, $root, ?string $propertyPath, $invalidValue, int $plural = null, $code = null, Constraint $constraint = null, $cause = null)
5353
{
5454
$this->message = $message;
5555
$this->messageTemplate = $messageTemplate;

src/Symfony/Component/Validator/Test/ConstraintValidatorTestCase.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ protected function restoreDefaultTimezone()
9999
protected function createContext()
100100
{
101101
$translator = $this->getMockBuilder(TranslatorInterface::class)->getMock();
102+
$translator->expects($this->any())->method('trans')->willReturnArgument(0);
102103
$validator = $this->getMockBuilder('Symfony\Component\Validator\Validator\ValidatorInterface')->getMock();
103104
$contextualValidator = $this->getMockBuilder('Symfony\Component\Validator\Validator\ContextualValidatorInterface')->getMock();
104105

@@ -330,7 +331,7 @@ public function assertRaised()
330331
private function getViolation()
331332
{
332333
return new ConstraintViolation(
333-
null,
334+
$this->message,
334335
$this->message,
335336
$this->parameters,
336337
$this->context->getRoot(),

src/Symfony/Component/Validator/Violation/ConstraintViolationBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class ConstraintViolationBuilder implements ConstraintViolationBuilderInterface
4747
/**
4848
* @param TranslatorInterface $translator
4949
*/
50-
public function __construct(ConstraintViolationList $violations, Constraint $constraint, $message, array $parameters, $root, $propertyPath, $invalidValue, $translator, $translationDomain = null)
50+
public function __construct(ConstraintViolationList $violations, Constraint $constraint, string $message, array $parameters, $root, string $propertyPath, $invalidValue, $translator, string $translationDomain = null)
5151
{
5252
if (!$translator instanceof LegacyTranslatorInterface && !$translator instanceof TranslatorInterface) {
5353
throw new \TypeError(sprintf('Argument 8 passed to %s() must be an instance of %s, %s given.', __METHOD__, TranslatorInterface::class, \is_object($translator) ? \get_class($translator) : \gettype($translator)));

0 commit comments

Comments
 (0)