-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Fine tune constructor types #32066
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
Fine tune constructor types #32066
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,7 +24,7 @@ abstract class AbstractUriElement | |
protected $node; | ||
|
||
/** | ||
* @var string The method to use for the element | ||
* @var string|null The method to use for the element | ||
*/ | ||
protected $method; | ||
|
||
|
@@ -36,7 +36,7 @@ abstract class AbstractUriElement | |
/** | ||
* @param \DOMElement $node A \DOMElement instance | ||
* @param string $currentUri The URI of the page where the link is embedded (or the base href) | ||
* @param string $method The method to use for the link (GET by default) | ||
* @param string|null $method The method to use for the link (GET by default) | ||
* | ||
* @throws \InvalidArgumentException if the node is not a link | ||
*/ | ||
|
@@ -70,7 +70,7 @@ public function getNode() | |
*/ | ||
public function getMethod() | ||
{ | ||
return $this->method; | ||
return $this->method ?? 'GET'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Must return string because of
request has string type which would otherwise fail. So lets default back to GET which is in the constructor.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will it not be better to set the |
||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -146,7 +146,7 @@ class Form implements \IteratorAggregate, FormInterface, ClearableErrorsInterfac | |
private $lockSetData = false; | ||
|
||
/** | ||
* @var string|int|null | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this cannot be int anymore due to constructor types added in #24722 |
||
* @var string|null | ||
*/ | ||
private $name; | ||
|
||
|
@@ -847,6 +847,8 @@ public function add($child, $type = null, array $options = []) | |
throw new UnexpectedTypeException($child, 'string, integer or Symfony\Component\Form\FormInterface'); | ||
} | ||
|
||
$child = (string) $child; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code forwarding this relies on the child name to be a string (instead of int) |
||
|
||
if (null !== $type && !\is_string($type) && !$type instanceof FormTypeInterface) { | ||
throw new UnexpectedTypeException($type, 'string or Symfony\Component\Form\FormTypeInterface'); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -107,23 +107,23 @@ class FormConfigBuilder implements FormConfigBuilderInterface | |
/** | ||
* Creates an empty form configuration. | ||
* | ||
* @param string|int $name The form name | ||
* @param string|null $name The form name | ||
* @param string|null $dataClass The class of the form's data | ||
* @param EventDispatcherInterface $dispatcher The event dispatcher | ||
* @param array $options The form options | ||
* | ||
* @throws InvalidArgumentException if the data class is not a valid class or if | ||
* the name contains invalid characters | ||
*/ | ||
public function __construct($name, ?string $dataClass, EventDispatcherInterface $dispatcher, array $options = []) | ||
public function __construct(?string $name, ?string $dataClass, EventDispatcherInterface $dispatcher, array $options = []) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
{ | ||
self::validateName($name); | ||
|
||
if (null !== $dataClass && !class_exists($dataClass) && !interface_exists($dataClass, false)) { | ||
throw new InvalidArgumentException(sprintf('Class "%s" not found. Is the "data_class" form option set correctly?', $dataClass)); | ||
} | ||
|
||
$this->name = (string) $name; | ||
$this->name = $name ?? ''; | ||
$this->dataClass = $dataClass; | ||
$this->dispatcher = $dispatcher; | ||
$this->options = $options; | ||
|
@@ -767,15 +767,17 @@ public function getFormConfig() | |
/** | ||
* Validates whether the given variable is a valid form name. | ||
* | ||
* @param string|int|null $name The tested form name | ||
* @param string|null $name The tested form name | ||
* | ||
* @throws UnexpectedTypeException if the name is not a string or an integer | ||
* @throws InvalidArgumentException if the name contains invalid characters | ||
* | ||
* @internal since Symfony 4.4 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is only used in this single class. So wouldn't need to be public anymore. The whole method is not needed anyonce once we added real types in Form parameters in SF 5. |
||
*/ | ||
public static function validateName($name) | ||
{ | ||
if (null !== $name && !\is_string($name) && !\is_int($name)) { | ||
throw new UnexpectedTypeException($name, 'string, integer or null'); | ||
if (null !== $name && !\is_string($name)) { | ||
throw new UnexpectedTypeException($name, 'string or null'); | ||
} | ||
|
||
if (!self::isValidName($name)) { | ||
|
@@ -792,12 +794,8 @@ public static function validateName($name) | |
* * starts with a letter, digit or underscore | ||
* * contains only letters, digits, numbers, underscores ("_"), | ||
* hyphens ("-") and colons (":") | ||
* | ||
* @param string|null $name The tested form name | ||
* | ||
* @return bool Whether the name is valid | ||
*/ | ||
public static function isValidName($name) | ||
final public static function isValidName(?string $name): bool | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there is no point in people overwriting the method as its called with |
||
{ | ||
return '' === $name || null === $name || preg_match('/^[a-zA-Z0-9_][a-zA-Z0-9_\-:]*$/D', $name); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,7 +32,7 @@ class RedirectResponse extends Response | |
* | ||
* @see http://tools.ietf.org/html/rfc2616#section-10.3 | ||
*/ | ||
public function __construct(?string $url, int $status = 302, array $headers = []) | ||
public function __construct(string $url, int $status = 302, array $headers = []) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this was only nullable because of a unit test. but this is an error that is not meant to be caught. so it just throws a TypeError instead of |
||
{ | ||
parent::__construct('', $status, $headers); | ||
|
||
|
@@ -82,7 +82,7 @@ public function getTargetUrl() | |
*/ | ||
public function setTargetUrl($url) | ||
{ | ||
if (empty($url)) { | ||
if ('' == $url) { | ||
throw new \InvalidArgumentException('Cannot redirect to an empty URL.'); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,12 +44,12 @@ class ConstraintViolation implements ConstraintViolationInterface | |
* violation | ||
* @param int|null $plural The number for determining the plural | ||
* form when translating the message | ||
* @param mixed $code The error code of the violation | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that must be an error. it cannot be mixed. \Symfony\Component\Validator\ConstraintViolationInterface::getCode says string|null. and calling code wouldn't be able to handle mixed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a test that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am going to prepare a PR to deprecate using anything else than a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see #32265 |
||
* @param string|null $code The error code of the violation | ||
* @param Constraint|null $constraint The constraint whose validation | ||
* caused the violation | ||
* @param mixed $cause The cause of the violation | ||
*/ | ||
public function __construct(?string $message, ?string $messageTemplate, array $parameters, $root, ?string $propertyPath, $invalidValue, int $plural = null, $code = null, Constraint $constraint = null, $cause = null) | ||
public function __construct(string $message, ?string $messageTemplate, array $parameters, $root, ?string $propertyPath, $invalidValue, int $plural = null, $code = null, Constraint $constraint = null, $cause = null) | ||
{ | ||
$this->message = $message; | ||
$this->messageTemplate = $messageTemplate; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,7 +47,7 @@ class ConstraintViolationBuilder implements ConstraintViolationBuilderInterface | |
/** | ||
* @param TranslatorInterface $translator | ||
*/ | ||
public function __construct(ConstraintViolationList $violations, Constraint $constraint, $message, array $parameters, $root, $propertyPath, $invalidValue, $translator, $translationDomain = null) | ||
public function __construct(ConstraintViolationList $violations, Constraint $constraint, string $message, array $parameters, $root, string $propertyPath, $invalidValue, $translator, string $translationDomain = null) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this breaks the 3.4 CI, so let's allow null There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The job you linked succeded? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I relaunched it after #32074 and it passed. |
||
{ | ||
if (!$translator instanceof LegacyTranslatorInterface && !$translator instanceof TranslatorInterface) { | ||
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))); | ||
|
Uh oh!
There was an error while loading. Please reload this page.