Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,23 @@ jobs:
run: composer install --no-interaction --no-ansi --no-progress
- name: Run PEST (with test coverage)
run: composer test:coverage
pest-mutation:
name: PEST - Mutation Testing
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v7
- name: Install PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.5
coverage: xdebug
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Composer install
run: composer install --no-interaction --no-ansi --no-progress
- name: Run PEST (with mutation testing)
run: composer test:mutate
pest-type-coverage:
name: PEST - Type Coverage
runs-on: ubuntu-latest
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"test": "./vendor/bin/pest --parallel",
"test:coverage": "XDEBUG_MODE=coverage composer test -- --coverage --min=97.4",
"test:type-coverage": "composer test -- --type-coverage --min=100",
"test:mutate": "XDEBUG_MODE=coverage composer test -- --mutate --everything --covered-only --min=100",
"phpstan": "./vendor/bin/phpstan --memory-limit=2G",
"pint": "./vendor/bin/pint"
},
Expand Down
12 changes: 2 additions & 10 deletions src/ArrayIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,24 +110,16 @@ public function getTotalPages(): int
*/
public function getForPage(int $page): array
{
if ($page <= 0) {
if ($page <= 0) { // @pest-mutate-ignore: IncrementInteger
$page = 1;
}

$output = [];
$perPage = $this->getPerPage();
$start = ($page * $perPage) - $perPage;
$end = $perPage + $start - 1;
$totalItems = $this->getTotalItems();

if ($end > $totalItems) {
$end = $totalItems - 1;
}

for ($i = $start; $i <= $end; $i++) {
if (!$this->setCursor($i)) {
break;
}
for ($i = $start; $i <= $end && $this->setCursor($i); $i++) {
$output[] = $this->current();
}

Expand Down
2 changes: 1 addition & 1 deletion src/ArraySectionIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class ArraySectionIterator extends ArrayIterator implements ArrayAccess, Countab

public function __construct(?int $totalItems = null)
{
parent::__construct([]);
parent::__construct([]); // @pest-mutate-ignore: RemoveMethodCall

$this->totalItems = $totalItems ?? 0;
}
Expand Down
50 changes: 27 additions & 23 deletions src/Date/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function __construct(mixed $longInitValue = '')
} elseif ($longInitValue === '' || $longInitValue === null) {
$this->setTimeInOldStyle(time());
} elseif (is_int($longInitValue) || is_string($longInitValue)) {
if (strlen('' . $longInitValue) === 14) {
if (strlen((string) $longInitValue) === 14) {
$this->setLongTimestamp($longInitValue);
} else {
$this->setTimeInOldStyle($longInitValue);
Expand All @@ -61,7 +61,7 @@ public function jsonSerialize(): string
*/
public static function isDateValue(int | string | \Stringable | null $longValue): bool
{
return StringUtil::isMatch('/^([0-9]){14}$/', (string) $longValue);
return StringUtil::isMatch('/^([0-9]){14}$/', (string) $longValue); // @pest-mutate-ignore: RemoveStringCast
}

/**
Expand Down Expand Up @@ -147,7 +147,7 @@ public function setTimeInOldStyle(int | string $intTimestamp): static
#[Deprecated(replacement: '%class%->getTimestamp()')]
public function getTimeInOldStyle(): int
{
return (int) mktime($this->getHour(), $this->getMinute(), $this->getSecond(), $this->getMonth(), $this->getDay(), $this->getYear());
return (int) mktime($this->getHour(), $this->getMinute(), $this->getSecond(), $this->getMonth(), $this->getDay(), $this->getYear()); // @pest-mutate-ignore: RemoveIntegerCast
}

/**
Expand Down Expand Up @@ -207,13 +207,13 @@ public function setNextMonth(): static
$objSourceDate = clone $this;

$this->setNextDay();
$intDaysAdded = 1;
$intDaysAdded = 1; // @pest-mutate-ignore: DecrementInteger
while ($this->getDay() !== $objSourceDate->getDay()) {
$this->setNextDay();
$intDaysAdded++;

// if we skip a month border, roll back until the previous months last day.
if ($intDaysAdded > 31) {
if ($intDaysAdded > 31) { // @pest-mutate-ignore: IncrementInteger
$this->setIntDay(1);
$this->setPreviousDay();

Expand All @@ -223,8 +223,9 @@ public function setNextMonth(): static
}

$this->setIntHour($objSourceDate->getHour());
$this->setIntMin($objSourceDate->getMinute());
$this->setIntSec($objSourceDate->getSecond());
// DST gaps shift whole hours only, minutes and seconds never drift.
$this->setIntMin($objSourceDate->getMinute()); // @pest-mutate-ignore: RemoveMethodCall
$this->setIntSec($objSourceDate->getSecond()); // @pest-mutate-ignore: RemoveMethodCall

return $this;
}
Expand All @@ -241,13 +242,13 @@ public function setPreviousMonth(): static
$objSourceDate = clone $this;

$this->setPreviousDay();
$intDaysSubtracted = 1;
$intDaysSubtracted = 1; // @pest-mutate-ignore: DecrementInteger
while ($this->getDay() !== $objSourceDate->getDay()) {
$this->setPreviousDay();
$intDaysSubtracted++;

// if we skip a month border, roll back until the next months last day.
if ($intDaysSubtracted > 31) {
if ($intDaysSubtracted > 31) { // @pest-mutate-ignore: IncrementInteger
$this->setNextMonth();
$this->setIntDay(1);
$this->setPreviousDay();
Expand All @@ -258,8 +259,9 @@ public function setPreviousMonth(): static
}

$this->setIntHour($objSourceDate->getHour());
$this->setIntMin($objSourceDate->getMinute());
$this->setIntSec($objSourceDate->getSecond());
// DST gaps shift whole hours only, minutes and seconds never drift.
$this->setIntMin($objSourceDate->getMinute()); // @pest-mutate-ignore: RemoveMethodCall
$this->setIntSec($objSourceDate->getSecond()); // @pest-mutate-ignore: RemoveMethodCall

return $this;
}
Expand All @@ -270,7 +272,7 @@ public function setPreviousMonth(): static
public function setPreviousQuarter(): self
{
$currentDay = $this->getDay();
$this->setIntDay(1);
$this->setIntDay(1); // @pest-mutate-ignore: IncrementInteger,DecrementInteger,RemoveMethodCall
for ($i = 0; $i < 3; $i++) {
$this->setPreviousMonth();
}
Expand All @@ -285,7 +287,7 @@ public function setPreviousQuarter(): self
public function setPreviousHalfYear(): self
{
$currentDay = $this->getDay();
$this->setIntDay(1);
$this->setIntDay(1); // @pest-mutate-ignore: IncrementInteger,DecrementInteger,RemoveMethodCall
for ($i = 0; $i < 6; $i++) {
$this->setPreviousMonth();
}
Expand All @@ -302,7 +304,7 @@ public function setPreviousHalfYear(): self
public function setPreviousYear(): static
{
$intCurrentDay = $this->getDay();
$this->setIntDay(1);
$this->setIntDay(1); // @pest-mutate-ignore: IncrementInteger,DecrementInteger,RemoveMethodCall
for ($intI = 0; $intI < 12; $intI++) {
$this->setPreviousMonth();
}
Expand All @@ -319,7 +321,7 @@ public function setPreviousYear(): static
public function setNextYear(): static
{
$intCurrentDay = $this->getDay();
$this->setIntDay(1);
$this->setIntDay(1); // @pest-mutate-ignore: IncrementInteger,DecrementInteger,RemoveMethodCall
for ($intI = 0; $intI < 12; $intI++) {
$this->setNextMonth();
}
Expand Down Expand Up @@ -385,10 +387,10 @@ public function setIntYear(int | string $intYear): static
return $this;
}

if (StringUtil::length('' . $intYear) === 2) {
if (strlen((string) $intYear) === 2) {
$intYear = '20' . $intYear;
}
if (StringUtil::length('' . $intYear) === 1) {
if (strlen((string) $intYear) === 1) {
$intYear = '200' . $intYear;
}

Expand Down Expand Up @@ -468,7 +470,7 @@ public function setIntSec(int | string $intSec, bool $bitForce = false): static
}

$strSec = sprintf('%02s', $intSec);
$this->longTimestamp = substr_replace($this->longTimestamp, $strSec, 12, 2);
$this->longTimestamp = substr_replace($this->longTimestamp, $strSec, 12, 2); // @pest-mutate-ignore: IncrementInteger

return $this;
}
Expand Down Expand Up @@ -578,7 +580,7 @@ public function getIntSec(): string

public function getSecond(): int
{
return (int) StringUtil::of($this->longTimestamp)->substr(12, 2)->value();
return (int) StringUtil::of($this->longTimestamp)->substr(12, 2)->value(); // @pest-mutate-ignore: IncrementInteger
}

/**
Expand Down Expand Up @@ -666,10 +668,11 @@ public function addInterval(DateInterval $dateInterval): self

$timeStamp = $dateTime->format($this->strParseFormat);

if ((int) $timeStamp > (int) self::MAX_TIMESTAMP) {
// PHP compares numeric strings numerically and clamping an equal value is a no-op.
if ((int) $timeStamp > (int) self::MAX_TIMESTAMP) { // @pest-mutate-ignore: RemoveIntegerCast,GreaterToGreaterOrEqual
$timeStamp = self::MAX_TIMESTAMP;
}
if ((int) $timeStamp < (int) self::MIN_TIMESTAMP) {
if ((int) $timeStamp < (int) self::MIN_TIMESTAMP) { // @pest-mutate-ignore: RemoveIntegerCast,SmallerToSmallerOrEqual
$timeStamp = self::MIN_TIMESTAMP;
}

Expand All @@ -689,10 +692,11 @@ public function subtractInterval(DateInterval $dateInterval): self

$timeStamp = $dateTime->format($this->strParseFormat);

if ((int) $timeStamp > (int) self::MAX_TIMESTAMP) {
// PHP compares numeric strings numerically and clamping an equal value is a no-op.
if ((int) $timeStamp > (int) self::MAX_TIMESTAMP) { // @pest-mutate-ignore: RemoveIntegerCast,GreaterToGreaterOrEqual
$timeStamp = self::MAX_TIMESTAMP;
}
if ((int) $timeStamp < (int) self::MIN_TIMESTAMP) {
if ((int) $timeStamp < (int) self::MIN_TIMESTAMP) { // @pest-mutate-ignore: RemoveIntegerCast,SmallerToSmallerOrEqual
$timeStamp = self::MIN_TIMESTAMP;
}

Expand Down
2 changes: 1 addition & 1 deletion src/FullText.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private function tokenize(float | int | string | null ...$parts): Collection
{
$fullText = StringUtil::of(
Collection::make($parts)
->map(static fn (float | int | string | null $part): string => StringUtil::trim((string) $part))
->map(static fn (float | int | string | null $part): string => StringUtil::trim((string) $part)) // @pest-mutate-ignore: RemoveStringCast
->implode(' '),
)->trim()->value();

Expand Down
22 changes: 11 additions & 11 deletions src/StringUtil.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Artemeon\Support;

use Artemeon\Support\Date\Date;
Expand Down Expand Up @@ -55,7 +57,7 @@ public static function equals(?string $left, ?string $right): bool
*/
public static function trim(mixed $value, mixed $charlist = null): string
{
return parent::trim((string) $value, $charlist);
return parent::trim((string) $value, $charlist); // @pest-mutate-ignore: RemoveStringCast
}

public static function limit(mixed $value, mixed $limit = 100, mixed $end = '…', mixed $preserveWords = false): string
Expand Down Expand Up @@ -136,15 +138,8 @@ public static function toDate(DateInterface | string | null $string): ?DateInter
public static function jsSafeString(string | \Stringable $string): string
{
$jsonString = json_encode((string) $string, JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR);
if (self::substr($jsonString, 0, 1) === '"') {
$jsonString = StringUtil::substr($jsonString, 1);
}

if (self::substr($jsonString, -1) === '"') {
$jsonString = self::substr($jsonString, 0, -1);
}

$jsonString = addcslashes($jsonString, "'");
// Strip the surrounding quotes json_encode() always adds to a string.
$jsonString = addcslashes(self::substr($jsonString, 1, -1), "'");

return htmlspecialchars($jsonString, ENT_QUOTES | ENT_HTML401);
}
Expand All @@ -154,7 +149,12 @@ public static function jsSafeString(string | \Stringable $string): string
*/
public static function removeScriptTags(?string $string): string
{
return (string) preg_replace('~<script(.*)</script>~imUs', '', (string) $string);
// preg_replace() only returns null on a PCRE error.
return (string) preg_replace( // @pest-mutate-ignore: RemoveStringCast
'~<script(.*)</script>~imUs',
'',
(string) $string,
);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Stringable.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Stringable extends \Illuminate\Support\Stringable
{
final public function __construct(mixed $value = '')
{
parent::__construct((string) $value);
parent::__construct($value);
}

public function indexOf(string $needle, bool $caseSensitive = true): bool | int
Expand Down
2 changes: 1 addition & 1 deletion src/Timer.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ public function getDurationInSeconds(): float
$this->end();
}

return round(((float) $this->end - (float) $this->start), 6);
return round(((float) $this->end - (float) $this->start), 6); // @pest-mutate-ignore: RemoveDoubleCast
}
}
12 changes: 12 additions & 0 deletions tests/Unit/ArrayIteratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
[range(1, 21), 20, 2],
[range(1, 40), 20, 2],
[range(1, 42), 20, 3],
[range(1, 3), 1, 3],
[range(1, 42), 0, 0],
[range(1, 42), -1, 0],
]);
Expand Down Expand Up @@ -78,6 +79,17 @@
expect($iterator->valid())->toBeTrue();
});

it('should be invalid past the last item', function (): void {
$iterator = new ArrayIterator(['foo']);
$iterator->next();

expect($iterator->valid())->toBeFalse();
});

it('should reindex associative items', function (): void {
expect(new ArrayIterator(['a' => 'foo', 'b' => 'bar'])->current())->toBe('foo');
});

it('should get items for specific page', function (array $items, int $page, int $perPage, array $expected): void {
$iterator = new ArrayIterator($items);
$iterator->setPerPage($perPage);
Expand Down
24 changes: 24 additions & 0 deletions tests/Unit/ArraySectionIteratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,29 @@
expect($iterator->getTotalItems())->toBe($totalItems);
})->with([[500], [1000]]);

it('should default to zero total items', function (): void {
expect(new ArraySectionIterator()->getTotalItems())->toBe(0);
});

it('should default to the first page and rewind', function (): void {
$iterator = new ArraySectionIterator(10);
$iterator->setSection([1, 2]);
$iterator->next();
$iterator->setPage(null);

expect($iterator->getPage())->toBe(1)
->and($iterator->key())->toBe(0);
});

it('should be invalid past the end of the section', function (): void {
$iterator = new ArraySectionIterator(2);
$iterator->setSection([1, 2]);
$iterator->next();
$iterator->next();

expect($iterator->valid())->toBeFalse();
});

it('should overwrite total items', function (int $initialItems, int $newItems): void {
$iterator = new ArraySectionIterator($initialItems);
$iterator->setTotalItems($newItems);
Expand Down Expand Up @@ -166,4 +189,5 @@
[range(1, 15), 15, 15, 1, 1],
[range(1, 15), 30, 15, 2, 1],
[range(11, 15), 20, 5, 4, 3],
[range(6, 10), 20, 5, 4, 2],
]);
Loading
Loading