Skip to content

Commit e1a3029

Browse files
committed
Modernizer: add scalar + return type declarations (wherever possible)
In line with the new minimum of PHPUnit 7.x, the assertions in the PHPUnit Polyfills will now have both parameter as well as return type declarations (wherever possible considering the minimum supported PHP version of PHP 7.1). Additional type declarations will be added in future majors if/when the minimum supported PHP version allows for it. Includes removing polyfilled inline type checks for assertions which were introduced in PHPUnit with declared types, but for which the polyfills couldn't type the parameters prior to this. Includes minor adjustments to exception expectations in the tests to allow for the PHP native `TypeError`s - in contrast to the emulated ones. Notes: * `AssertObjectProperty`: the `string` type for the `$propertyName` parameter for the `assertObject[Not]HasProperty()` has not been applied as it would invalidate the type check (as we can't enforce `strict_types` for tests using the assertion). Refs: * sebastianbergmann/phpunit@852e540 * sebastianbergmann/phpunit@a7ab2b9
1 parent d6cf070 commit e1a3029

25 files changed

+133
-200
lines changed

.phpcs.xml.dist

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,18 @@
161161
<exclude-pattern>/tests/Polyfills/Fixtures/ValueObjectUnionReturnType\.php$</exclude-pattern>
162162
</rule>
163163

164+
165+
<!--
166+
#############################################################################
167+
TEMPORARY ADJUSTMENTS
168+
Adjustments which should be removed once the associated issue has been resolved.
169+
#############################################################################
170+
-->
171+
172+
<!-- Bug in PHPCS. This exclusion can be removed once PHPCS PR #750 has been merged and released.
173+
https://github.com/PHPCSStandards/PHP_CodeSniffer/pull/750 -->
174+
<rule ref="Squiz.Commenting.FunctionComment.IncorrectTypeHint">
175+
<exclude-pattern>/src/Polyfills/AssertContainsOnly\.php$</exclude-pattern>
176+
</rule>
177+
164178
</ruleset>

phpstan-bootstrap.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ final class Exporter {
2828
*
2929
* @return string
3030
*/
31-
public function export( $value, $indentation = 0 ) {}
31+
public function export( $value, int $indentation = 0 ) {}
3232
}
3333
}
3434

@@ -47,6 +47,6 @@ final class Exporter {
4747
*
4848
* @return string
4949
*/
50-
public function export( $value, $indentation = 0 ) {}
50+
public function export( $value, int $indentation = 0 ) {}
5151
}
5252
}

phpstan.neon.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ parameters:
3939
path: src/Polyfills/ExpectUserDeprecation.php
4040

4141
-
42-
message: '`^Call to static method PHPUnit\\Framework\\Assert::assertIsArray\(\) with mixed and mixed will always evaluate to false\.$`'
42+
message: '`^Call to static method PHPUnit\\Framework\\Assert::assertIsArray\(\) with mixed and string will always evaluate to false\.$`'
4343
count: 2
4444
path: src/Polyfills/AssertIsList.php
4545

phpunitpolyfills-autoload.php

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ final class Autoload {
3131
*
3232
* @return bool
3333
*/
34-
public static function load( $className ) {
34+
public static function load( string $className ): bool {
3535
// Only load classes belonging to this library.
3636
if ( \stripos( $className, 'Yoast\PHPUnitPolyfills' ) !== 0 ) {
3737
return false;
@@ -124,7 +124,7 @@ public static function load( $className ) {
124124
*
125125
* @return void
126126
*/
127-
public static function loadExpectExceptionMessageMatches() {
127+
public static function loadExpectExceptionMessageMatches(): void {
128128
if ( \method_exists( TestCase::class, 'expectExceptionMessageMatches' ) === false ) {
129129
// PHPUnit < 8.4.0.
130130
require_once __DIR__ . '/src/Polyfills/ExpectExceptionMessageMatches.php';
@@ -141,7 +141,7 @@ public static function loadExpectExceptionMessageMatches() {
141141
*
142142
* @return void
143143
*/
144-
public static function loadAssertFileEqualsSpecializations() {
144+
public static function loadAssertFileEqualsSpecializations(): void {
145145
if ( \method_exists( Assert::class, 'assertFileEqualsIgnoringCase' ) === false ) {
146146
// PHPUnit < 8.5.0.
147147
require_once __DIR__ . '/src/Polyfills/AssertFileEqualsSpecializations.php';
@@ -158,7 +158,7 @@ public static function loadAssertFileEqualsSpecializations() {
158158
*
159159
* @return void
160160
*/
161-
public static function loadEqualToSpecializations() {
161+
public static function loadEqualToSpecializations(): void {
162162
if ( \method_exists( Assert::class, 'equalToWithDelta' ) === false ) {
163163
// PHPUnit < 9.0.0.
164164
require_once __DIR__ . '/src/Polyfills/EqualToSpecializations.php';
@@ -175,7 +175,7 @@ public static function loadEqualToSpecializations() {
175175
*
176176
* @return void
177177
*/
178-
public static function loadAssertionRenames() {
178+
public static function loadAssertionRenames(): void {
179179
if ( \method_exists( Assert::class, 'assertMatchesRegularExpression' ) === false ) {
180180
// PHPUnit < 9.1.0.
181181
require_once __DIR__ . '/src/Polyfills/AssertionRenames.php';
@@ -192,7 +192,7 @@ public static function loadAssertionRenames() {
192192
*
193193
* @return void
194194
*/
195-
public static function loadAssertClosedResource() {
195+
public static function loadAssertClosedResource(): void {
196196
if ( \method_exists( Assert::class, 'assertIsClosedResource' ) === false ) {
197197
// PHPUnit < 9.3.0.
198198
require_once __DIR__ . '/src/Polyfills/AssertClosedResource.php';
@@ -209,7 +209,7 @@ public static function loadAssertClosedResource() {
209209
*
210210
* @return void
211211
*/
212-
public static function loadAssertObjectEquals() {
212+
public static function loadAssertObjectEquals(): void {
213213
if ( \method_exists( Assert::class, 'assertObjectEquals' ) === false ) {
214214
// PHPUnit < 9.4.0.
215215
require_once __DIR__ . '/src/Polyfills/AssertObjectEquals.php';
@@ -226,7 +226,7 @@ public static function loadAssertObjectEquals() {
226226
*
227227
* @return void
228228
*/
229-
public static function loadAssertIsList() {
229+
public static function loadAssertIsList(): void {
230230
if ( \method_exists( Assert::class, 'assertIsList' ) === false ) {
231231
// PHPUnit < 10.0.0.
232232
require_once __DIR__ . '/src/Polyfills/AssertIsList.php';
@@ -243,7 +243,7 @@ public static function loadAssertIsList() {
243243
*
244244
* @return void
245245
*/
246-
public static function loadAssertIgnoringLineEndings() {
246+
public static function loadAssertIgnoringLineEndings(): void {
247247
if ( \method_exists( Assert::class, 'assertStringEqualsStringIgnoringLineEndings' ) === false ) {
248248
// PHPUnit < 10.0.0.
249249
require_once __DIR__ . '/src/Polyfills/AssertIgnoringLineEndings.php';
@@ -260,7 +260,7 @@ public static function loadAssertIgnoringLineEndings() {
260260
*
261261
* @return void
262262
*/
263-
public static function loadAssertObjectProperty() {
263+
public static function loadAssertObjectProperty(): void {
264264
if ( \method_exists( Assert::class, 'assertObjectHasProperty' ) === false ) {
265265
// PHPUnit < 10.1.0.
266266
require_once __DIR__ . '/src/Polyfills/AssertObjectProperty.php';
@@ -277,7 +277,7 @@ public static function loadAssertObjectProperty() {
277277
*
278278
* @return void
279279
*/
280-
public static function loadAssertArrayWithListKeys() {
280+
public static function loadAssertArrayWithListKeys(): void {
281281
if ( \method_exists( Assert::class, 'assertArrayIsEqualToArrayOnlyConsideringListOfKeys' ) === false ) {
282282
// PHPUnit < 11.0.0.
283283
require_once __DIR__ . '/src/Polyfills/AssertArrayWithListKeys.php';
@@ -294,7 +294,7 @@ public static function loadAssertArrayWithListKeys() {
294294
*
295295
* @return void
296296
*/
297-
public static function loadExpectUserDeprecation() {
297+
public static function loadExpectUserDeprecation(): void {
298298
if ( \method_exists( TestCase::class, 'expectUserDeprecationMessage' ) === false ) {
299299
// PHPUnit < 11.0.0.
300300
require_once __DIR__ . '/src/Polyfills/ExpectUserDeprecation.php';
@@ -311,7 +311,7 @@ public static function loadExpectUserDeprecation() {
311311
*
312312
* @return void
313313
*/
314-
public static function loadAssertObjectNotEquals() {
314+
public static function loadAssertObjectNotEquals(): void {
315315
if ( \method_exists( Assert::class, 'assertObjectNotEquals' ) === false ) {
316316
// PHPUnit < 11.2.0.
317317
require_once __DIR__ . '/src/Polyfills/AssertObjectNotEquals.php';
@@ -328,7 +328,7 @@ public static function loadAssertObjectNotEquals() {
328328
*
329329
* @return void
330330
*/
331-
public static function loadAssertContainsOnly() {
331+
public static function loadAssertContainsOnly(): void {
332332
if ( \method_exists( Assert::class, 'assertContainsOnlyIterable' ) === false ) {
333333
// PHPUnit < 11.5.0.
334334
require_once __DIR__ . '/src/Polyfills/AssertContainsOnly.php';
@@ -344,7 +344,7 @@ public static function loadAssertContainsOnly() {
344344
*
345345
* @return void
346346
*/
347-
public static function loadTestCase() {
347+
public static function loadTestCase(): void {
348348
if ( \version_compare( self::getPHPUnitVersion(), '8.0.0', '<' ) ) {
349349
// PHPUnit < 8.0.0.
350350
require_once __DIR__ . '/src/TestCases/TestCasePHPUnitLte7.php';
@@ -360,7 +360,7 @@ public static function loadTestCase() {
360360
*
361361
* @return void
362362
*/
363-
public static function loadTestListenerDefaultImplementation() {
363+
public static function loadTestListenerDefaultImplementation(): void {
364364
require_once __DIR__ . '/src/TestListeners/TestListenerDefaultImplementationPHPUnitGte7.php';
365365
}
366366

@@ -369,7 +369,7 @@ public static function loadTestListenerDefaultImplementation() {
369369
*
370370
* @return string Version number as a string.
371371
*/
372-
public static function getPHPUnitVersion() {
372+
public static function getPHPUnitVersion(): string {
373373
if ( \class_exists( '\PHPUnit\Runner\Version' ) ) {
374374
return PHPUnit_Version::id();
375375
}

src/Exceptions/InvalidComparisonMethodException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ final class InvalidComparisonMethodException extends Exception {
1919
*
2020
* @return string
2121
*/
22-
public function __toString() {
22+
public function __toString(): string {
2323
return $this->getMessage() . \PHP_EOL;
2424
}
2525
}

src/Helpers/ComparatorValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ final class ComparatorValidator {
4848
*
4949
* @throws InvalidComparisonMethodException When the comparator method does not comply with the requirements.
5050
*/
51-
public static function isValid( $expected, $actual, $method = 'equals' ) {
51+
public static function isValid( $expected, $actual, string $method = 'equals' ): void {
5252
/*
5353
* Verify the method exists.
5454
*/

src/Helpers/ResourceHelper.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ final class ResourceHelper {
2828
*
2929
* @return bool
3030
*/
31-
public static function isResource( $actual ) {
31+
public static function isResource( $actual ): bool {
3232
return ( $actual !== null
3333
&& \is_scalar( $actual ) === false
3434
&& \is_array( $actual ) === false
@@ -42,7 +42,7 @@ public static function isResource( $actual ) {
4242
*
4343
* @return bool
4444
*/
45-
public static function isClosedResource( $actual ) {
45+
public static function isClosedResource( $actual ): bool {
4646
$type = \gettype( $actual );
4747

4848
/*
@@ -92,7 +92,7 @@ public static function isClosedResource( $actual ) {
9292
*
9393
* @return bool
9494
*/
95-
public static function isResourceStateReliable( $actual ) {
95+
public static function isResourceStateReliable( $actual ): bool {
9696
try {
9797
$type = @\get_resource_type( $actual );
9898

@@ -118,7 +118,7 @@ public static function isResourceStateReliable( $actual ) {
118118
*
119119
* @return bool
120120
*/
121-
public static function isIncompatiblePHPForLibXMLResources() {
121+
public static function isIncompatiblePHPForLibXMLResources(): bool {
122122
if ( \PHP_VERSION_ID >= 70100 && \PHP_VERSION_ID < 70134 ) {
123123
return true;
124124
}

src/Polyfills/AssertArrayWithListKeys.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ trait AssertArrayWithListKeys {
3737
*
3838
* @return void
3939
*/
40-
final public static function assertArrayIsEqualToArrayOnlyConsideringListOfKeys( array $expected, array $actual, array $keysToBeConsidered, string $message = '' ) {
40+
final public static function assertArrayIsEqualToArrayOnlyConsideringListOfKeys( array $expected, array $actual, array $keysToBeConsidered, string $message = '' ): void {
4141
$filteredExpected = [];
4242
foreach ( $keysToBeConsidered as $key ) {
4343
if ( isset( $expected[ $key ] ) ) {
@@ -68,7 +68,7 @@ final public static function assertArrayIsEqualToArrayOnlyConsideringListOfKeys(
6868
*
6969
* @return void
7070
*/
71-
final public static function assertArrayIsEqualToArrayIgnoringListOfKeys( array $expected, array $actual, array $keysToBeIgnored, string $message = '' ) {
71+
final public static function assertArrayIsEqualToArrayIgnoringListOfKeys( array $expected, array $actual, array $keysToBeIgnored, string $message = '' ): void {
7272
foreach ( $keysToBeIgnored as $key ) {
7373
unset( $expected[ $key ], $actual[ $key ] );
7474
}
@@ -89,7 +89,7 @@ final public static function assertArrayIsEqualToArrayIgnoringListOfKeys( array
8989
*
9090
* @return void
9191
*/
92-
final public static function assertArrayIsIdenticalToArrayOnlyConsideringListOfKeys( array $expected, array $actual, array $keysToBeConsidered, string $message = '' ) {
92+
final public static function assertArrayIsIdenticalToArrayOnlyConsideringListOfKeys( array $expected, array $actual, array $keysToBeConsidered, string $message = '' ): void {
9393
$keysToBeConsidered = \array_combine( $keysToBeConsidered, $keysToBeConsidered );
9494
$expected = \array_intersect_key( $expected, $keysToBeConsidered );
9595
$actual = \array_intersect_key( $actual, $keysToBeConsidered );
@@ -110,7 +110,7 @@ final public static function assertArrayIsIdenticalToArrayOnlyConsideringListOfK
110110
*
111111
* @return void
112112
*/
113-
final public static function assertArrayIsIdenticalToArrayIgnoringListOfKeys( array $expected, array $actual, array $keysToBeIgnored, string $message = '' ) {
113+
final public static function assertArrayIsIdenticalToArrayIgnoringListOfKeys( array $expected, array $actual, array $keysToBeIgnored, string $message = '' ): void {
114114
foreach ( $keysToBeIgnored as $key ) {
115115
unset( $expected[ $key ], $actual[ $key ] );
116116
}

src/Polyfills/AssertClosedResource.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ trait AssertClosedResource {
2727
*
2828
* @return void
2929
*/
30-
public static function assertIsClosedResource( $actual, $message = '' ) {
30+
public static function assertIsClosedResource( $actual, string $message = '' ): void {
3131
$exporter = self::getPHPUnitExporterObject();
3232
$msg = \sprintf( 'Failed asserting that %s is of type "resource (closed)"', $exporter->export( $actual ) );
3333

@@ -46,7 +46,7 @@ public static function assertIsClosedResource( $actual, $message = '' ) {
4646
*
4747
* @return void
4848
*/
49-
public static function assertIsNotClosedResource( $actual, $message = '' ) {
49+
public static function assertIsNotClosedResource( $actual, string $message = '' ): void {
5050
$exporter = self::getPHPUnitExporterObject();
5151
$type = $exporter->export( $actual );
5252
if ( $type === 'NULL' ) {
@@ -77,7 +77,7 @@ public static function assertIsNotClosedResource( $actual, $message = '' ) {
7777
*
7878
* @return bool
7979
*/
80-
public static function shouldClosedResourceAssertionBeSkipped( $actual ) {
80+
public static function shouldClosedResourceAssertionBeSkipped( $actual ): bool {
8181
return ( ResourceHelper::isResourceStateReliable( $actual ) === false );
8282
}
8383

0 commit comments

Comments
 (0)