Skip to content

Commit

Permalink
Fix PHP 8.2 warning: Use [static::class, '…'] instead of `'static::…
Browse files Browse the repository at this point in the history
……` for callbacks (#570)

* Fix: Use self::class . '::[…]' instead of 'static::[…]' as callbacks (PHP 8.2 warning)

* Fix: Use static

* Fix: Remove default value

* Fix: Adjust number of allowed deprecations

* Fix: Run 'make baseline'

* Fix: Avoid string concatenation

* Fix: Run 'make baseline'

Co-authored-by: Andreas Möller <[email protected]>
  • Loading branch information
MyIgel and localheinz authored Dec 13, 2022
1 parent e5e8400 commit e63e29e
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 9 deletions.
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)
{
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

0 comments on commit e63e29e

Please sign in to comment.