Skip to content

Commit a84cbb1

Browse files
committed
Fix some issues reported by phpstan
1 parent d76cce7 commit a84cbb1

File tree

5 files changed

+43
-31
lines changed

5 files changed

+43
-31
lines changed

.github/workflows/static-analysis.yml

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,25 @@ jobs:
1919
with:
2020
php-version: '8.1'
2121
ini-values: memory_limit=-1, date.timezone='UTC'
22-
tools: phpcs
22+
tools: phpcs, phpstan
23+
24+
- name: Validate composer.json
25+
run: composer validate
26+
27+
- name: Install dependencies
28+
run: composer update
29+
30+
- name: Generate action files
31+
run: vendor/bin/codecept build
2332

2433
- name: Check production code style
2534
run: phpcs src/
2635

2736
- name: Check test code style
28-
run: phpcs tests/ --standard=tests/phpcs.xml
37+
run: phpcs tests/ --standard=tests/phpcs.xml
38+
39+
- name: Static analysis of production code
40+
run: phpstan analyze src/ --level=1
41+
42+
- name: Static analysis of test code
43+
run: phpstan analyze tests/

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@
3737
"classmap": [
3838
"tests/data/app/data.php",
3939
"tests/unit/Codeception/Constraints/TestedWebElement.php"
40-
]
40+
],
41+
"psr-4": {
42+
"Tests\\Web\\": "tests/web/"
43+
}
4144
},
4245
"config": {
4346
"classmap-authoritative": true

src/Codeception/Module/WebDriver.php

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -984,7 +984,7 @@ private function debugCookies(): void
984984
$result = [];
985985
$cookies = $this->webDriver->manage()->getCookies();
986986
foreach ($cookies as $cookie) {
987-
$result[] = is_array($cookie) ? $cookie : $cookie->toArray();
987+
$result[] = $cookie->toArray();
988988
}
989989

990990
$this->debugSection('Cookies', json_encode($result, JSON_THROW_ON_ERROR));
@@ -1228,7 +1228,7 @@ public function _findClickable(WebDriverSearchContext $page, $link): ?WebDriverE
12281228
return $this->matchFirstOrFail($page, $link);
12291229
}
12301230

1231-
$locator = static::xPathLiteral(trim((string) $link));
1231+
$locator = self::xPathLiteral(trim((string) $link));
12321232

12331233
// narrow
12341234
$xpath = Locator::combine(
@@ -1281,7 +1281,7 @@ protected function findFields($selector): array
12811281
return $fields;
12821282
}
12831283

1284-
$locator = static::xPathLiteral(trim((string) $selector));
1284+
$locator = self::xPathLiteral(trim((string) $selector));
12851285
// by text or label
12861286
$xpath = Locator::combine(
12871287
".//*[self::input | self::textarea | self::select][not(./@type = 'submit' or ./@type = 'image' or ./@type = 'hidden')][(((./@name = {$locator}) or ./@id = //label[contains(normalize-space(string(.)), {$locator})]/@for) or ./@placeholder = {$locator})]",
@@ -1354,7 +1354,7 @@ public function dontSeeLink(string $text, string $url = ''): void
13541354
}
13551355
}
13561356

1357-
private function filterNodesByHref(string $url, array $nodes): ?array
1357+
private function filterNodesByHref(string $url, array $nodes): array
13581358
{
13591359
//current uri can be relative, merging it with configured base url gives absolute url
13601360
$absoluteCurrentUrl = Uri::mergeUrls($this->_getUrl(), $this->_getCurrentUri());
@@ -1802,15 +1802,15 @@ protected function findCheckable(
18021802
return $this->matchFirstOrFail($this->getBaseElement(), $radioOrCheckbox);
18031803
}
18041804

1805-
$locator = static::xPathLiteral($radioOrCheckbox);
1805+
$locator = self::xPathLiteral($radioOrCheckbox);
18061806
if ($context instanceof WebDriverElement && $context->getTagName() === 'input') {
18071807
$contextType = $context->getAttribute('type');
18081808
if (!in_array($contextType, ['checkbox', 'radio'], true)) {
18091809
return null;
18101810
}
18111811

1812-
$nameLiteral = static::xPathLiteral($context->getAttribute('name'));
1813-
$typeLiteral = static::xPathLiteral($contextType);
1812+
$nameLiteral = self::xPathLiteral($context->getAttribute('name'));
1813+
$typeLiteral = self::xPathLiteral($contextType);
18141814
$inputLocatorFragment = "input[@type = {$typeLiteral}][@name = {$nameLiteral}]";
18151815
$xpath = Locator::combine(
18161816
"ancestor::form//{$inputLocatorFragment}[(@id = ancestor::form//label[contains(normalize-space(string(.)), {$locator})]/@for) or @placeholder = {$locator}]",
@@ -2221,11 +2221,9 @@ public function cancelPopup(): void
22212221
* Checks that the active JavaScript popup,
22222222
* as created by `window.alert`|`window.confirm`|`window.prompt`, contains the given string.
22232223
*
2224-
* @param $text
2225-
*
22262224
* @throws ModuleException
22272225
*/
2228-
public function seeInPopup($text): void
2226+
public function seeInPopup(string $text): void
22292227
{
22302228
if ($this->isPhantom()) {
22312229
throw new ModuleException($this, 'PhantomJS does not support working with popups');
@@ -2264,10 +2262,9 @@ public function dontSeeInPopup(string $text): void
22642262
/**
22652263
* Enters text into a native JavaScript prompt popup, as created by `window.prompt`.
22662264
*
2267-
* @param $keys
22682265
* @throws ModuleException
22692266
*/
2270-
public function typeInPopup($keys): void
2267+
public function typeInPopup(string $keys): void
22712268
{
22722269
if ($this->isPhantom()) {
22732270
throw new ModuleException($this, 'PhantomJS does not support working with popups');
@@ -2883,11 +2880,10 @@ public function waitForJS(string $script, int $timeout = 5): void
28832880
* $I->executeJS("window.alert(arguments[0])", ['Hello world']);
28842881
* ```
28852882
*
2886-
* @param $script
28872883
* @param array $arguments
28882884
* @return mixed
28892885
*/
2890-
public function executeJS($script, array $arguments = [])
2886+
public function executeJS(string $script, array $arguments = [])
28912887
{
28922888
return $this->webDriver->executeScript($script, $arguments);
28932889
}
@@ -2906,11 +2902,10 @@ public function executeJS($script, array $arguments = [])
29062902
* $I->executeAsyncJS('setTimeout(arguments[1], arguments[0])', [$seconds]);
29072903
* ```
29082904
*
2909-
* @param $script
29102905
* @param array $arguments
29112906
* @return mixed
29122907
*/
2913-
public function executeAsyncJS($script, array $arguments = [])
2908+
public function executeAsyncJS(string $script, array $arguments = [])
29142909
{
29152910
return $this->webDriver->executeAsyncScript($script, $arguments);
29162911
}
@@ -2934,7 +2929,7 @@ public function maximizeWindow(): void
29342929
* @param string|array|WebDriverBy $source (CSS ID or XPath)
29352930
* @param string|array|WebDriverBy $target (CSS ID or XPath)
29362931
*/
2937-
public function dragAndDrop(string $source, string $target): void
2932+
public function dragAndDrop($source, $target): void
29382933
{
29392934
$sourceNodes = $this->matchFirstOrFail($this->getBaseElement(), $source);
29402935
$targetNodes = $this->matchFirstOrFail($this->getBaseElement(), $target);
@@ -3123,7 +3118,7 @@ protected function getStrictLocator(array $by): WebDriverBy
31233118
return WebDriverBy::className($locator);
31243119
default:
31253120
throw new MalformedLocatorException(
3126-
"{$by} => {$locator}",
3121+
"{$type} => {$locator}",
31273122
"Strict locator can be either xpath, css, id, link, class, name: "
31283123
);
31293124
}
@@ -3440,10 +3435,8 @@ public function deleteSessionSnapshot($name)
34403435
* Check if the cookie domain matches the config URL.
34413436
*
34423437
* Taken from Guzzle\Cookie\SetCookie
3443-
*
3444-
* @param array|WebDriverCookie $cookie
34453438
*/
3446-
private function cookieDomainMatchesConfigUrl(WebDriverCookie $cookie): bool
3439+
private function cookieDomainMatchesConfigUrl(array|WebDriverCookie $cookie): bool
34473440
{
34483441
if (!isset($cookie['domain'])) {
34493442
return true;
@@ -3517,11 +3510,10 @@ public function openNewTab(): void
35173510
* <?php
35183511
* $I->seeNumberOfTabs(2);
35193512
* ```
3520-
* @param $number number of tabs
35213513
*/
3522-
public function seeNumberOfTabs($number): void
3514+
public function seeNumberOfTabs(int $number): void
35233515
{
3524-
$this->assertSame(count($this->webDriver->getWindowHandles()), $number);
3516+
$this->assertCount($number, $this->webDriver->getWindowHandles());
35253517
}
35263518

35273519
/**
@@ -3692,13 +3684,13 @@ protected function disableImplicitWait(): void
36923684
*
36933685
* Examples:
36943686
*
3695-
* echo static::xpathLiteral('foo " bar');
3687+
* echo self::xPathLiteral('foo " bar');
36963688
* //prints 'foo " bar'
36973689
*
3698-
* echo static::xpathLiteral("foo ' bar");
3690+
* echo self::xPathLiteral("foo ' bar");
36993691
* //prints "foo ' bar"
37003692
*
3701-
* echo static::xpathLiteral('a\'b"c');
3693+
* echo self::xPathLiteral('a\'b"c');
37023694
* //prints concat('a', "'", 'b"c')
37033695
*
37043696
* @return string Converted string

tests/web/TestsForWeb.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Codeception\Module\WebDriver;
88
use Codeception\Test\Unit;
99
use Codeception\Util\ActionSequence;
10+
use data;
1011

1112
/**
1213
* Author: davert

tests/web/WebDriverTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Codeception\Stub;
99
use Codeception\Stub\Expected;
1010
use Codeception\Util\Maybe;
11+
use data;
1112
use Facebook\WebDriver\Cookie;
1213
use Facebook\WebDriver\Remote\DesiredCapabilities;
1314
use Facebook\WebDriver\Remote\RemoteWebDriver;
@@ -580,7 +581,7 @@ public function testCreateCestScreenshotOnFail()
580581
]),
581582
]);
582583
$module = Stub::make(self::MODULE_CLASS, ['webDriver' => $fakeWd]);
583-
$cest = new \Codeception\Test\Cest(new stdClass(), 'login', 'someCest.php');
584+
$cest = new \Codeception\Test\Cest(new \stdClass(), 'login', 'someCest.php');
584585
$module->_failed($cest, new \PHPUnit\Framework\AssertionFailedError());
585586
}
586587

0 commit comments

Comments
 (0)