Skip to content

Commit

Permalink
Modernizer: add scalar + return type declarations (wherever possible)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jrfnl committed Jan 14, 2025
1 parent d6cf070 commit 80361f1
Show file tree
Hide file tree
Showing 24 changed files with 132 additions and 197 deletions.
14 changes: 14 additions & 0 deletions .phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,18 @@
<exclude-pattern>/tests/Polyfills/Fixtures/ValueObjectUnionReturnType\.php$</exclude-pattern>
</rule>


<!--
#############################################################################
TEMPORARY ADJUSTMENTS
Adjustments which should be removed once the associated issue has been resolved.
#############################################################################
-->

<!-- Bug in PHPCS. This exclusion can be removed once PHPCS PR #750 has been merged and released.
https://github.com/PHPCSStandards/PHP_CodeSniffer/pull/750 -->
<rule ref="Squiz.Commenting.FunctionComment.IncorrectTypeHint">
<exclude-pattern>/src/Polyfills/AssertContainsOnly\.php$</exclude-pattern>
</rule>

</ruleset>
4 changes: 2 additions & 2 deletions phpstan-bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ final class Exporter {
*
* @return string
*/
public function export( $value, $indentation = 0 ) {}
public function export( $value, int $indentation = 0 ) {}
}
}

Expand All @@ -47,6 +47,6 @@ final class Exporter {
*
* @return string
*/
public function export( $value, $indentation = 0 ) {}
public function export( $value, int $indentation = 0 ) {}
}
}
2 changes: 1 addition & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ parameters:
path: src/Polyfills/ExpectUserDeprecation.php

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

Expand Down
34 changes: 17 additions & 17 deletions phpunitpolyfills-autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ final class Autoload {
*
* @return bool
*/
public static function load( $className ) {
public static function load( string $className ): bool {
// Only load classes belonging to this library.
if ( \stripos( $className, 'Yoast\PHPUnitPolyfills' ) !== 0 ) {
return false;
Expand Down Expand Up @@ -124,7 +124,7 @@ public static function load( $className ) {
*
* @return void
*/
public static function loadExpectExceptionMessageMatches() {
public static function loadExpectExceptionMessageMatches(): void {
if ( \method_exists( TestCase::class, 'expectExceptionMessageMatches' ) === false ) {
// PHPUnit < 8.4.0.
require_once __DIR__ . '/src/Polyfills/ExpectExceptionMessageMatches.php';
Expand All @@ -141,7 +141,7 @@ public static function loadExpectExceptionMessageMatches() {
*
* @return void
*/
public static function loadAssertFileEqualsSpecializations() {
public static function loadAssertFileEqualsSpecializations(): void {
if ( \method_exists( Assert::class, 'assertFileEqualsIgnoringCase' ) === false ) {
// PHPUnit < 8.5.0.
require_once __DIR__ . '/src/Polyfills/AssertFileEqualsSpecializations.php';
Expand All @@ -158,7 +158,7 @@ public static function loadAssertFileEqualsSpecializations() {
*
* @return void
*/
public static function loadEqualToSpecializations() {
public static function loadEqualToSpecializations(): void {
if ( \method_exists( Assert::class, 'equalToWithDelta' ) === false ) {
// PHPUnit < 9.0.0.
require_once __DIR__ . '/src/Polyfills/EqualToSpecializations.php';
Expand All @@ -175,7 +175,7 @@ public static function loadEqualToSpecializations() {
*
* @return void
*/
public static function loadAssertionRenames() {
public static function loadAssertionRenames(): void {
if ( \method_exists( Assert::class, 'assertMatchesRegularExpression' ) === false ) {
// PHPUnit < 9.1.0.
require_once __DIR__ . '/src/Polyfills/AssertionRenames.php';
Expand All @@ -192,7 +192,7 @@ public static function loadAssertionRenames() {
*
* @return void
*/
public static function loadAssertClosedResource() {
public static function loadAssertClosedResource(): void {
if ( \method_exists( Assert::class, 'assertIsClosedResource' ) === false ) {
// PHPUnit < 9.3.0.
require_once __DIR__ . '/src/Polyfills/AssertClosedResource.php';
Expand All @@ -209,7 +209,7 @@ public static function loadAssertClosedResource() {
*
* @return void
*/
public static function loadAssertObjectEquals() {
public static function loadAssertObjectEquals(): void {
if ( \method_exists( Assert::class, 'assertObjectEquals' ) === false ) {
// PHPUnit < 9.4.0.
require_once __DIR__ . '/src/Polyfills/AssertObjectEquals.php';
Expand All @@ -226,7 +226,7 @@ public static function loadAssertObjectEquals() {
*
* @return void
*/
public static function loadAssertIsList() {
public static function loadAssertIsList(): void {
if ( \method_exists( Assert::class, 'assertIsList' ) === false ) {
// PHPUnit < 10.0.0.
require_once __DIR__ . '/src/Polyfills/AssertIsList.php';
Expand All @@ -243,7 +243,7 @@ public static function loadAssertIsList() {
*
* @return void
*/
public static function loadAssertIgnoringLineEndings() {
public static function loadAssertIgnoringLineEndings(): void {
if ( \method_exists( Assert::class, 'assertStringEqualsStringIgnoringLineEndings' ) === false ) {
// PHPUnit < 10.0.0.
require_once __DIR__ . '/src/Polyfills/AssertIgnoringLineEndings.php';
Expand All @@ -260,7 +260,7 @@ public static function loadAssertIgnoringLineEndings() {
*
* @return void
*/
public static function loadAssertObjectProperty() {
public static function loadAssertObjectProperty(): void {
if ( \method_exists( Assert::class, 'assertObjectHasProperty' ) === false ) {
// PHPUnit < 10.1.0.
require_once __DIR__ . '/src/Polyfills/AssertObjectProperty.php';
Expand All @@ -277,7 +277,7 @@ public static function loadAssertObjectProperty() {
*
* @return void
*/
public static function loadAssertArrayWithListKeys() {
public static function loadAssertArrayWithListKeys(): void {
if ( \method_exists( Assert::class, 'assertArrayIsEqualToArrayOnlyConsideringListOfKeys' ) === false ) {
// PHPUnit < 11.0.0.
require_once __DIR__ . '/src/Polyfills/AssertArrayWithListKeys.php';
Expand All @@ -294,7 +294,7 @@ public static function loadAssertArrayWithListKeys() {
*
* @return void
*/
public static function loadExpectUserDeprecation() {
public static function loadExpectUserDeprecation(): void {
if ( \method_exists( TestCase::class, 'expectUserDeprecationMessage' ) === false ) {
// PHPUnit < 11.0.0.
require_once __DIR__ . '/src/Polyfills/ExpectUserDeprecation.php';
Expand All @@ -311,7 +311,7 @@ public static function loadExpectUserDeprecation() {
*
* @return void
*/
public static function loadAssertObjectNotEquals() {
public static function loadAssertObjectNotEquals(): void {
if ( \method_exists( Assert::class, 'assertObjectNotEquals' ) === false ) {
// PHPUnit < 11.2.0.
require_once __DIR__ . '/src/Polyfills/AssertObjectNotEquals.php';
Expand All @@ -328,7 +328,7 @@ public static function loadAssertObjectNotEquals() {
*
* @return void
*/
public static function loadAssertContainsOnly() {
public static function loadAssertContainsOnly(): void {
if ( \method_exists( Assert::class, 'assertContainsOnlyIterable' ) === false ) {
// PHPUnit < 11.5.0.
require_once __DIR__ . '/src/Polyfills/AssertContainsOnly.php';
Expand All @@ -344,7 +344,7 @@ public static function loadAssertContainsOnly() {
*
* @return void
*/
public static function loadTestCase() {
public static function loadTestCase(): void {
if ( \version_compare( self::getPHPUnitVersion(), '8.0.0', '<' ) ) {
// PHPUnit < 8.0.0.
require_once __DIR__ . '/src/TestCases/TestCasePHPUnitLte7.php';
Expand All @@ -360,7 +360,7 @@ public static function loadTestCase() {
*
* @return void
*/
public static function loadTestListenerDefaultImplementation() {
public static function loadTestListenerDefaultImplementation(): void {
require_once __DIR__ . '/src/TestListeners/TestListenerDefaultImplementationPHPUnitGte7.php';
}

Expand All @@ -369,7 +369,7 @@ public static function loadTestListenerDefaultImplementation() {
*
* @return string Version number as a string.
*/
public static function getPHPUnitVersion() {
public static function getPHPUnitVersion(): string {
if ( \class_exists( '\PHPUnit\Runner\Version' ) ) {
return PHPUnit_Version::id();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/InvalidComparisonMethodException.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ final class InvalidComparisonMethodException extends Exception {
*
* @return string
*/
public function __toString() {
public function __toString(): string {
return $this->getMessage() . \PHP_EOL;
}
}
2 changes: 1 addition & 1 deletion src/Helpers/ComparatorValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ final class ComparatorValidator {
*
* @throws InvalidComparisonMethodException When the comparator method does not comply with the requirements.
*/
public static function isValid( $expected, $actual, $method = 'equals' ) {
public static function isValid( $expected, $actual, string $method = 'equals' ): void {
/*
* Verify the method exists.
*/
Expand Down
8 changes: 4 additions & 4 deletions src/Helpers/ResourceHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ final class ResourceHelper {
*
* @return bool
*/
public static function isResource( $actual ) {
public static function isResource( $actual ): bool {
return ( $actual !== null
&& \is_scalar( $actual ) === false
&& \is_array( $actual ) === false
Expand All @@ -42,7 +42,7 @@ public static function isResource( $actual ) {
*
* @return bool
*/
public static function isClosedResource( $actual ) {
public static function isClosedResource( $actual ): bool {
$type = \gettype( $actual );

/*
Expand Down Expand Up @@ -92,7 +92,7 @@ public static function isClosedResource( $actual ) {
*
* @return bool
*/
public static function isResourceStateReliable( $actual ) {
public static function isResourceStateReliable( $actual ): bool {
try {
$type = @\get_resource_type( $actual );

Expand All @@ -118,7 +118,7 @@ public static function isResourceStateReliable( $actual ) {
*
* @return bool
*/
public static function isIncompatiblePHPForLibXMLResources() {
public static function isIncompatiblePHPForLibXMLResources(): bool {
if ( \PHP_VERSION_ID >= 70100 && \PHP_VERSION_ID < 70134 ) {
return true;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Polyfills/AssertArrayWithListKeys.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ trait AssertArrayWithListKeys {
*
* @return void
*/
final public static function assertArrayIsEqualToArrayOnlyConsideringListOfKeys( array $expected, array $actual, array $keysToBeConsidered, string $message = '' ) {
final public static function assertArrayIsEqualToArrayOnlyConsideringListOfKeys( array $expected, array $actual, array $keysToBeConsidered, string $message = '' ): void {
$filteredExpected = [];
foreach ( $keysToBeConsidered as $key ) {
if ( isset( $expected[ $key ] ) ) {
Expand Down Expand Up @@ -68,7 +68,7 @@ final public static function assertArrayIsEqualToArrayOnlyConsideringListOfKeys(
*
* @return void
*/
final public static function assertArrayIsEqualToArrayIgnoringListOfKeys( array $expected, array $actual, array $keysToBeIgnored, string $message = '' ) {
final public static function assertArrayIsEqualToArrayIgnoringListOfKeys( array $expected, array $actual, array $keysToBeIgnored, string $message = '' ): void {
foreach ( $keysToBeIgnored as $key ) {
unset( $expected[ $key ], $actual[ $key ] );
}
Expand All @@ -89,7 +89,7 @@ final public static function assertArrayIsEqualToArrayIgnoringListOfKeys( array
*
* @return void
*/
final public static function assertArrayIsIdenticalToArrayOnlyConsideringListOfKeys( array $expected, array $actual, array $keysToBeConsidered, string $message = '' ) {
final public static function assertArrayIsIdenticalToArrayOnlyConsideringListOfKeys( array $expected, array $actual, array $keysToBeConsidered, string $message = '' ): void {
$keysToBeConsidered = \array_combine( $keysToBeConsidered, $keysToBeConsidered );
$expected = \array_intersect_key( $expected, $keysToBeConsidered );
$actual = \array_intersect_key( $actual, $keysToBeConsidered );
Expand All @@ -110,7 +110,7 @@ final public static function assertArrayIsIdenticalToArrayOnlyConsideringListOfK
*
* @return void
*/
final public static function assertArrayIsIdenticalToArrayIgnoringListOfKeys( array $expected, array $actual, array $keysToBeIgnored, string $message = '' ) {
final public static function assertArrayIsIdenticalToArrayIgnoringListOfKeys( array $expected, array $actual, array $keysToBeIgnored, string $message = '' ): void {
foreach ( $keysToBeIgnored as $key ) {
unset( $expected[ $key ], $actual[ $key ] );
}
Expand Down
6 changes: 3 additions & 3 deletions src/Polyfills/AssertClosedResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ trait AssertClosedResource {
*
* @return void
*/
public static function assertIsClosedResource( $actual, $message = '' ) {
public static function assertIsClosedResource( $actual, string $message = '' ): void {
$exporter = self::getPHPUnitExporterObject();
$msg = \sprintf( 'Failed asserting that %s is of type "resource (closed)"', $exporter->export( $actual ) );

Expand All @@ -46,7 +46,7 @@ public static function assertIsClosedResource( $actual, $message = '' ) {
*
* @return void
*/
public static function assertIsNotClosedResource( $actual, $message = '' ) {
public static function assertIsNotClosedResource( $actual, string $message = '' ): void {
$exporter = self::getPHPUnitExporterObject();
$type = $exporter->export( $actual );
if ( $type === 'NULL' ) {
Expand Down Expand Up @@ -77,7 +77,7 @@ public static function assertIsNotClosedResource( $actual, $message = '' ) {
*
* @return bool
*/
public static function shouldClosedResourceAssertionBeSkipped( $actual ) {
public static function shouldClosedResourceAssertionBeSkipped( $actual ): bool {
return ( ResourceHelper::isResourceStateReliable( $actual ) === false );
}

Expand Down
Loading

0 comments on commit 80361f1

Please sign in to comment.