-
Notifications
You must be signed in to change notification settings - Fork 800
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
autoloader: Fix phan issues (#37608)
No real functionality changes. * `$io` parameter to `AutoloadGenerator` constructor is no longer optional. The class didn't work when omitted anyway. * Improve some phpdocs. * `PHP_Autoloader::load_class()` now always returns bool as documented. * Use `??` where applicable. * Replace `->withConsecutive()` in tests, PHPUnit deprecated it without any replacement besides rolling our own. * Fix some test logic.
- Loading branch information
Showing
16 changed files
with
172 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
projects/packages/autoloader/changelog/fix-phan-issues-in-autoloader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Significance: patch | ||
Type: fixed | ||
|
||
`AutoloadGenerator::__construct` no longer pretends `$io` is nullable. That never worked. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
projects/packages/autoloader/tests/php/lib/with-consecutive.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
/** | ||
* A function to replace PHPUnit's `->withConsecutive()`. | ||
* | ||
* @package automattic/jetpack-autoloader | ||
*/ | ||
|
||
use PHPUnit\Framework\Constraint\Callback; | ||
use PHPUnit\Framework\Constraint\Constraint; | ||
use PHPUnit\Framework\Constraint\IsEqual; | ||
|
||
/** | ||
* Reimplement `withConsecutive` for PHPUnit. | ||
* | ||
* Unfortunately PHPUnit deprecated withConsecutive with no replacement, so we | ||
* have to roll our own version. | ||
* | ||
* @see https://github.com/sebastianbergmann/phpunit/issues/4026 | ||
* | ||
* Where previously you'd have done like | ||
* ``` | ||
* ->withConsecutive( | ||
* [ first, call, args ], | ||
* [ second, call, args ], | ||
* [ and, so, on ] | ||
* ) | ||
* ``` | ||
* you can do like this now | ||
* ``` | ||
* ->with( ...with_consecutive( | ||
* [ first, call, args ], | ||
* [ second, call, args ], | ||
* [ and, so, on ] | ||
* ) ) | ||
* ``` | ||
* | ||
* @param array ...$args Sets of arguments as you'd have passed to `->withConsecutive()`. | ||
* @return Constraint[] Array of constraints to pass to `->with()`. | ||
* @throws InvalidArgumentException If arguments are invalid. | ||
*/ | ||
function with_consecutive( ...$args ) { | ||
if ( ! $args ) { | ||
throw new InvalidArgumentException( 'Must pass at least one set of arguments' ); | ||
} | ||
|
||
$ct = count( $args[0] ); | ||
$value_sets = array(); | ||
foreach ( $args as $group ) { | ||
if ( count( $group ) !== $ct ) { | ||
throw new InvalidArgumentException( 'All sets of arguments must be the same length' ); | ||
} | ||
for ( $i = 0; $i < $ct; $i++ ) { | ||
$value_sets[ $i ][] = $group[ $i ] instanceof Constraint ? $group[ $i ] : new IsEqual( $group[ $i ] ); | ||
} | ||
} | ||
$funcs = array(); | ||
for ( $i = 0; $i < $ct; $i++ ) { | ||
$funcs[] = new Callback( | ||
function ( $value ) use ( $value_sets, $i ) { | ||
static $set = null; | ||
$set = $set ?? $value_sets[ $i ]; // @phan-suppress-current-line PhanTypePossiblyInvalidDimOffset -- False positive. | ||
if ( ! $set ) { | ||
$n = count( $value_sets[ $i ] ); // @phan-suppress-current-line PhanTypePossiblyInvalidDimOffset -- False positive. | ||
throw new InvalidArgumentException( "More calls than argument sets. Use `->expects( \$this->exactly( $n ) )` or the like when mocking the method to avoid this." ); | ||
} | ||
$expect = array_shift( $set ); | ||
$expect->evaluate( $value ); | ||
return true; | ||
} | ||
); | ||
} | ||
return $funcs; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.