Skip to content

Commit d549552

Browse files
authored
Merge pull request #100 from uuf6429/chore/phpstan-improvements
PHPStan improvements
1 parent afd1e9d commit d549552

20 files changed

+148
-106
lines changed

http-kernel-fixtures/cookie_page2.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<body>
1010
Previous cookie: <?php
1111
/** @var \Symfony\Component\HttpFoundation\Request $request */
12-
echo $request->cookies->has('srvr_cookie') ? html_escape_value($request->cookies->get('srvr_cookie')) : 'NO';
12+
echo $request->cookies->has('srvr_cookie') ? html_escape_value($request->cookies->get('srvr_cookie') ?? '') : 'NO';
1313
?>
1414
</body>
1515
</html>

http-kernel-fixtures/sub-folder/cookie_page1.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
/** @var \Symfony\Component\HttpFoundation\Request $request */
33
$requestUri = $request->server->get('REQUEST_URI');
4+
assert(is_string($requestUri));
45
$resp = new Symfony\Component\HttpFoundation\Response();
56
$cook = Symfony\Component\HttpFoundation\Cookie::create('srvr_cookie', 'srv_var_is_set_sub_folder', 0, dirname($requestUri));
67
$resp->headers->setCookie($cook);

http-kernel-fixtures/sub-folder/cookie_page4.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<?php
22
/** @var \Symfony\Component\HttpFoundation\Request $request */
33
$resp = new Symfony\Component\HttpFoundation\Response();
4-
$cookiePath = dirname($request->server->get('REQUEST_URI')) . '/';
4+
$requestUri = $request->server->get('REQUEST_URI');
5+
assert(is_string($requestUri));
6+
$cookiePath = dirname($requestUri) . '/';
57
$cookie = Symfony\Component\HttpFoundation\Cookie::create('srvr_cookie', 'srv_var_is_set', 0, $cookiePath);
68
$resp->headers->setCookie($cookie);
79
?>

phpstan.dist.neon

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
parameters:
2-
level: 8
2+
level: 9
33
paths:
44
- src
55
- tests
66
- http-kernel-fixtures
77
- web-fixtures
8-
checkMissingIterableValueType: false
98

109
includes:
1110
- vendor/phpstan/phpstan-phpunit/extension.neon

src/FixturesKernel.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Behat\Mink\Tests\Driver\Util;
44

5+
use Behat\Mink\Tests\Driver\TestCase;
56
use Symfony\Component\HttpFoundation\Cookie;
67
use Symfony\Component\HttpFoundation\Request;
78
use Symfony\Component\HttpFoundation\Response;
@@ -25,8 +26,8 @@ public function handle(Request $request, $type = 1 /* self::MAIN_REQUEST */ , $c
2526

2627
private function handleFixtureRequest(Request $request): Response
2728
{
28-
$fixturesDir = realpath(__DIR__ . '/../web-fixtures');
29-
$overwriteDir = realpath(__DIR__ . '/../http-kernel-fixtures');
29+
$fixturesDir = realpath(TestCase::WEB_FIXTURES_DIR);
30+
$overwriteDir = realpath(TestCase::KERNEL_FIXTURES_DIR);
3031

3132
require_once $fixturesDir . '/utils.php';
3233

@@ -61,7 +62,7 @@ private function prepareSession(Request $request): void
6162
$cookies = $request->cookies;
6263

6364
if ($cookies->has($session->getName())) {
64-
$session->setId($cookies->get($session->getName()));
65+
$session->setId((string) $cookies->get($session->getName()));
6566
} else {
6667
$session->migrate(false);
6768
}

tests/AbstractConfig.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ abstract public function createDriver();
2222
*/
2323
public function mapRemoteFilePath($file)
2424
{
25-
if (!isset($_SERVER['TEST_MACHINE_BASE_PATH']) || !isset($_SERVER['DRIVER_MACHINE_BASE_PATH'])) {
25+
if (!isset($_SERVER['TEST_MACHINE_BASE_PATH'], $_SERVER['DRIVER_MACHINE_BASE_PATH'])) {
2626
return $file;
2727
}
2828

tests/Basic/BasicAuthTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@ public function testSetBasicAuth(string $user, string $pass, string $pageText):
2020
$this->assertStringContainsString($pageText, $session->getPage()->getContent());
2121
}
2222

23+
/**
24+
* @return iterable<string, array{string, string, string}>
25+
*/
2326
public static function setBasicAuthDataProvider(): iterable
2427
{
25-
return [
26-
['mink-user', 'mink-password', 'is authenticated'],
27-
['', '', 'is not authenticated'],
28-
];
28+
yield 'valid credentials' => ['mink-user', 'mink-password', 'is authenticated'];
29+
yield 'no credentials' => ['', '', 'is not authenticated'];
2930
}
3031

3132
public function testBasicAuthInUrl(): void

tests/Basic/BestPracticesTest.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ public function testImplementFindXpath(): void
2424
{
2525
$driver = $this->createDriver();
2626

27-
$this->assertNotImplementMethod('find', $driver, 'The driver should overwrite `findElementXpaths` rather than `find` for forward compatibility with Mink 2.');
28-
$this->assertImplementMethod('findElementXpaths', $driver, 'The driver must be able to find elements.');
29-
$this->assertNotImplementMethod('setSession', $driver, 'The driver should not deal with the Session directly for forward compatibility with Mink 2.');
27+
$this->assertMethodIsNotImplemented('find', $driver, 'The driver should overwrite `findElementXpaths` rather than `find` for forward compatibility with Mink 2.');
28+
$this->assertMethodIsImplemented('findElementXpaths', $driver, 'The driver must be able to find elements.');
29+
$this->assertMethodIsNotImplemented('setSession', $driver, 'The driver should not deal with the Session directly for forward compatibility with Mink 2.');
3030
}
3131

3232
/**
@@ -36,9 +36,12 @@ public function testImplementBasicApi(string $method): void
3636
{
3737
$driver = $this->createDriver();
3838

39-
$this->assertImplementMethod($method, $driver, 'The driver is unusable when this method is not implemented.');
39+
$this->assertMethodIsImplemented($method, $driver, 'The driver is unusable when this method is not implemented.');
4040
}
4141

42+
/**
43+
* @return iterable<array{string}>
44+
*/
4245
public static function provideRequiredMethods(): iterable
4346
{
4447
return [
@@ -53,7 +56,7 @@ public static function provideRequiredMethods(): iterable
5356
];
5457
}
5558

56-
private function assertImplementMethod(string $method, object $object, string $reason = ''): void
59+
private function assertMethodIsImplemented(string $method, object $object, string $reason = ''): void
5760
{
5861
$ref = new \ReflectionClass(get_class($object));
5962
$refMethod = $ref->getMethod($method);
@@ -67,7 +70,7 @@ private function assertImplementMethod(string $method, object $object, string $r
6770
$this->assertNotSame(CoreDriver::class, $refMethod->getDeclaringClass()->name, $message);
6871
}
6972

70-
private function assertNotImplementMethod(string $method, object $object, string $reason = ''): void
73+
private function assertMethodIsNotImplemented(string $method, object $object, string $reason = ''): void
7174
{
7275
$ref = new \ReflectionClass(get_class($object));
7376
$refMethod = $ref->getMethod($method);

tests/Basic/ContentTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ public function testGetAttribute(string $attributeName, ?string $attributeValue)
6161
$this->assertSame($attributeValue, $element->getAttribute($attributeName));
6262
}
6363

64+
/**
65+
* @return iterable<array{string, mixed}>
66+
*/
6467
public static function getAttributeDataProvider(): iterable
6568
{
6669
return [

tests/Basic/CookieTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
use Behat\Mink\Tests\Driver\TestCase;
66

7+
/**
8+
* @phpstan-type TCookieRemovalMode 'session_reset'|'cookie_delete'
9+
*/
710
final class CookieTest extends TestCase
811
{
912
/**
@@ -87,6 +90,9 @@ public function testCookieWithPaths(string $cookieRemovalMode): void
8790
$this->assertStringContainsString('Previous cookie: NO', $session->getPage()->getText());
8891
}
8992

93+
/**
94+
* @phpstan-return iterable<array{TCookieRemovalMode}>
95+
*/
9096
public static function cookieWithPathsDataProvider(): iterable
9197
{
9298
return [
@@ -96,6 +102,7 @@ public static function cookieWithPathsDataProvider(): iterable
96102
}
97103

98104
/**
105+
* @phpstan-param TCookieRemovalMode $cookieRemovalMode
99106
* @dataProvider cookieWithPathsDataProvider
100107
*/
101108
public function testCookieInSubPath(string $cookieRemovalMode): void

tests/Basic/IFrameTest.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,11 @@ public function testIFrame(string $iframeIdentifier, string $elementSelector, st
2929
}
3030

3131
/**
32-
* @return array
32+
* @return iterable<string, array{string, string, string}>
3333
*/
34-
public static function iFrameDataProvider()
34+
public static function iFrameDataProvider(): iterable
3535
{
36-
return array(
37-
'by name' => array('subframe_by_name', '#text', 'iFrame div text'),
38-
'by id' => array('subframe_by_id', '#foobar', 'Some accentués characters'),
39-
);
36+
yield 'by name' => ['subframe_by_name', '#text', 'iFrame div text'];
37+
yield 'by id' => ['subframe_by_id', '#foobar', 'Some accentués characters'];
4038
}
4139
}

tests/Form/GeneralTest.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,15 @@ public function testFormSubmitWays(string $submitVia): void
7777
}
7878
}
7979

80+
/**
81+
* @return iterable<array{string}>
82+
*/
8083
public static function formSubmitWaysDataProvider(): iterable
8184
{
82-
return [
83-
['Save'],
84-
['input-type-image'],
85-
['button-without-type'],
86-
['button-type-submit'],
87-
];
85+
yield ['Save'];
86+
yield ['input-type-image'];
87+
yield ['button-without-type'];
88+
yield ['button-type-submit'];
8889
}
8990

9091
public function testFormSubmit(): void
@@ -189,7 +190,7 @@ public function testAdvancedForm(): void
189190
$notes->setValue('new notes');
190191
$this->assertEquals('new notes', $notes->getValue());
191192

192-
$about->attachFile($this->mapRemoteFilePath(__DIR__ . '/../../web-fixtures/some_file.txt'));
193+
$about->attachFile($this->mapRemoteFilePath(self::WEB_FIXTURES_DIR . '/some_file.txt'));
193194

194195
$button = $page->findButton('Register');
195196
$this->assertNotNull($button);
@@ -352,7 +353,7 @@ public function testSubmitEmptyTextarea(): void
352353
/**
353354
* @dataProvider provideInvalidValues
354355
*
355-
* @param mixed $value
356+
* @param array<array-key, mixed>|bool|string $value
356357
*/
357358
public function testSetInvalidValueInField(string $field, $value): void
358359
{
@@ -366,6 +367,9 @@ public function testSetInvalidValueInField(string $field, $value): void
366367
$color->setValue($value);
367368
}
368369

370+
/**
371+
* @return iterable<string, array{string, mixed}>
372+
*/
369373
public static function provideInvalidValues(): iterable
370374
{
371375
$trueValue = ['true', true];

tests/Form/Html5Test.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public function testHtml5FormMethod(): void
163163
/**
164164
* @dataProvider provideInvalidValues
165165
*
166-
* @param mixed $value
166+
* @param array<array-key, mixed>|bool|string $value
167167
*/
168168
public function testSetInvalidValueInField(string $field, $value): void
169169
{
@@ -177,6 +177,9 @@ public function testSetInvalidValueInField(string $field, $value): void
177177
$color->setValue($value);
178178
}
179179

180+
/**
181+
* @return iterable<string, array{string, mixed}>
182+
*/
180183
public static function provideInvalidValues(): iterable
181184
{
182185
$trueValue = ['true', true];

tests/Form/RadioTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ public function testSetBooleanValue(bool $value): void
112112
$option->setValue($value);
113113
}
114114

115+
/**
116+
* @return iterable<array{bool}>
117+
*/
115118
public static function provideBooleanValues(): iterable
116119
{
117120
yield [true];

tests/Form/SelectTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ public function testElementSelectedStateCheck(string $selectName, string $option
7878
$this->assertTrue($option->isSelected());
7979
}
8080

81+
/**
82+
* @return iterable<array{string, string, string}>
83+
*/
8184
public static function elementSelectedStateCheckDataProvider(): iterable
8285
{
8386
return [
@@ -119,6 +122,9 @@ public function testSetBooleanValue(bool $value): void
119122
$select->setValue($value);
120123
}
121124

125+
/**
126+
* @return iterable<array{bool}>
127+
*/
122128
public static function provideBooleanValues(): iterable
123129
{
124130
yield [true];

tests/Js/ChangeEventTest.php

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,18 @@ public function testSetValueChangeEvent(string $elementId, string $valueForEmpty
6363
}
6464
}
6565

66+
/**
67+
* @return iterable<string, array{string, string, string}>
68+
*/
6669
public static function setValueChangeEventDataProvider(): iterable
6770
{
68-
$file1 = __DIR__ . '/../../web-fixtures/file1.txt';
69-
$file2 = __DIR__ . '/../../web-fixtures/file2.txt';
70-
71-
return [
72-
'input default' => ['the-input-default', 'from empty', 'from existing'],
73-
'input text' => ['the-input-text', 'from empty', 'from existing'],
74-
'input email' => ['the-email', 'from empty', 'from existing'],
75-
'textarea' => ['the-textarea', 'from empty', 'from existing'],
76-
'file' => ['the-file', $file1, $file2],
77-
'select' => ['the-select', '30'],
78-
'radio' => ['the-radio-m', 'm'],
79-
];
71+
yield 'input default' => ['the-input-default', 'from empty', 'from existing'];
72+
yield 'input text' => ['the-input-text', 'from empty', 'from existing'];
73+
yield 'input email' => ['the-email', 'from empty', 'from existing'];
74+
yield 'textarea' => ['the-textarea', 'from empty', 'from existing'];
75+
yield 'file' => ['the-file', self::WEB_FIXTURES_DIR . '/file1.txt', self::WEB_FIXTURES_DIR . '/file2.txt'];
76+
yield 'select' => ['the-select', '30', ''];
77+
yield 'radio' => ['the-radio-m', 'm', ''];
8078
}
8179

8280
/**
@@ -95,12 +93,13 @@ public function testSelectOptionChangeEvent(string $elementId, string $elementVa
9593
$this->assertElementChangeCount($elementId);
9694
}
9795

96+
/**
97+
* @return iterable<string, array{string, string}>
98+
*/
9899
public static function selectOptionChangeEventDataProvider(): iterable
99100
{
100-
return [
101-
'select' => ['the-select', '30'],
102-
'radio' => ['the-radio-m', 'm'],
103-
];
101+
yield 'select' => ['the-select', '30'];
102+
yield 'radio' => ['the-radio-m', 'm'];
104103
}
105104

106105
/**
@@ -145,12 +144,13 @@ public function testUncheckChangeEvent(bool $useSetValue): void
145144
$this->assertElementChangeCount('the-checked-checkbox');
146145
}
147146

147+
/**
148+
* @return iterable<array{mixed}>
149+
*/
148150
public static function checkboxTestWayDataProvider(): iterable
149151
{
150-
return [
151-
[true],
152-
[false],
153-
];
152+
yield [true];
153+
yield [false];
154154
}
155155

156156
private function assertElementChangeCount(string $elementId, string $message = ''): void

tests/Js/EventsTest.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,16 @@ public function testKeyboardEvents(?string $modifier, string $eventProperties):
118118
$this->assertEquals('key upped:78 / ' . $eventProperties, $event->getText());
119119
}
120120

121+
/**
122+
* @return iterable<string, array{KeyModifier::*|null, string}>
123+
*/
121124
public static function provideKeyboardEventsModifiers(): iterable
122125
{
123-
return [
124-
'none' => [null, '0 / 0 / 0 / 0'],
125-
'alt' => [KeyModifier::ALT, '1 / 0 / 0 / 0'],
126-
// jQuery considers ctrl as being a metaKey in the normalized event
127-
'ctrl' => [KeyModifier::CTRL, '0 / 1 / 0 / 1'],
128-
'shift' => [KeyModifier::SHIFT, '0 / 0 / 1 / 0'],
129-
'meta' => [KeyModifier::META, '0 / 0 / 0 / 1'],
130-
];
126+
yield 'none' => [null, '0 / 0 / 0 / 0'];
127+
yield 'alt' => [KeyModifier::ALT, '1 / 0 / 0 / 0'];
128+
// jQuery considers ctrl as being a metaKey in the normalized event
129+
yield 'ctrl' => [KeyModifier::CTRL, '0 / 1 / 0 / 1'];
130+
yield 'shift' => [KeyModifier::SHIFT, '0 / 0 / 1 / 0'];
131+
yield 'meta' => [KeyModifier::META, '0 / 0 / 0 / 1'];
131132
}
132133
}

0 commit comments

Comments
 (0)