[BrowserKit] [5.0] Add type-hint to browserkit classes
This commit is contained in:
parent
38b9b951a4
commit
be0e4bd781
@ -64,12 +64,10 @@ abstract class AbstractBrowser
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets whether to automatically follow redirects or not.
|
* 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -92,10 +90,8 @@ abstract class AbstractBrowser
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the maximum number of redirects that crawler can follow.
|
* 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->maxRedirects = $maxRedirects < 0 ? -1 : $maxRedirects;
|
||||||
$this->followRedirects = -1 != $this->maxRedirects;
|
$this->followRedirects = -1 != $this->maxRedirects;
|
||||||
@ -118,13 +114,13 @@ abstract class AbstractBrowser
|
|||||||
*
|
*
|
||||||
* @throws \RuntimeException When Symfony Process Component is not installed
|
* @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')) {
|
if ($insulated && !class_exists('Symfony\\Component\\Process\\Process')) {
|
||||||
throw new \LogicException('Unable to isolate requests as the Symfony Process Component is not installed.');
|
throw new \LogicException('Unable to isolate requests as the Symfony Process Component is not installed.');
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->insulated = (bool) $insulated;
|
$this->insulated = $insulated;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -141,11 +137,8 @@ abstract class AbstractBrowser
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets single server parameter.
|
* 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;
|
$this->server[$key] = $value;
|
||||||
}
|
}
|
||||||
@ -153,12 +146,9 @@ abstract class AbstractBrowser
|
|||||||
/**
|
/**
|
||||||
* Gets single server parameter for specified key.
|
* 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
|
* @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;
|
return isset($this->server[$key]) ? $this->server[$key] : $default;
|
||||||
}
|
}
|
||||||
@ -416,7 +406,7 @@ abstract class AbstractBrowser
|
|||||||
return $this->crawler = $this->followRedirect();
|
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
|
// Check for meta refresh redirect
|
||||||
if ($this->followMetaRefresh && null !== $redirect = $this->getMetaRefreshUrl()) {
|
if ($this->followMetaRefresh && null !== $redirect = $this->getMetaRefreshUrl()) {
|
||||||
@ -515,13 +505,9 @@ abstract class AbstractBrowser
|
|||||||
*
|
*
|
||||||
* This method returns null if the DomCrawler component is not available.
|
* 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
|
* @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')) {
|
if (!class_exists('Symfony\Component\DomCrawler\Crawler')) {
|
||||||
return;
|
return;
|
||||||
@ -655,7 +641,7 @@ abstract class AbstractBrowser
|
|||||||
*
|
*
|
||||||
* @return string An absolute URI
|
* @return string An absolute URI
|
||||||
*/
|
*/
|
||||||
protected function getAbsoluteUri($uri)
|
protected function getAbsoluteUri(string $uri)
|
||||||
{
|
{
|
||||||
// already absolute?
|
// already absolute?
|
||||||
if (0 === strpos($uri, 'http://') || 0 === strpos($uri, 'https://')) {
|
if (0 === strpos($uri, 'http://') || 0 === strpos($uri, 'https://')) {
|
||||||
|
@ -119,14 +119,11 @@ class Cookie
|
|||||||
/**
|
/**
|
||||||
* Creates a Cookie instance from a Set-Cookie header value.
|
* 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
|
* @return static
|
||||||
*
|
*
|
||||||
* @throws \InvalidArgumentException
|
* @throws \InvalidArgumentException
|
||||||
*/
|
*/
|
||||||
public static function fromString($cookie, $url = null)
|
public static function fromString(string $cookie, string $url = null)
|
||||||
{
|
{
|
||||||
$parts = explode(';', $cookie);
|
$parts = explode(';', $cookie);
|
||||||
|
|
||||||
|
@ -33,13 +33,9 @@ class CookieJar
|
|||||||
* (this behavior ensures a BC behavior with previous versions of
|
* (this behavior ensures a BC behavior with previous versions of
|
||||||
* Symfony).
|
* 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
|
* @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();
|
$this->flushExpiredCookies();
|
||||||
|
|
||||||
@ -68,12 +64,8 @@ class CookieJar
|
|||||||
* You should never use an empty domain, but if you do so,
|
* You should never use an empty domain, but if you do so,
|
||||||
* all cookies for the given name/path expire (this behavior
|
* all cookies for the given name/path expire (this behavior
|
||||||
* ensures a BC behavior with previous versions of Symfony).
|
* 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) {
|
if (null === $path) {
|
||||||
$path = '/';
|
$path = '/';
|
||||||
@ -143,7 +135,7 @@ class CookieJar
|
|||||||
* @param Response $response A Response object
|
* @param Response $response A Response object
|
||||||
* @param string $uri The base URL
|
* @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);
|
$this->updateFromSetCookie($response->getHeader('Set-Cookie', false), $uri);
|
||||||
}
|
}
|
||||||
@ -172,12 +164,9 @@ class CookieJar
|
|||||||
/**
|
/**
|
||||||
* Returns not yet expired cookie values for the given URI.
|
* 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
|
* @return array An array of cookie values
|
||||||
*/
|
*/
|
||||||
public function allValues($uri, $returnsRawValue = false)
|
public function allValues(string $uri, bool $returnsRawValue = false)
|
||||||
{
|
{
|
||||||
$this->flushExpiredCookies();
|
$this->flushExpiredCookies();
|
||||||
|
|
||||||
@ -212,11 +201,9 @@ class CookieJar
|
|||||||
/**
|
/**
|
||||||
* Returns not yet expired raw cookie values for the given URI.
|
* Returns not yet expired raw cookie values for the given URI.
|
||||||
*
|
*
|
||||||
* @param string $uri A URI
|
|
||||||
*
|
|
||||||
* @return array An array of cookie values
|
* @return array An array of cookie values
|
||||||
*/
|
*/
|
||||||
public function allRawValues($uri)
|
public function allRawValues(string $uri)
|
||||||
{
|
{
|
||||||
return $this->allValues($uri, true);
|
return $this->allValues($uri, true);
|
||||||
}
|
}
|
||||||
|
@ -84,12 +84,9 @@ final class Response
|
|||||||
/**
|
/**
|
||||||
* Gets a response header.
|
* 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
|
* @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));
|
$normalizedHeader = str_replace('-', '_', strtolower($header));
|
||||||
foreach ($this->headers as $key => $value) {
|
foreach ($this->headers as $key => $value) {
|
||||||
|
Reference in New Issue
Block a user