Skip to content

Commit

Permalink
Fix various phpstan warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
uuf6429 committed Jan 4, 2025
1 parent 817e92e commit e3fa72f
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 16 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ indent_size = 2

[*.php]
ij_php_align_multiline_parameters = false

[*.neon]
indent_style = tab
37 changes: 27 additions & 10 deletions phpstan.dist.neon
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
parameters:
level: 8
paths:
- src
- tests
checkMissingIterableValueType: false
treatPhpDocTypesAsCertain: false

includes:
- vendor/phpstan/phpstan-phpunit/extension.neon
- vendor/phpstan/phpstan-phpunit/rules.neon
- vendor/phpstan/phpstan-phpunit/extension.neon
- vendor/phpstan/phpstan-phpunit/rules.neon

parameters:
level: 8
paths:
- src
- tests
ignoreErrors:
-
# 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\.$#'
identifier: argument.type
count: 1
path: src/WebdriverClassicDriver.php
-
# See: https://github.com/php-webdriver/php-webdriver/pull/1120
message: '#^Parameter \#1 \$seconds of method Facebook\\WebDriver\\WebDriverTimeouts\:\:pageLoadTimeout\(\) expects int, float\|int given\.$#'
identifier: argument.type
count: 1
path: src/WebdriverClassicDriver.php
-
# See: https://github.com/php-webdriver/php-webdriver/pull/1120
message: '#^Parameter \#1 \$seconds of method Facebook\\WebDriver\\WebDriverTimeouts\:\:setScriptTimeout\(\) expects int, float\|int given\.$#'
identifier: argument.type
count: 1
path: src/WebdriverClassicDriver.php
25 changes: 21 additions & 4 deletions src/WebdriverClassicDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
use JetBrains\PhpStorm\Language;

/**
* @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
* @phpstan-type TWebDriverInstantiator callable(string $driverHost, DesiredCapabilities $capabilities): RemoteWebDriver
*/
class WebdriverClassicDriver extends CoreDriver
Expand Down Expand Up @@ -80,6 +83,9 @@ class WebdriverClassicDriver extends CoreDriver

private DesiredCapabilities $desiredCapabilities;

/**
* @var TTimeouts
*/
private array $timeouts = [];

private string $webDriverHost;
Expand All @@ -93,6 +99,7 @@ class WebdriverClassicDriver extends CoreDriver

/**
* @param string $browserName One of 'edge', 'firefox', 'chrome' or any one of {@see WebDriverBrowserType} constants.
* @param TCapabilities $desiredCapabilities
* @param TWebDriverInstantiator|null $webDriverInstantiator
*/
public function __construct(
Expand Down Expand Up @@ -340,12 +347,16 @@ public function getAttribute(
return $this->executeJsOnXpath($xpath, $script);
}

/**
* {@inheritdoc}
* @return TElementValue
*/
public function getValue(
#[Language('XPath')]
string $xpath
) {
$element = $this->findElement($xpath);
$widgetType = strtolower($element->getTagName() ?? '');
$widgetType = $element->getTagName();
if ($widgetType === 'input') {
$widgetType = strtolower((string)$element->getAttribute('type'));
}
Expand Down Expand Up @@ -380,13 +391,17 @@ public function getValue(
}
}

/**
* {@inheritdoc}
* @param TElementValue $value
*/
public function setValue(
#[Language('XPath')]
string $xpath,
$value
): void {
$element = $this->findElement($xpath);
$widgetType = strtolower($element->getTagName() ?? '');
$widgetType = $element->getTagName();
if ($widgetType === 'input') {
$widgetType = strtolower((string)$element->getAttribute('type'));
}
Expand Down Expand Up @@ -519,7 +534,7 @@ public function selectOption(
bool $multiple = false
): void {
$element = $this->findElement($xpath);
$tagName = strtolower($element->getTagName() ?? '');
$tagName = $element->getTagName();

if ($tagName === 'input' && strtolower((string)$element->getAttribute('type')) === 'radio') {
$this->selectRadioValue($element, $value);
Expand Down Expand Up @@ -747,7 +762,7 @@ public function getWebDriverSessionId(): ?string
/**
* Sets the timeouts to apply to the webdriver session
*
* @param array $timeouts The session timeout settings: Array of {script, implicit, page} => time in milliseconds
* @param TTimeouts $timeouts The session timeout settings: Array of {script, implicit, page} => time in milliseconds
* @throws DriverException
* @api
*/
Expand Down Expand Up @@ -805,6 +820,8 @@ private function getNormalisedBrowserName(): string
/**
* Detect and assign appropriate browser capabilities
*
* @param TCapabilities $desiredCapabilities
*
* @see https://github.com/SeleniumHQ/selenium/wiki/DesiredCapabilities
*/
private function initCapabilities(array $desiredCapabilities): DesiredCapabilities
Expand Down
10 changes: 8 additions & 2 deletions tests/Custom/CapabilityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
use Facebook\WebDriver\WebDriverTimeouts;
use Mink\WebdriverClassicDriver\WebdriverClassicDriver;

/**
* @phpstan-import-type TCapabilities from WebdriverClassicDriver
*/
class CapabilityTest extends \PHPUnit\Framework\TestCase
{
/**
* @param array<string, mixed> $desiredCapabilities
* @param array<string, mixed> $expectedCapabilities
* @param TCapabilities $desiredCapabilities
* @param TCapabilities $expectedCapabilities
*
* @dataProvider capabilitiesDataProvider
*/
Expand Down Expand Up @@ -39,6 +42,9 @@ function ($host, $capabilities) use (&$actualCapabilities, $mockWebDriver) {
$this->assertSame($expectedCapabilities, $actualCapabilities);
}

/**
* @return iterable<string, array{browserName: string, desiredCapabilities: TCapabilities, expectedCapabilities: TCapabilities}>
*/
public static function capabilitiesDataProvider(): iterable
{
yield 'unknown browser starts with default driver capabilities' => [
Expand Down
3 changes: 3 additions & 0 deletions tests/Custom/TimeoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ public function testDeprecatedShortPageLoadTimeoutThrowsException(string $type):
$this->driver->visit($this->pathTo('/page_load.php?sleep=2'));
}

/**
* @return iterable<string, array{type: string}>
*/
public static function deprecatedPageLoadDataProvider(): iterable
{
yield 'selenium 3 style' => ['type' => 'pageLoad'];
Expand Down

0 comments on commit e3fa72f

Please sign in to comment.