Skip to content

Commit

Permalink
pref: use function array_is_list
Browse files Browse the repository at this point in the history
  • Loading branch information
Rhilip committed Feb 2, 2022
1 parent c32d54c commit d12af71
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php-versions: [ 5.6, 7.1, 7.2, 7.3, 7.4, 8.0 ]
php-versions: [ 5.6, 7.1, 7.2, 7.3, 7.4, 8.0, 8.1 ]
steps:
- uses: actions/checkout@master
- name: Get Composer Cache Directory
Expand Down
15 changes: 8 additions & 7 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<phpunit bootstrap="./vendor/autoload.php">
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
</phpunit>
<?xml version="1.0"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="./vendor/autoload.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./src</directory>
</include>
</coverage>
</phpunit>
29 changes: 20 additions & 9 deletions src/Bencode.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,25 @@

namespace Rhilip\Bencode;

if (!function_exists('array_is_list')) {
function array_is_list(array $array)
{
if ([] === $array || $array === array_values($array)) {
return true;
}

$nextKey = -1;

foreach ($array as $k => $v) {
if ($k !== ++$nextKey) {
return false;
}
}

return true;
}
}

/**
* Class Bencode
*
Expand Down Expand Up @@ -123,15 +142,7 @@ public static function encode($data)
{
if (is_array($data)) {
$return = '';
$check = -1;
$list = true;
foreach ($data as $key => $value) {
if ($key !== ++$check) {
$list = false;
break;
}
}
if ($list) {
if (array_is_list($data)) {
$return .= 'l';
foreach ($data as $value) {
$return .= self::encode($value);
Expand Down

0 comments on commit d12af71

Please sign in to comment.