From 0d75b54b197938f7884b7db2d6b621d3e1deb78e Mon Sep 17 00:00:00 2001 From: Christian Sciberras Date: Sun, 5 Jan 2025 16:54:36 +0100 Subject: [PATCH] Fix various phpstan warnings (#57) --- .editorconfig | 3 +++ phpstan.dist.neon | 37 ++++++++++++++++++++++++--------- src/WebdriverClassicDriver.php | 25 ++++++++++++++++++---- tests/Custom/CapabilityTest.php | 10 +++++++-- tests/Custom/TimeoutTest.php | 3 +++ 5 files changed, 62 insertions(+), 16 deletions(-) diff --git a/.editorconfig b/.editorconfig index 9cd5d20..879a1c1 100644 --- a/.editorconfig +++ b/.editorconfig @@ -14,3 +14,6 @@ indent_size = 2 [*.php] ij_php_align_multiline_parameters = false + +[*.neon] +indent_style = tab diff --git a/phpstan.dist.neon b/phpstan.dist.neon index db774f4..7b42960 100644 --- a/phpstan.dist.neon +++ b/phpstan.dist.neon @@ -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 diff --git a/src/WebdriverClassicDriver.php b/src/WebdriverClassicDriver.php index 97976be..da24eae 100644 --- a/src/WebdriverClassicDriver.php +++ b/src/WebdriverClassicDriver.php @@ -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 + * @phpstan-type TElementValue array|bool|mixed|string|null * @phpstan-type TWebDriverInstantiator callable(string $driverHost, DesiredCapabilities $capabilities): RemoteWebDriver */ class WebdriverClassicDriver extends CoreDriver @@ -80,6 +83,9 @@ class WebdriverClassicDriver extends CoreDriver private DesiredCapabilities $desiredCapabilities; + /** + * @var TTimeouts + */ private array $timeouts = []; private string $webDriverHost; @@ -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( @@ -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')); } @@ -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')); } @@ -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); @@ -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 */ @@ -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 diff --git a/tests/Custom/CapabilityTest.php b/tests/Custom/CapabilityTest.php index 9abc275..ce129d0 100644 --- a/tests/Custom/CapabilityTest.php +++ b/tests/Custom/CapabilityTest.php @@ -5,13 +5,16 @@ use Mink\WebdriverClassicDriver\Tests\WebDriverMockingTrait; use Mink\WebdriverClassicDriver\WebdriverClassicDriver; +/** + * @phpstan-import-type TCapabilities from WebdriverClassicDriver + */ class CapabilityTest extends \PHPUnit\Framework\TestCase { use WebDriverMockingTrait; /** - * @param array $desiredCapabilities - * @param array $expectedCapabilities + * @param TCapabilities $desiredCapabilities + * @param TCapabilities $expectedCapabilities * * @dataProvider capabilitiesDataProvider */ @@ -35,6 +38,9 @@ function ($host, $capabilities) use (&$actualCapabilities, $mockWebDriver) { $this->assertSame($expectedCapabilities, $actualCapabilities); } + /** + * @return iterable + */ public static function capabilitiesDataProvider(): iterable { yield 'unknown browser starts with default driver capabilities' => [ diff --git a/tests/Custom/TimeoutTest.php b/tests/Custom/TimeoutTest.php index b8c49e9..9fe3ae6 100644 --- a/tests/Custom/TimeoutTest.php +++ b/tests/Custom/TimeoutTest.php @@ -65,6 +65,9 @@ public function testDeprecatedShortPageLoadTimeoutThrowsException(string $type): $this->driver->visit($this->pathTo('/page_load.php?sleep=2')); } + /** + * @return iterable + */ public static function deprecatedPageLoadDataProvider(): iterable { yield 'selenium 3 style' => ['type' => 'pageLoad'];