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
6 changes: 3 additions & 3 deletions packages/scalars/src/ArrayObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,13 +318,13 @@ public function sum(): float|int
*/
public function avg(): float|int|null
{
$sum = $this->sum();
$count = $this->count();

if ($sum === 0 || $sum === 0.0) {
if ($count === 0) {
return null;
}

return $sum / $this->count();
return $this->sum() / $count;
}

/**
Expand Down
5 changes: 4 additions & 1 deletion packages/scalars/src/StringObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ public static function wrap(mixed $string = '', ?string $encoding = self::ENCODI
*/
public function __call(string $name, array $args): mixed
{
if ($name === 'split') {
return $this->explode(...$args);
}

$class = Str::class;

if (is_callable([$class, $name])) {
Expand All @@ -158,7 +162,6 @@ public function __call(string $name, array $args): mixed
$maps = [
'toUpperCase' => [Utf8String::class, 'strtoupper'],
'toLowerCase' => [Utf8String::class, 'strtolower'],
'split' => [$this, 'explode'],
];

if ($maps[$name] ?? null) {
Expand Down
9 changes: 8 additions & 1 deletion packages/scalars/test/ArrayObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -691,11 +691,18 @@ public function searchWithClosureReturnsFalseWhenNothingMatches(): void
}

#[Test]
public function avgReturnsNullWhenSumIsZero(): void
public function avgReturnsNullWhenArrayIsEmpty(): void
{
self::assertNull(arr([])->avg());
}

#[Test]
public function avgReturnsZeroWhenSumIsZero(): void
{
self::assertSame(0, arr([1, -1])->avg());
self::assertSame(0, arr([0, 0])->avg());
}

#[Test]
public function wrapReturnsSameInstanceWhenAlreadyWrapped(): void
{
Expand Down
9 changes: 9 additions & 0 deletions packages/scalars/test/StringObjectTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php

Check warning on line 1 in packages/scalars/test/StringObjectTest.php

View workflow job for this annotation

GitHub Actions / php-cs-fixer

Found violation(s) of type: blank_line_between_import_groups

Check warning on line 1 in packages/scalars/test/StringObjectTest.php

View workflow job for this annotation

GitHub Actions / php-cs-fixer

Found violation(s) of type: no_extra_blank_lines

Check warning on line 1 in packages/scalars/test/StringObjectTest.php

View workflow job for this annotation

GitHub Actions / php-cs-fixer

Found violation(s) of type: new_expression_parentheses

declare(strict_types=1);

Expand Down Expand Up @@ -525,6 +525,15 @@
];
}

#[Test]
public function splitSplitsStringByDelimiter(): void
{
self::assertEquals(
['Foo', 'Bar', 'Yoo'],
(new StringObject('Foo/Bar/Yoo'))->split('/')->dump()
);
}

#[Test]
public function applyReturnsNewInstanceFromCallbackResult()
{
Expand Down
Loading