Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix PHP 8.2 warning: Use [static::class, '…'] instead of 'static::… for callbacks #570

Merged
merged 7 commits into from
Dec 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,11 @@ parameters:
count: 1
path: src/Faker/Provider/Base.php

-
message: "#^Parameter \\#2 \\$callback of function preg_replace_callback expects callable\\(array\\<int\\|string, string\\>\\)\\: string, array\\{class\\-string\\<static\\(Faker\\\\Provider\\\\Base\\)\\>, 'randomDigit'\\} given\\.$#"
count: 1
path: src/Faker/Provider/Base.php

-
message: "#^Parameter \\$validator of method Faker\\\\Provider\\\\Base\\:\\:valid\\(\\) has invalid type Faker\\\\Provider\\\\Closure\\.$#"
count: 1
Expand Down Expand Up @@ -733,6 +738,11 @@ parameters:
count: 2
path: src/Faker/Provider/cs_CZ/Person.php

-
message: "#^Parameter \\#2 \\$callback of function preg_replace_callback expects callable\\(array\\<int\\|string, string\\>\\)\\: string, array\\{class\\-string\\<static\\(Faker\\\\Provider\\\\en_CA\\\\Address\\)\\>, 'randomDigit'\\} given\\.$#"
count: 1
path: src/Faker/Provider/en_CA/Address.php

-
message: "#^Unsafe call to private method Faker\\\\Provider\\\\en_GB\\\\Company\\:\\:generateBranchTraderVatNumber\\(\\) through static\\:\\:\\.$#"
count: 1
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener"/>
</listeners>
<php>
<env name="SYMFONY_DEPRECATIONS_HELPER" value="max[self]=404"/>
<env name="SYMFONY_DEPRECATIONS_HELPER" value="max[self]=0"/>
</php>
<testsuites>
<testsuite name="Faker Test Suite">
Expand Down
3 changes: 3 additions & 0 deletions psalm.baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@
</UndefinedDocblockClass>
</file>
<file src="src/Faker/Provider/Base.php">
<InvalidArgument occurrences="1">
<code>[static::class, 'randomDigit']</code>
</InvalidArgument>
<NoValue occurrences="1">
<code>$array</code>
</NoValue>
Expand Down
12 changes: 6 additions & 6 deletions src/Faker/Provider/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ public static function shuffleString($string = '', $encoding = 'UTF-8')
return implode('', static::shuffleArray($array));
}

private static function replaceWildcard($string, $wildcard = '#', $callback = 'static::randomDigit')
private static function replaceWildcard($string, $wildcard, $callback)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Method is private and always invoked with arguments for all parameters - no need for default values.

Also see #571.

{
if (($pos = strpos($string, $wildcard)) === false) {
return $string;
Expand Down Expand Up @@ -415,7 +415,7 @@ public static function numerify($string = '###')
$string[$toReplace[$i]] = $numbers[$i];
}
}
$string = self::replaceWildcard($string, '%', 'static::randomDigitNotNull');
$string = self::replaceWildcard($string, '%', [static::class, 'randomDigitNotNull']);

return $string;
}
Expand All @@ -429,7 +429,7 @@ public static function numerify($string = '###')
*/
public static function lexify($string = '????')
{
return self::replaceWildcard($string, '?', 'static::randomLetter');
return self::replaceWildcard($string, '?', [static::class, 'randomLetter']);
}

/**
Expand Down Expand Up @@ -460,7 +460,7 @@ public static function bothify($string = '## ??')
*/
public static function asciify($string = '****')
{
return preg_replace_callback('/\*/u', 'static::randomAscii', $string);
return preg_replace_callback('/\*/u', [static::class, 'randomAscii'], $string);
}

/**
Expand Down Expand Up @@ -532,8 +532,8 @@ public static function regexify($regex = '')
return str_replace('.', '\.', $randomElement);
}, $regex);
// replace \d with number and \w with letter and . with ascii
$regex = preg_replace_callback('/\\\w/', 'static::randomLetter', $regex);
$regex = preg_replace_callback('/\\\d/', 'static::randomDigit', $regex);
$regex = preg_replace_callback('/\\\w/', [static::class, 'randomLetter'], $regex);
$regex = preg_replace_callback('/\\\d/', [static::class, 'randomDigit'], $regex);
//replace . with ascii except backslash
$regex = preg_replace_callback('/(?<!\\\)\./', static function () {
$chr = static::asciify('*');
Expand Down
4 changes: 2 additions & 2 deletions src/Faker/Provider/en_CA/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ public static function postcode()
{
$string = static::randomElement(static::$postcode);

$string = preg_replace_callback('/\#/u', 'static::randomDigit', $string);
$string = preg_replace_callback('/\?/u', 'static::randomPostcodeLetter', $string);
$string = preg_replace_callback('/\#/u', [static::class, 'randomDigit'], $string);
$string = preg_replace_callback('/\?/u', [static::class, 'randomPostcodeLetter'], $string);

return static::toUpper($string);
}
Expand Down