Skip to content

[BrowserKit] [5.0] Add type-hint to all classes #32280

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
Show file tree
Hide file tree
Changes from all commits
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
34 changes: 10 additions & 24 deletions src/Symfony/Component/BrowserKit/AbstractBrowser.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,10 @@ public function __construct(array $server = [], History $history = null, CookieJ

/**
* Sets whether to automatically follow redirects or not.
*
* @param bool $followRedirect Whether to follow redirects
*/
public function followRedirects($followRedirect = true)
public function followRedirects(bool $followRedirects = true)
{
$this->followRedirects = (bool) $followRedirect;
$this->followRedirects = $followRedirects;
}

/**
Expand All @@ -92,10 +90,8 @@ public function isFollowingRedirects()

/**
* Sets the maximum number of redirects that crawler can follow.
*
* @param int $maxRedirects
*/
public function setMaxRedirects($maxRedirects)
public function setMaxRedirects(int $maxRedirects)
{
$this->maxRedirects = $maxRedirects < 0 ? -1 : $maxRedirects;
$this->followRedirects = -1 != $this->maxRedirects;
Expand All @@ -118,13 +114,13 @@ public function getMaxRedirects()
*
* @throws \RuntimeException When Symfony Process Component is not installed
*/
public function insulate($insulated = true)
public function insulate(bool $insulated = true)
{
if ($insulated && !class_exists('Symfony\\Component\\Process\\Process')) {
throw new \LogicException('Unable to isolate requests as the Symfony Process Component is not installed.');
}

$this->insulated = (bool) $insulated;
$this->insulated = $insulated;
}

/**
Expand All @@ -141,24 +137,18 @@ public function setServerParameters(array $server)

/**
* Sets single server parameter.
*
* @param string $key A key of the parameter
* @param string $value A value of the parameter
*/
public function setServerParameter($key, $value)
public function setServerParameter(string $key, string $value)
{
$this->server[$key] = $value;
}

/**
* Gets single server parameter for specified key.
*
* @param string $key A key of the parameter to get
* @param string $default A default value when key is undefined
*
* @return string A value of the parameter
*/
public function getServerParameter($key, $default = '')
public function getServerParameter(string $key, $default = '')
{
return isset($this->server[$key]) ? $this->server[$key] : $default;
}
Expand Down Expand Up @@ -416,7 +406,7 @@ public function request(string $method, string $uri, array $parameters = [], arr
return $this->crawler = $this->followRedirect();
}

$this->crawler = $this->createCrawlerFromContent($this->internalRequest->getUri(), $this->internalResponse->getContent(), $this->internalResponse->getHeader('Content-Type'));
$this->crawler = $this->createCrawlerFromContent($this->internalRequest->getUri(), $this->internalResponse->getContent(), $this->internalResponse->getHeader('Content-Type') ?? '');

// Check for meta refresh redirect
if ($this->followMetaRefresh && null !== $redirect = $this->getMetaRefreshUrl()) {
Expand Down Expand Up @@ -515,13 +505,9 @@ protected function filterResponse($response)
*
* This method returns null if the DomCrawler component is not available.
*
* @param string $uri A URI
* @param string $content Content for the crawler to use
* @param string $type Content type
*
* @return Crawler|null
*/
protected function createCrawlerFromContent($uri, $content, $type)
protected function createCrawlerFromContent(string $uri, string $content, string $type)
{
if (!class_exists('Symfony\Component\DomCrawler\Crawler')) {
return;
Expand Down Expand Up @@ -655,7 +641,7 @@ public function restart()
*
* @return string An absolute URI
*/
protected function getAbsoluteUri($uri)
protected function getAbsoluteUri(string $uri)
{
// already absolute?
if (0 === strpos($uri, 'http://') || 0 === strpos($uri, 'https://')) {
Expand Down
5 changes: 1 addition & 4 deletions src/Symfony/Component/BrowserKit/Cookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,11 @@ public function __toString()
/**
* Creates a Cookie instance from a Set-Cookie header value.
*
* @param string $cookie A Set-Cookie header value
* @param string|null $url The base URL
*
* @return static
*
* @throws \InvalidArgumentException
*/
public static function fromString($cookie, $url = null)
public static function fromString(string $cookie, string $url = null)
{
$parts = explode(';', $cookie);

Expand Down
23 changes: 5 additions & 18 deletions src/Symfony/Component/BrowserKit/CookieJar.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,9 @@ public function set(Cookie $cookie)
* (this behavior ensures a BC behavior with previous versions of
* Symfony).
*
* @param string $name The cookie name
* @param string $path The cookie path
* @param string $domain The cookie domain
*
* @return Cookie|null A Cookie instance or null if the cookie does not exist
*/
public function get($name, $path = '/', $domain = null)
public function get(string $name, string $path = '/', string $domain = null)
{
$this->flushExpiredCookies();

Expand Down Expand Up @@ -68,12 +64,8 @@ public function get($name, $path = '/', $domain = null)
* You should never use an empty domain, but if you do so,
* all cookies for the given name/path expire (this behavior
* ensures a BC behavior with previous versions of Symfony).
*
* @param string $name The cookie name
* @param string $path The cookie path
* @param string $domain The cookie domain
*/
public function expire($name, $path = '/', $domain = null)
public function expire(string $name, ?string $path = '/', string $domain = null)
{
if (null === $path) {
$path = '/';
Expand Down Expand Up @@ -143,7 +135,7 @@ public function updateFromSetCookie(array $setCookies, $uri = null)
* @param Response $response A Response object
* @param string $uri The base URL
*/
public function updateFromResponse(Response $response, $uri = null)
public function updateFromResponse(Response $response, string $uri = null)
{
$this->updateFromSetCookie($response->getHeader('Set-Cookie', false), $uri);
}
Expand Down Expand Up @@ -172,12 +164,9 @@ public function all()
/**
* Returns not yet expired cookie values for the given URI.
*
* @param string $uri A URI
* @param bool $returnsRawValue Returns raw value or urldecoded value
*
* @return array An array of cookie values
*/
public function allValues($uri, $returnsRawValue = false)
public function allValues(string $uri, bool $returnsRawValue = false)
{
$this->flushExpiredCookies();

Expand Down Expand Up @@ -212,11 +201,9 @@ public function allValues($uri, $returnsRawValue = false)
/**
* Returns not yet expired raw cookie values for the given URI.
*
* @param string $uri A URI
*
* @return array An array of cookie values
*/
public function allRawValues($uri)
public function allRawValues(string $uri)
{
return $this->allValues($uri, true);
}
Expand Down
5 changes: 1 addition & 4 deletions src/Symfony/Component/BrowserKit/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,9 @@ public function getHeaders(): array
/**
* Gets a response header.
*
* @param string $header The header name
* @param bool $first Whether to return the first value or all header values
*
* @return string|array The first header value if $first is true, an array of values otherwise
*/
public function getHeader($header, $first = true)
public function getHeader(string $header, bool $first = true)
{
$normalizedHeader = str_replace('-', '_', strtolower($header));
foreach ($this->headers as $key => $value) {
Expand Down