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

Support for PHP 8.4 #6

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,18 @@
<directory name="vendor" />
</ignoreFiles>
</projectFiles>

<issueHandlers>
<!-- Don't warn about methods only available in later PHP versions -->
<UndefinedMethod>
<errorLevel type="suppress">
<referencedMethod name="Random\Randomizer::getFloat" />
</errorLevel>
</UndefinedMethod>
<UndefinedClass>
<errorLevel type="suppress">
<referencedClass name="Random\Randomizer" />
</errorLevel>
</UndefinedClass>
</issueHandlers>
</psalm>
10 changes: 8 additions & 2 deletions src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1072,7 +1072,13 @@ private function round(float $number): int
*/
private function random_float(float|int $min, float|int $max): float
{
# See https://www.php.net/manual/en/function.mt-rand.php#75793
return ($min + lcg_value() * (abs($max - $min)));
if (class_exists(\Random\Randomizer::class) && method_exists(\Random\Randomizer::class, "getFloat")) {
# getFloat() was introduced in PHP 8.3
$randomizer = new \Random\Randomizer();
return $randomizer->getFloat($min, $max);
} else {
# See https://www.php.net/manual/en/function.mt-rand.php#75793
return ($min + lcg_value() * (abs($max - $min)));
}
}
}
8 changes: 4 additions & 4 deletions src/Helpers/Dir.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ class Dir
* Get all files
*
* @param string $dir
* @param array $ignore
* @param array|null $ignore
* @param bool $absolute
* @return array
*/
public static function files(string $dir, array $ignore = null, bool $absolute = false): array
public static function files(string $dir, ?array $ignore = null, bool $absolute = false): array
{
$result = array_values(array_filter(static::read($dir, $ignore, true), 'is_file'));

Expand Down Expand Up @@ -108,11 +108,11 @@ public static function make(string $dir, bool $recursive = true): bool
* It skips unwanted invisible stuff.
*
* @param string $dir The path of directory
* @param array $ignore Optional array with filenames, which should be ignored
* @param array|null $ignore Optional array with filenames, which should be ignored
* @param bool $absolute If true, the full path for each item will be returned
* @return array An array of filenames
*/
public static function read(string $dir, array $ignore = null, bool $absolute = false): array
public static function read(string $dir, ?array $ignore = null, bool $absolute = false): array
{
if (is_dir($dir) === false) {
return [];
Expand Down
14 changes: 7 additions & 7 deletions src/Helpers/F.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ public static function dirname(string $file): string
* Checks if the file exists on disk
*
* @param string $file
* @param string $in
* @param string|null $in
* @return bool
*/
public static function exists(string $file, string $in = null): bool
public static function exists(string $file, ?string $in = null): bool
{
try {
static::realpath($file, $in);
Expand All @@ -62,11 +62,11 @@ public static function exists(string $file, string $in = null): bool
/**
* Gets the extension of a file
*
* @param string $file The filename or path
* @param string $extension Set an optional extension to overwrite the current one
* @param string|null $file The filename or path
* @param string|null $extension Set an optional extension to overwrite the current one
* @return string
*/
public static function extension(string $file = null, string $extension = null): string
public static function extension(?string $file = null, ?string $extension = null): string
{
// overwrite the current extension
if ($extension !== null) {
Expand Down Expand Up @@ -130,10 +130,10 @@ public static function name(string $name): string
* Returns the absolute path to the file if the file can be found.
*
* @param string $file
* @param string $in
* @param string|null $in
* @return string|null
*/
public static function realpath(string $file, string $in = null)
public static function realpath(string $file, ?string $in = null)
{
$realpath = realpath($file);

Expand Down
7 changes: 2 additions & 5 deletions src/Helpers/Mime.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

namespace SimpleCaptcha\Helpers;

use SimpleCaptcha\Helpers\F;


/**
* The `Mime` class provides method
* for MIME type detection or guessing
Expand Down Expand Up @@ -168,10 +165,10 @@ public static function fromMimeContentType(string $file)
* Returns the MIME type of a file
*
* @param string $file
* @param string $extension
* @param string|null $extension
* @return string|false
*/
public static function type(string $file, string $extension = null)
public static function type(string $file, ?string $extension = null)
{
// use the standard finfo extension
$mime = static::fromFileInfo($file);
Expand Down
18 changes: 9 additions & 9 deletions src/Helpers/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Str
* @param bool $caseInsensitive
* @return bool
*/
public static function contains(string $string = null, string $needle, bool $caseInsensitive = false): bool
public static function contains(string $string, string $needle, bool $caseInsensitive = false): bool
{
if ($needle === '') {
return true;
Expand All @@ -38,10 +38,10 @@ public static function contains(string $string = null, string $needle, bool $cas
/**
* A UTF-8 safe version of strlen()
*
* @param string $string
* @param string|null $string
* @return int
*/
public static function length(string $string = null): int
public static function length(?string $string = null): int
{
return mb_strlen($string ?? '', 'UTF-8');
}
Expand All @@ -50,10 +50,10 @@ public static function length(string $string = null): int
/**
* A UTF-8 safe version of strtolower()
*
* @param string $string
* @param string|null $string
* @return string
*/
public static function lower(string $string = null): string
public static function lower(?string $string = null): string
{
return mb_strtolower($string ?? '', 'UTF-8');
}
Expand Down Expand Up @@ -81,7 +81,7 @@ public static function ltrim(string $string, string $trim = ' '): string
* @param bool $caseInsensitive
* @return int|bool
*/
public static function position(string $string = null, string $needle, bool $caseInsensitive = false)
public static function position(string $string, string $needle, bool $caseInsensitive = false)
{
if ($caseInsensitive === true) {
$string = static::lower($string);
Expand Down Expand Up @@ -113,7 +113,7 @@ public static function rtrim(string $string, string $trim = ' '): string
* @param bool $caseInsensitive
* @return bool
*/
public static function startsWith(string $string = null, string $needle, bool $caseInsensitive = false): bool
public static function startsWith(string $string, string $needle, bool $caseInsensitive = false): bool
{
if ($needle === '') {
return true;
Expand All @@ -131,9 +131,9 @@ public static function startsWith(string $string = null, string $needle, bool $c
* @param int $length
* @return string
*/
public static function substr(string $string = null, int $start = 0, int $length = null): string
public static function substr(string $string, int $start, int $length): string
{
return mb_substr($string ?? '', $start, $length, 'UTF-8');
return mb_substr($string, $start, $length, 'UTF-8');
}


Expand Down