Skip to content

Commit

Permalink
Upgrade phpstan; increase phpstan level; fix a few bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
uuf6429 committed Jan 4, 2025
1 parent 5a53ec5 commit 0d74697
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 23 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
},
"require-dev": {
"mink/driver-testsuite": "dev-master",
"phpstan/phpstan": "^1.10",
"phpstan/phpstan-phpunit": "^1.3",
"phpstan/phpstan": "^2",
"phpstan/phpstan-phpunit": "^2",
"phpunit/phpunit": "^9.6.8",
"symfony/error-handler": "^5.4 || ^6.0 || ^7.0",
"symfony/process": "^5.4 || ^6.0 || ^7.0",
Expand Down
12 changes: 11 additions & 1 deletion phpstan.dist.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,21 @@ includes:
- vendor/phpstan/phpstan-phpunit/rules.neon

parameters:
level: 9
level: 10
paths:
- src
- tests
ignoreErrors:
-
message: '#^Method Mink\\WebdriverClassicDriver\\WebdriverClassicDriver\:\:getWindowHandleFromName\(\) should return string but returns mixed\.$#'
identifier: return.type
count: 1
path: src/WebdriverClassicDriver.php
-
message: '#^Parameter \#1 \$handle of method Facebook\\WebDriver\\Remote\\RemoteTargetLocator\:\:window\(\) expects string, mixed given\.$#'
identifier: argument.type
count: 3
path: src/WebdriverClassicDriver.php
-
# See: https://github.com/php-webdriver/php-webdriver/pull/1120
message: '#^Parameter \#1 \$seconds of method Facebook\\WebDriver\\WebDriverTimeouts\:\:implicitlyWait\(\) expects int, float\|int given\.$#'
Expand Down
28 changes: 12 additions & 16 deletions src/WebdriverClassicDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
* @phpstan-type TCapabilities array<string, mixed>
* @phpstan-type TElementValue array<array-key, mixed>|bool|mixed|string|null
* @phpstan-type TWebDriverInstantiator callable(string $driverHost, DesiredCapabilities $capabilities): RemoteWebDriver
* @phpstan-type TTimeouts array{script?: null|numeric, implicit?: null|numeric, page?: null|numeric, "page load"?: null|numeric, pageLoad?: null|numeric}
* @phpstan-type TCapabilities array<string, mixed>
* @phpstan-type TElementValue array<array-key, mixed>|bool|mixed|string|null
*/
class WebdriverClassicDriver extends CoreDriver
{
Expand Down Expand Up @@ -670,10 +673,6 @@ public function executeScript(
$this->getWebDriver()->executeScript($script);
}

/**
* {@inheritdoc}
* @return mixed
*/
public function evaluateScript(
#[Language('JavaScript')]
string $script
Expand Down Expand Up @@ -971,7 +970,7 @@ private function executeJsOnXpath(
* @throws DriverException
*/
private function executeJsOnElement(
RemoteWebElement $element,
WebDriverElement $element,
#[Language('JavaScript')]
string $script
) {
Expand Down Expand Up @@ -1044,7 +1043,7 @@ private function getWindowHandleFromName(string $name): string
}
}

private function clickOnElement(RemoteWebElement $element): void
private function clickOnElement(WebDriverElement $element): void
{
$element->getLocationOnScreenOnceScrolledIntoView();
$element->click();
Expand Down Expand Up @@ -1108,14 +1107,11 @@ private function withWindow(?string $name, callable $callback): void
*/
private function findElement(
#[Language('XPath')]
string $xpath,
?RemoteWebElement $parent = null
string $xpath
): RemoteWebElement {
try {
$finder = WebDriverBy::xpath($xpath);
return $parent
? $parent->findElement($finder)
: $this->getWebDriver()->findElement($finder);
return $this->getWebDriver()->findElement($finder);
} catch (\Throwable $e) {
throw new DriverException("Failed to find element: {$e->getMessage()}", 0, $e);
}
Expand All @@ -1124,7 +1120,7 @@ private function findElement(
/**
* @throws DriverException
*/
private function selectRadioValue(RemoteWebElement $element, string $value): void
private function selectRadioValue(WebDriverElement $element, string $value): void
{
try {
(new WebDriverRadios($element))->selectByValue($value);
Expand All @@ -1142,7 +1138,7 @@ private function selectRadioValue(RemoteWebElement $element, string $value): voi
/**
* @throws DriverException
*/
private function selectOptionOnElement(RemoteWebElement $element, string $value, bool $multiple = false): void
private function selectOptionOnElement(WebDriverElement $element, string $value, bool $multiple = false): void
{
try {
$select = new WebDriverSelect($element);
Expand Down Expand Up @@ -1172,7 +1168,7 @@ private function selectOptionOnElement(RemoteWebElement $element, string $value,
*
* @throws DriverException
*/
private function deselectAllOptions(RemoteWebElement $element): void
private function deselectAllOptions(WebDriverElement $element): void
{
try {
(new WebDriverSelect($element))->deselectAll();
Expand All @@ -1190,7 +1186,7 @@ private function deselectAllOptions(RemoteWebElement $element): void
* @throws DriverException
*/
private function ensureInputType(
RemoteWebElement $element,
WebDriverElement $element,
#[Language('XPath')]
string $xpath,
string $type,
Expand Down Expand Up @@ -1233,7 +1229,7 @@ private function jsonEncode($value, string $action, string $field): string
* @param mixed $value
* @throws DriverException
*/
private function setElementDomProperty(RemoteWebElement $element, string $property, $value): void
private function setElementDomProperty(WebDriverElement $element, string $property, $value): void
{
$this->executeJsOnElement(
$element,
Expand Down
5 changes: 4 additions & 1 deletion tests/WebdriverClassicConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ public static function getInstance(): self

public function createDriver(): WebdriverClassicDriver
{
$seleniumHost = $_SERVER['DRIVER_URL'];
$seleniumHost = $_SERVER['DRIVER_URL'] ?? null;
if (!is_string($seleniumHost)) {
throw new \RuntimeException('Selenium host must be specified (as a string) in $_SERVER[DRIVER_URL].');
}

return new WebdriverClassicDriver($this->getBrowserName(), [], $seleniumHost);
}
Expand Down
5 changes: 2 additions & 3 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@

use Symfony\Component\Process\Process;

$minkTestServerPort = isset($_SERVER['WEB_FIXTURES_HOST'])
? parse_url($_SERVER['WEB_FIXTURES_HOST'], PHP_URL_PORT)
: '8002';
$fixturesHost = $_SERVER['WEB_FIXTURES_HOST'] ?? '';
$minkTestServerPort = parse_url(is_string($fixturesHost) ? $fixturesHost : '', PHP_URL_PORT) ?: '8002';

$minkTestServer = new Process([
PHP_BINARY,
Expand Down

0 comments on commit 0d74697

Please sign in to comment.