Skip to content

Commit

Permalink
Fix PHP 8.4 deprecations and CS (#641)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas authored Oct 3, 2024
1 parent 9f6010b commit 3011210
Show file tree
Hide file tree
Showing 25 changed files with 69 additions and 69 deletions.
16 changes: 8 additions & 8 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,25 +64,25 @@ final class Client extends AbstractBrowser implements WebDriver, JavaScriptExecu
/**
* @param string[]|null $arguments
*/
public static function createChromeClient(string $chromeDriverBinary = null, array $arguments = null, array $options = [], string $baseUri = null): self
public static function createChromeClient(?string $chromeDriverBinary = null, ?array $arguments = null, array $options = [], ?string $baseUri = null): self
{
return new self(new ChromeManager($chromeDriverBinary, $arguments, $options), $baseUri);
}

/**
* @param string[]|null $arguments
*/
public static function createFirefoxClient(string $geckodriverBinary = null, array $arguments = null, array $options = [], string $baseUri = null): self
public static function createFirefoxClient(?string $geckodriverBinary = null, ?array $arguments = null, array $options = [], ?string $baseUri = null): self
{
return new self(new FirefoxManager($geckodriverBinary, $arguments, $options), $baseUri);
}

public static function createSeleniumClient(string $host = null, WebDriverCapabilities $capabilities = null, string $baseUri = null, array $options = []): self
public static function createSeleniumClient(?string $host = null, ?WebDriverCapabilities $capabilities = null, ?string $baseUri = null, array $options = []): self
{
return new self(new SeleniumManager($host, $capabilities, $options), $baseUri);
}

public function __construct(BrowserManagerInterface $browserManager, string $baseUri = null)
public function __construct(BrowserManagerInterface $browserManager, ?string $baseUri = null)
{
$this->browserManager = $browserManager;
$this->baseUri = $baseUri;
Expand Down Expand Up @@ -252,7 +252,7 @@ public function refreshCrawler(): PantherCrawler
return $this->crawler = $this->createCrawler();
}

public function request(string $method, string $uri, array $parameters = [], array $files = [], array $server = [], string $content = null, bool $changeHistory = true): PantherCrawler
public function request(string $method, string $uri, array $parameters = [], array $files = [], array $server = [], ?string $content = null, bool $changeHistory = true): PantherCrawler
{
if ('GET' !== $method) {
throw new \InvalidArgumentException('Only the GET method is supported when using WebDriver.');
Expand All @@ -266,7 +266,7 @@ public function request(string $method, string $uri, array $parameters = [], arr

foreach (['parameters', 'files', 'server'] as $arg) {
if ([] !== $$arg) {
throw new \InvalidArgumentException(sprintf('The parameter "$%s" is not supported when using WebDriver.', $arg));
throw new \InvalidArgumentException(\sprintf('The parameter "$%s" is not supported when using WebDriver.', $arg));
}
}

Expand Down Expand Up @@ -773,9 +773,9 @@ public function ping(int $timeout = 1000): bool
private function createException(string $implementableClass): \Exception
{
if (null === $this->webDriver) {
return new \LogicException(sprintf('WebDriver not started yet. Call method `start()` first before calling any `%s` method.', $implementableClass));
return new \LogicException(\sprintf('WebDriver not started yet. Call method `start()` first before calling any `%s` method.', $implementableClass));
}

return new \RuntimeException(sprintf('"%s" does not implement "%s".', \get_class($this->webDriver), $implementableClass));
return new \RuntimeException(\sprintf('"%s" does not implement "%s".', \get_class($this->webDriver), $implementableClass));
}
}
2 changes: 1 addition & 1 deletion src/Cookie/CookieJar.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ private function webDriverToSymfony(WebDriverCookie $cookie): Cookie
return new Cookie($cookie->getName(), $cookie->getValue(), $expiry, $cookie->getPath(), (string) $cookie->getDomain(), (bool) $cookie->isSecure(), (bool) $cookie->isHttpOnly());
}

private function getWebDriverCookie(string $name, string $path = '/', string $domain = null): ?WebDriverCookie
private function getWebDriverCookie(string $name, string $path = '/', ?string $domain = null): ?WebDriverCookie
{
try {
$cookie = $this->webDriver->manage()->getCookieNamed($name);
Expand Down
18 changes: 9 additions & 9 deletions src/DomCrawler/Crawler.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ final class Crawler extends BaseCrawler implements WebDriverElement
/**
* @param WebDriverElement[] $elements
*/
public function __construct(array $elements = [], WebDriver $webDriver = null, string $uri = null)
public function __construct(array $elements = [], ?WebDriver $webDriver = null, ?string $uri = null)
{
$this->uri = $uri;
$this->webDriver = $webDriver;
Expand Down Expand Up @@ -177,7 +177,7 @@ public function ancestors(): static
/**
* @see https://github.com/symfony/symfony/issues/26432
*/
public function children(string $selector = null): static
public function children(?string $selector = null): static
{
$xpath = 'child::*';
if (null !== $selector) {
Expand All @@ -203,7 +203,7 @@ public function nodeName(): string
return $this->getElementOrThrow()->getTagName();
}

public function text(string $default = null, bool $normalizeWhitespace = true): string
public function text(?string $default = null, bool $normalizeWhitespace = true): string
{
if (!$normalizeWhitespace) {
throw new \InvalidArgumentException('Panther only supports getting normalized text.');
Expand All @@ -220,7 +220,7 @@ public function text(string $default = null, bool $normalizeWhitespace = true):
}
}

public function html(string $default = null): string
public function html(?string $default = null): string
{
try {
$element = $this->getElementOrThrow();
Expand Down Expand Up @@ -274,19 +274,19 @@ public function filter($selector): static
public function selectLink($value): static
{
return $this->selectFromXpath(
sprintf('descendant-or-self::a[contains(concat(\' \', normalize-space(string(.)), \' \'), %1$s) or ./img[contains(concat(\' \', normalize-space(string(@alt)), \' \'), %1$s)]]', self::xpathLiteral(' '.$value.' '))
\sprintf('descendant-or-self::a[contains(concat(\' \', normalize-space(string(.)), \' \'), %1$s) or ./img[contains(concat(\' \', normalize-space(string(@alt)), \' \'), %1$s)]]', self::xpathLiteral(' '.$value.' '))
);
}

public function selectImage($value): static
{
return $this->selectFromXpath(sprintf('descendant-or-self::img[contains(normalize-space(string(@alt)), %s)]', self::xpathLiteral($value)));
return $this->selectFromXpath(\sprintf('descendant-or-self::img[contains(normalize-space(string(@alt)), %s)]', self::xpathLiteral($value)));
}

public function selectButton($value): static
{
return $this->selectFromXpath(
sprintf(
\sprintf(
'descendant-or-self::input[((contains(%1$s, "submit") or contains(%1$s, "button")) and contains(concat(\' \', normalize-space(string(@value)), \' \'), %2$s)) or (contains(%1$s, "image") and contains(concat(\' \', normalize-space(string(@alt)), \' \'), %2$s)) or @id=%3$s or @name=%3$s] | descendant-or-self::button[contains(concat(\' \', normalize-space(string(.)), \' \'), %2$s) or @id=%3$s or @name=%3$s]',
'translate(@type, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz")',
self::xpathLiteral(' '.$value.' '),
Expand Down Expand Up @@ -330,7 +330,7 @@ public function images(): array
return $images;
}

public function form(array $values = null, $method = null): Form
public function form(?array $values = null, $method = null): Form
{
$form = new Form($this->getElementOrThrow(), $this->webDriver);
if (null !== $values) {
Expand Down Expand Up @@ -393,7 +393,7 @@ private function selectFromXpath(string $xpath): self
/**
* @param WebDriverElement[]|null $nodes
*/
private function createSubCrawler(array $nodes = null): self
private function createSubCrawler(?array $nodes = null): self
{
return new self($nodes ?? [], $this->webDriver, $this->uri);
}
Expand Down
10 changes: 5 additions & 5 deletions src/DomCrawler/Field/ChoiceFormField.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function select($value): void
public function tick(): void
{
if ('checkbox' !== $type = $this->element->getAttribute('type')) {
throw new \LogicException(sprintf('You cannot tick "%s" as it is not a checkbox (%s).', $this->element->getAttribute('name'), $type));
throw new \LogicException(\sprintf('You cannot tick "%s" as it is not a checkbox (%s).', $this->element->getAttribute('name'), $type));
}

$this->setValue(true);
Expand All @@ -66,7 +66,7 @@ public function tick(): void
public function untick(): void
{
if ('checkbox' !== $type = $this->element->getAttribute('type')) {
throw new \LogicException(sprintf('You cannot tick "%s" as it is not a checkbox (%s).', $this->element->getAttribute('name'), $type));
throw new \LogicException(\sprintf('You cannot tick "%s" as it is not a checkbox (%s).', $this->element->getAttribute('name'), $type));
}

$this->setValue(false);
Expand Down Expand Up @@ -114,7 +114,7 @@ public function setValue($value): void
{
if (\is_bool($value)) {
if ('checkbox' !== $this->type) {
throw new \InvalidArgumentException(sprintf('Invalid argument of type "%s"', \gettype($value)));
throw new \InvalidArgumentException(\sprintf('Invalid argument of type "%s"', \gettype($value)));
}

if ($value) {
Expand Down Expand Up @@ -189,12 +189,12 @@ protected function initialize(): void
{
$tagName = $this->element->getTagName();
if ('input' !== $tagName && 'select' !== $tagName) {
throw new \LogicException(sprintf('A ChoiceFormField can only be created from an input or select tag (%s given).', $tagName));
throw new \LogicException(\sprintf('A ChoiceFormField can only be created from an input or select tag (%s given).', $tagName));
}

$type = strtolower((string) $this->element->getAttribute('type'));
if ('input' === $tagName && 'checkbox' !== $type && 'radio' !== $type) {
throw new \LogicException(sprintf('A ChoiceFormField can only be created from an input tag with a type of checkbox or radio (given type is %s).', $type));
throw new \LogicException(\sprintf('A ChoiceFormField can only be created from an input tag with a type of checkbox or radio (given type is %s).', $type));
}

$this->type = 'select' === $tagName ? 'select' : $type;
Expand Down
4 changes: 2 additions & 2 deletions src/DomCrawler/Field/FileFormField.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ protected function initialize(): void
{
$tagName = $this->element->getTagName();
if ('input' !== $tagName) {
throw new \LogicException(sprintf('A FileFormField can only be created from an input tag (%s given).', $tagName));
throw new \LogicException(\sprintf('A FileFormField can only be created from an input tag (%s given).', $tagName));
}

$type = strtolower($this->element->getAttribute('type'));
if ('file' !== $type) {
throw new \LogicException(sprintf('A FileFormField can only be created from an input tag with a type of file (given type is %s).', $type));
throw new \LogicException(\sprintf('A FileFormField can only be created from an input tag with a type of file (given type is %s).', $type));
}

$value = $this->element->getAttribute('value');
Expand Down
2 changes: 1 addition & 1 deletion src/DomCrawler/Field/InputFormField.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ protected function initialize(): void
{
$tagName = $this->element->getTagName();
if ('input' !== $tagName && 'button' !== $tagName) {
throw new \LogicException(sprintf('An InputFormField can only be created from an input or button tag (%s given).', $tagName));
throw new \LogicException(\sprintf('An InputFormField can only be created from an input or button tag (%s given).', $tagName));
}

$type = strtolower((string) $this->element->getAttribute('type'));
Expand Down
2 changes: 1 addition & 1 deletion src/DomCrawler/Field/TextareaFormField.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected function initialize(): void
{
$tagName = $this->element->getTagName();
if ('textarea' !== $tagName) {
throw new \LogicException(sprintf('A TextareaFormField can only be created from a textarea tag (%s given).', $tagName));
throw new \LogicException(\sprintf('A TextareaFormField can only be created from a textarea tag (%s given).', $tagName));
}
}
}
8 changes: 4 additions & 4 deletions src/DomCrawler/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private function setElement(WebDriverElement $element): void
try {
$form = $this->webDriver->findElement(WebDriverBy::id($formId));
} catch (NoSuchElementException $e) {
throw new \LogicException(sprintf('The selected node has an invalid form attribute (%s).', $formId));
throw new \LogicException(\sprintf('The selected node has an invalid form attribute (%s).', $formId));
}

$this->element = $form;
Expand All @@ -76,7 +76,7 @@ private function setElement(WebDriverElement $element): void
}
} while ('form' !== $element->getTagName());
} elseif ('form' !== $tagName = $element->getTagName()) {
throw new \LogicException(sprintf('Unable to submit on a "%s" tag.', $tagName));
throw new \LogicException(\sprintf('Unable to submit on a "%s" tag.', $tagName));
}

$this->element = $element;
Expand Down Expand Up @@ -166,7 +166,7 @@ public function getFiles(): array
continue;
}

if ($field instanceof Field\FileFormField) {
if ($field instanceof FileFormField) {
$files[$field->getName()] = $field->getValue();
}
}
Expand Down Expand Up @@ -270,7 +270,7 @@ protected function getRawUri(): string
private function getFormElement(string $name): WebDriverElement
{
return $this->element->findElement(WebDriverBy::xpath(
sprintf('.//input[@name=%1$s] | .//textarea[@name=%1$s] | .//select[@name=%1$s] | .//button[@name=%1$s] | .//input[@name=%2$s] | .//textarea[@name=%2$s] | .//select[@name=%2$s] | .//button[@name=%2$s]', XPathEscaper::escapeQuotes($name), XPathEscaper::escapeQuotes($name.'[]'))
\sprintf('.//input[@name=%1$s] | .//textarea[@name=%1$s] | .//select[@name=%1$s] | .//button[@name=%1$s] | .//input[@name=%2$s] | .//textarea[@name=%2$s] | .//select[@name=%2$s] | .//button[@name=%2$s]', XPathEscaper::escapeQuotes($name), XPathEscaper::escapeQuotes($name.'[]'))
));
}

Expand Down
2 changes: 1 addition & 1 deletion src/DomCrawler/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ final class Image extends BaseImage
public function __construct(WebDriverElement $element)
{
if ('img' !== $tagName = $element->getTagName()) {
throw new \LogicException(sprintf('Unable to visualize a "%s" tag.', $tagName));
throw new \LogicException(\sprintf('Unable to visualize a "%s" tag.', $tagName));
}

$this->element = $element;
Expand Down
2 changes: 1 addition & 1 deletion src/DomCrawler/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function __construct(WebDriverElement $element, string $currentUri)
{
$tagName = $element->getTagName();
if ('a' !== $tagName && 'area' !== $tagName && 'link' !== $tagName) {
throw new \LogicException(sprintf('Unable to navigate from a "%s" tag.', $tagName));
throw new \LogicException(\sprintf('Unable to navigate from a "%s" tag.', $tagName));
}

$this->element = $element;
Expand Down
2 changes: 1 addition & 1 deletion src/ExceptionThrower.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ trait ExceptionThrower
{
private function createNotSupportedException(string $method): \InvalidArgumentException
{
return new \InvalidArgumentException(sprintf('The "%s" method is not supported when using WebDriver.', $method));
return new \InvalidArgumentException(\sprintf('The "%s" method is not supported when using WebDriver.', $method));
}
}
14 changes: 7 additions & 7 deletions src/PantherTestCaseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public static function startWebServer(array $options = []): void
self::$webServerManager = new WebServerManager(...array_values($options));
self::$webServerManager->start();

self::$baseUri = sprintf('http://%s:%s', $options['hostname'], $options['port']);
self::$baseUri = \sprintf('http://%s:%s', $options['hostname'], $options['port']);
}

public static function isWebServerStarted(): bool
Expand Down Expand Up @@ -180,19 +180,19 @@ protected static function createPantherClient(array $options = [], array $kernel

$browserArguments = $options['browser_arguments'] ?? null;
if (null !== $browserArguments && !\is_array($browserArguments)) {
throw new \TypeError(sprintf('Expected key "browser_arguments" to be an array or null, "%s" given.', get_debug_type($browserArguments)));
throw new \TypeError(\sprintf('Expected key "browser_arguments" to be an array or null, "%s" given.', get_debug_type($browserArguments)));
}

if (PantherTestCase::FIREFOX === $browser) {
self::$pantherClients[0] = self::$pantherClient = Client::createFirefoxClient(null, $browserArguments, $managerOptions, self::$baseUri);
self::$pantherClients[0] = self::$pantherClient = PantherClient::createFirefoxClient(null, $browserArguments, $managerOptions, self::$baseUri);
} else {
try {
self::$pantherClients[0] = self::$pantherClient = Client::createChromeClient(null, $browserArguments, $managerOptions, self::$baseUri);
self::$pantherClients[0] = self::$pantherClient = PantherClient::createChromeClient(null, $browserArguments, $managerOptions, self::$baseUri);
} catch (\RuntimeException $e) {
if (PantherTestCase::CHROME === $browser) {
throw $e;
}
self::$pantherClients[0] = self::$pantherClient = Client::createFirefoxClient(null, $browserArguments, $managerOptions, self::$baseUri);
self::$pantherClients[0] = self::$pantherClient = PantherClient::createFirefoxClient(null, $browserArguments, $managerOptions, self::$baseUri);
}

if (null === $browser) {
Expand Down Expand Up @@ -236,7 +236,7 @@ protected static function createHttpBrowserClient(array $options = [], array $ke
if (null === self::$httpBrowserClient) {
$httpClientOptions = $options['http_client_options'] ?? [];
if (!\is_array($httpClientOptions)) {
throw new \TypeError(sprintf('Expected key "http_client_options" to be an array, "%s" given.', get_debug_type($httpClientOptions)));
throw new \TypeError(\sprintf('Expected key "http_client_options" to be an array, "%s" given.', get_debug_type($httpClientOptions)));
}

// The ScopingHttpClient can't be used cause the HttpBrowser only supports absolute URLs,
Expand All @@ -249,7 +249,7 @@ protected static function createHttpBrowserClient(array $options = [], array $ke
}

$urlComponents = parse_url(self::$baseUri);
self::$httpBrowserClient->setServerParameter('HTTP_HOST', sprintf('%s:%s', $urlComponents['host'], $urlComponents['port']));
self::$httpBrowserClient->setServerParameter('HTTP_HOST', \sprintf('%s:%s', $urlComponents['host'], $urlComponents['port']));
if ('https' === $urlComponents['scheme']) {
self::$httpBrowserClient->setServerParameter('HTTPS', 'true');
}
Expand Down
2 changes: 1 addition & 1 deletion src/ProcessManager/ChromeManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ final class ChromeManager implements BrowserManagerInterface
/**
* @throws \RuntimeException
*/
public function __construct(string $chromeDriverBinary = null, array $arguments = null, array $options = [])
public function __construct(?string $chromeDriverBinary = null, ?array $arguments = null, array $options = [])
{
$this->options = $options ? array_merge($this->getDefaultOptions(), $options) : $this->getDefaultOptions();
$this->process = $this->createProcess($chromeDriverBinary ?: $this->findChromeDriverBinary());
Expand Down
Loading

0 comments on commit 3011210

Please sign in to comment.