Skip to content

Commit

Permalink
Update string length and license check functions, and update dependen…
Browse files Browse the repository at this point in the history
…cies

The update includes switching from `mb_strlen` to `strlen` function for getting string length in several classes. This also includes refactoring the license check function and removing the Coveralls section from the GitHub action workflow. Lastly, all outdated dependencies have been updated.
  • Loading branch information
Spomky committed Jul 18, 2024
1 parent bb73f49 commit df91c86
Show file tree
Hide file tree
Showing 15 changed files with 115 additions and 420 deletions.
8 changes: 0 additions & 8 deletions .github/workflows/integrate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,6 @@ jobs:
- name: "Execute tests (PHP)"
run: "castor test --coverage-text"

# - name: Send coverage to Coveralls
# if: "matrix.php-version == '8.1' && matrix.dependencies == 'highest'"
# env:
# COVERALLS_REPO_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
# run: |
# wget "https://github.com/php-coveralls/php-coveralls/releases/download/v2.5.2/php-coveralls.phar"
# php ./php-coveralls.phar -v

static_analysis:
name: "3️⃣ Static Analysis"
needs:
Expand Down
9 changes: 4 additions & 5 deletions castor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

declare(strict_types=1);

use Castor\Attribute\AsOption;
use Castor\Attribute\AsTask;
use function Castor\io;
use function Castor\run;
Expand Down Expand Up @@ -99,10 +100,10 @@ function validate(): void
*/
#[AsTask(description: 'Check licenses')]
function checkLicenses(
#[AsOption(description: 'Allowed licenses.')]
array $allowedLicenses = ['Apache-2.0', 'BSD-2-Clause', 'BSD-3-Clause', 'ISC', 'MIT', 'MPL-2.0', 'OSL-3.0']
): void {
io()->title('Checking licenses');
$allowedExceptions = [];
$command = ['composer', 'licenses', '-f', 'json'];
$environment = [
'XDEBUG_MODE' => 'off',
Expand All @@ -115,14 +116,12 @@ function checkLicenses(
$licenses = json_decode($result->getOutput(), true);
$disallowed = array_filter(
$licenses['dependencies'],
static fn (array $info, $name) => ! in_array($name, $allowedExceptions, true)
&& count(array_diff($info['license'], $allowedLicenses)) === 1,
static fn (array $info, $name) => count(array_diff($info['license'], $allowedLicenses)) === 1,
\ARRAY_FILTER_USE_BOTH
);
$allowed = array_filter(
$licenses['dependencies'],
static fn (array $info, $name) => in_array($name, $allowedExceptions, true)
|| count(array_diff($info['license'], $allowedLicenses)) === 0,
static fn (array $info, $name) => count(array_diff($info['license'], $allowedLicenses)) === 0,
\ARRAY_FILTER_USE_BOTH
);
if (count($disallowed) > 0) {
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,20 @@
"require-dev": {
"ext-json": "*",
"ekino/phpstan-banned-code": "^1.0",
"infection/infection": "^0.28",
"infection/infection": "^0.29",
"phpstan/extension-installer": "^1.1",
"phpstan/phpstan": "^1.0",
"phpstan/phpstan-beberlei-assert": "^1.0",
"phpstan/phpstan-deprecation-rules": "^1.0",
"phpstan/phpstan-phpunit": "^1.0",
"phpstan/phpstan-strict-rules": "^1.0",
"phpunit/phpunit": "^10.1",
"phpunit/phpunit": "^10.1|^11.0",
"rector/rector": "^1.0",
"roave/security-advisories": "dev-latest",
"symfony/var-dumper": "^6.0|^7.0",
"symplify/easy-coding-standard": "^12.0",
"php-parallel-lint/php-parallel-lint": "^1.3",
"qossmic/deptrac-shim": "^1.0"
"qossmic/deptrac": "^2.0"
},
"config": {
"sort-packages": true,
Expand Down
6 changes: 3 additions & 3 deletions deptrac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ parameters:
layers:
- name: 'CBOR'
collectors:
- type: 'className'
regex: '^CBO\\'
- type: 'classLike'
value: '^CBO\\'
- name: 'Vendors'
collectors:
- { type: className, regex: '^Brick\\' }
- { type: 'classLike', value: '^Brick\\' }
ruleset:
CBOR:
- Vendors
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
</php>
<source>
<include>
<directory suffix=".php">src</directory>
<directory>src</directory>
</include>
</source>
</phpunit>
3 changes: 1 addition & 2 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Rector\Config\RectorConfig;
use Rector\PHPUnit\CodeQuality\Rector\Class_\PreferPHPUnitThisCallRector;
use Rector\PHPUnit\Set\PHPUnitLevelSetList;
use Rector\PHPUnit\Set\PHPUnitSetList;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Set\ValueObject\SetList;
Expand All @@ -15,7 +14,7 @@
$config->import(SetList::DEAD_CODE);
$config->import(LevelSetList::UP_TO_PHP_80);
$config->import(SymfonySetList::SYMFONY_CODE_QUALITY);
$config->import(PHPUnitLevelSetList::UP_TO_PHPUNIT_100);
$config->import(PHPUnitSetList::PHPUNIT_100);
$config->import(PHPUnitSetList::PHPUNIT_CODE_QUALITY);
$config->parallel();
$config->paths([__DIR__ . '/src', __DIR__ . '/tests']);
Expand Down
4 changes: 3 additions & 1 deletion src/ByteStringObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace CBOR;

use function strlen;

/**
* @see \CBOR\Test\ByteStringObjectTest
*/
Expand Down Expand Up @@ -46,7 +48,7 @@ public function getValue(): string

public function getLength(): int
{
return mb_strlen($this->value, '8bit');
return strlen($this->value);
}

public function normalize(): string
Expand Down
5 changes: 3 additions & 2 deletions src/LengthCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use InvalidArgumentException;
use function chr;
use function count;
use function strlen;
use const STR_PAD_LEFT;

final class LengthCalculator
Expand All @@ -17,7 +18,7 @@ final class LengthCalculator
*/
public static function getLengthOfString(string $data): array
{
$length = mb_strlen($data, '8bit');
$length = strlen($data);

return self::computeLength($length);
}
Expand Down Expand Up @@ -54,7 +55,7 @@ private static function computeLength(int $length): array

private static function hex2bin(string $data): string
{
$data = str_pad($data, (int) (2 ** ceil(log(mb_strlen($data, '8bit'), 2))), '0', STR_PAD_LEFT);
$data = str_pad($data, (int) (2 ** ceil(log(strlen($data), 2))), '0', STR_PAD_LEFT);
$result = hex2bin($data);
if ($result === false) {
throw new InvalidArgumentException('Unable to convert the data');
Expand Down
3 changes: 2 additions & 1 deletion src/OtherObject/DoublePrecisionFloatObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use CBOR\OtherObject as Base;
use CBOR\Utils;
use InvalidArgumentException;
use function strlen;
use const INF;
use const NAN;

Expand All @@ -26,7 +27,7 @@ public static function createFromLoadedData(int $additionalInformation, ?string

public static function create(string $value): self
{
if (mb_strlen($value, '8bit') !== 8) {
if (strlen($value) !== 8) {
throw new InvalidArgumentException('The value is not a valid double precision floating point');
}

Expand Down
3 changes: 2 additions & 1 deletion src/OtherObject/HalfPrecisionFloatObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use CBOR\OtherObject as Base;
use CBOR\Utils;
use InvalidArgumentException;
use function strlen;
use const INF;
use const NAN;

Expand All @@ -26,7 +27,7 @@ public static function createFromLoadedData(int $additionalInformation, ?string

public static function create(string $value): self
{
if (mb_strlen($value, '8bit') !== 2) {
if (strlen($value) !== 2) {
throw new InvalidArgumentException('The value is not a valid half precision floating point');
}

Expand Down
3 changes: 2 additions & 1 deletion src/OtherObject/SimpleObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use InvalidArgumentException;
use function chr;
use function ord;
use function strlen;

final class SimpleObject extends Base implements Normalizable
{
Expand Down Expand Up @@ -46,7 +47,7 @@ public static function createFromLoadedData(int $additionalInformation, ?string
if ($data === null) {
throw new InvalidArgumentException('Invalid simple value. Content data is missing.');
}
if (mb_strlen($data, '8bit') !== 1) {
if (strlen($data) !== 1) {
throw new InvalidArgumentException('Invalid simple value. Content data is too long.');
}
if (ord($data) < 32) {
Expand Down
3 changes: 2 additions & 1 deletion src/OtherObject/SinglePrecisionFloatObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use CBOR\OtherObject as Base;
use CBOR\Utils;
use InvalidArgumentException;
use function strlen;
use const INF;
use const NAN;

Expand All @@ -25,7 +26,7 @@ public static function createFromLoadedData(int $additionalInformation, ?string

public static function create(string $value): self
{
if (mb_strlen($value, '8bit') !== 4) {
if (strlen($value) !== 4) {
throw new InvalidArgumentException('The value is not a valid single precision floating point');
}

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

use InvalidArgumentException;
use RuntimeException;
use function strlen;

final class StringStream implements Stream
{
Expand Down Expand Up @@ -53,21 +54,21 @@ public function read(int $length): string
if ($newData === false) {
throw new RuntimeException('Unable to read the memory');
}
if (mb_strlen($newData, '8bit') < $sizeToRead) {
if (strlen($newData) < $sizeToRead) {
throw new InvalidArgumentException(sprintf(
'Out of range. Expected: %d, read: %d.',
$length,
mb_strlen($data, '8bit')
strlen($data)
));
}
$data .= $newData;
}

if (mb_strlen($data, '8bit') !== $length) {
if (strlen($data) !== $length) {
throw new InvalidArgumentException(sprintf(
'Out of range. Expected: %d, read: %d.',
$length,
mb_strlen($data, '8bit')
strlen($data)
));
}

Expand Down
5 changes: 3 additions & 2 deletions src/Tag/TimestampTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use DateTimeImmutable;
use DateTimeInterface;
use InvalidArgumentException;
use function strlen;
use const STR_PAD_RIGHT;

final class TimestampTag extends Tag implements Normalizable
Expand Down Expand Up @@ -60,8 +61,8 @@ public function normalize(): DateTimeInterface
$value = (string) $object->normalize();
$parts = explode('.', $value);
if (isset($parts[1])) {
if (mb_strlen($parts[1], '8bit') > 6) {
$parts[1] = mb_substr($parts[1], 0, 6, '8bit');
if (strlen($parts[1]) > 6) {
$parts[1] = substr($parts[1], 0, 6);
} else {
$parts[1] = str_pad($parts[1], 6, '0', STR_PAD_RIGHT);
}
Expand Down
Loading

0 comments on commit df91c86

Please sign in to comment.