Skip to content

Commit

Permalink
Fix default order.
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed May 16, 2023
1 parent c4140be commit bee920f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
23 changes: 22 additions & 1 deletion src/Sort.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public function getColumnOrders(bool $value = false): array
}
}
} else {
$this->columnOrders = $this->defaultColumnOrder;
$this->setDefaultColumnOrders();
}

return $this->columnOrders;

Check failure on line 211 in src/Sort.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

NullableReturnStatement

src/Sort.php:211:16: NullableReturnStatement: The declared return type 'array<array-key, mixed>' for Yii\DataProvider\Sort::getColumnOrders is not nullable, but the function returns 'array<array-key, 3|4|mixed>|null' (see https://psalm.dev/139)

Check failure on line 211 in src/Sort.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.2-ubuntu-latest

NullableReturnStatement

src/Sort.php:211:16: NullableReturnStatement: The declared return type 'array<array-key, mixed>' for Yii\DataProvider\Sort::getColumnOrders is not nullable, but the function returns 'array<array-key, 3|4|mixed>|null' (see https://psalm.dev/139)

Check failure on line 211 in src/Sort.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.2-ubuntu-latest

NullableReturnStatement

src/Sort.php:211:16: NullableReturnStatement: The declared return type 'array<array-key, mixed>' for Yii\DataProvider\Sort::getColumnOrders is not nullable, but the function returns 'array<array-key, 3|4|mixed>|null' (see https://psalm.dev/139)
Expand Down Expand Up @@ -404,4 +404,25 @@ private function parseSortParam(string $param): array
{
return explode($this->separator, $param);
}

private function setDefaultColumnOrders(): void
{
if ($this->defaultColumnOrder !== []) {
$this->columnOrders = $this->defaultColumnOrder;
return;
}

$defaultColumnOrder = [];

foreach ($this->columns as $name => $definition) {

Check failure on line 417 in src/Sort.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

MixedAssignment

src/Sort.php:417:45: MixedAssignment: Unable to determine the type that $definition is being assigned to (see https://psalm.dev/032)

Check failure on line 417 in src/Sort.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.2-ubuntu-latest

MixedAssignment

src/Sort.php:417:45: MixedAssignment: Unable to determine the type that $definition is being assigned to (see https://psalm.dev/032)

Check failure on line 417 in src/Sort.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.2-ubuntu-latest

MixedAssignment

src/Sort.php:417:45: MixedAssignment: Unable to determine the type that $definition is being assigned to (see https://psalm.dev/032)
$defaultDirection = $definition['default'] ?? SORT_ASC;

Check failure on line 418 in src/Sort.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

MixedAssignment

src/Sort.php:418:13: MixedAssignment: Unable to determine the type that $defaultDirection is being assigned to (see https://psalm.dev/032)

Check failure on line 418 in src/Sort.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

MixedArrayAccess

src/Sort.php:418:33: MixedArrayAccess: Cannot access array value on mixed variable $definition (see https://psalm.dev/051)

Check failure on line 418 in src/Sort.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.2-ubuntu-latest

MixedAssignment

src/Sort.php:418:13: MixedAssignment: Unable to determine the type that $defaultDirection is being assigned to (see https://psalm.dev/032)

Check failure on line 418 in src/Sort.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.2-ubuntu-latest

MixedArrayAccess

src/Sort.php:418:33: MixedArrayAccess: Cannot access array value on mixed variable $definition (see https://psalm.dev/051)

Check failure on line 418 in src/Sort.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.2-ubuntu-latest

MixedAssignment

src/Sort.php:418:13: MixedAssignment: Unable to determine the type that $defaultDirection is being assigned to (see https://psalm.dev/032)

Check failure on line 418 in src/Sort.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.2-ubuntu-latest

MixedArrayAccess

src/Sort.php:418:33: MixedArrayAccess: Cannot access array value on mixed variable $definition (see https://psalm.dev/051)
$defaultColumnOrder[$name] = $defaultDirection;

Check failure on line 419 in src/Sort.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

MixedAssignment

src/Sort.php:419:13: MixedAssignment: Unable to determine the type of this assignment (see https://psalm.dev/032)

Check failure on line 419 in src/Sort.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.2-ubuntu-latest

MixedAssignment

src/Sort.php:419:13: MixedAssignment: Unable to determine the type of this assignment (see https://psalm.dev/032)

Check failure on line 419 in src/Sort.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.2-ubuntu-latest

MixedAssignment

src/Sort.php:419:13: MixedAssignment: Unable to determine the type of this assignment (see https://psalm.dev/032)

if ($this->multiSort === false) {
break;
}
}

$this->columnOrders = $defaultColumnOrder;
}
}
19 changes: 18 additions & 1 deletion tests/DataProvider/SortTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Yii\DataProvider\Tests\DataProvider;

use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use Yii\DataProvider\Sort;

Expand Down Expand Up @@ -104,6 +105,22 @@ public function testGetColumnOrders()
$this->assertSame(SORT_ASC, $orders['age']);
}

public function testGetColumnOrdersWithEmpty()
{
$this->sort->columns(['age', 'name'])->multiSort();

$orders = $this->sort->getColumnOrders();
$this->assertCount(2, $orders);
$this->assertSame(SORT_ASC, $orders['age']);
$this->assertSame(SORT_ASC, $orders['name']);

$this->sort->multiSort(false);

$orders = $this->sort->getColumnOrders(true);
$this->assertCount(1, $orders);
$this->assertSame(SORT_ASC, $orders['age']);
}

/**
* @see https://github.com/yiisoft/yii2/pull/13260
*/
Expand Down Expand Up @@ -197,7 +214,7 @@ public function testGetSortParam(): void

public function testGetSortParamWithException(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Unknown attribute: unexistingAttribute');

$this->sort->columns(
Expand Down

0 comments on commit bee920f

Please sign in to comment.