Skip to content

Commit

Permalink
Added find first to array trait. Removed find first from collection t…
Browse files Browse the repository at this point in the history
…rait.
  • Loading branch information
laurentmuller committed May 27, 2024
1 parent e6fc790 commit 6962d27
Show file tree
Hide file tree
Showing 30 changed files with 291 additions and 275 deletions.
22 changes: 11 additions & 11 deletions src/Entity/Calculation.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ public function contains(CalculationGroup $group): bool
* Finds or create a calculation category for the given category.
*
* @param Category $category the category to find
*
* @psalm-suppress MixedArgumentTypeCoercion
*/
public function findCategory(Category $category): CalculationCategory
{
Expand All @@ -198,8 +200,8 @@ public function findCategory(Category $category): CalculationCategory

// find category
$code = $category->getCode();
$first = $this->findFirst(
$group->getCategories(),
/** @psalm-var CalculationCategory|null $first */
$first = $group->getCategories()->findFirst(
fn (int $key, CalculationCategory $category): bool => $code === $category->getCode()
);
if ($first instanceof CalculationCategory) {
Expand All @@ -217,13 +219,15 @@ public function findCategory(Category $category): CalculationCategory
* Finds or create a calculation group for the given group.
*
* @param Group $group the group to find
*
* @psalm-suppress MixedArgumentTypeCoercion
*/
public function findGroup(Group $group): CalculationGroup
{
// find the group
$code = $group->getCode();
$first = $this->findFirst(
$this->groups,
/** @psalm-var CalculationGroup|null $first */
$first = $this->groups->findFirst(
fn (int $key, CalculationGroup $group): bool => $code === $group->getCode()
);
if ($first instanceof CalculationGroup) {
Expand Down Expand Up @@ -671,13 +675,9 @@ public function isSortable(): bool
return false;
}

foreach ($this->groups as $group) {
if ($group->isSortable()) {
return true;
}
}

return false;
return $this->groups->exists(
static fn (int $key, CalculationGroup $group): bool => $group->isSortable()
);
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/Entity/CalculationGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,13 @@ public function isEmpty(): bool
*/
public function isSortable(): bool
{
foreach ($this->categories as $category) {
if ($category->isSortable()) {
return true;
}
if ($this->isEmpty()) {
return false;
}

return false;
return $this->categories->exists(
static fn (int $key, CalculationCategory $category): bool => $category->isSortable()
);
}

/**
Expand Down
33 changes: 16 additions & 17 deletions src/Entity/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use App\Interfaces\SortModeInterface;
use App\Interfaces\TimestampableInterface;
use App\Repository\GroupRepository;
use App\Traits\CollectionTrait;
use App\Traits\TimestampableTrait;
use App\Traits\ValidateMarginsTrait;
use App\Utils\StringUtils;
Expand All @@ -36,6 +37,7 @@
#[UniqueEntity(fields: 'code', message: 'group.unique_code')]
class Group extends AbstractEntity implements ComparableInterface, TimestampableInterface
{
use CollectionTrait;
use TimestampableTrait;
use ValidateMarginsTrait;

Expand Down Expand Up @@ -199,16 +201,19 @@ public function countTasks(): int
* @param float $amount the amount to get group margin for
*
* @return GroupMargin|null the group margin, if found; null otherwise
*
* @psalm-suppress MixedArgumentTypeCoercion
*/
public function findMargin(float $amount): ?GroupMargin
{
foreach ($this->margins as $margin) {
if ($margin->contains($amount)) {
return $margin;
}
if ($this->margins->isEmpty()) {
return null;
}

return null;
/** @psalm-var GroupMargin|null */
return $this->margins->findFirst(
fn (int $key, GroupMargin $margin): bool => $margin->contains($amount)
);
}

/**
Expand Down Expand Up @@ -294,13 +299,10 @@ public function hasProducts(): bool
if (!$this->hasCategories()) {
return false;
}
foreach ($this->categories as $category) {
if ($category->hasProducts()) {
return true;
}
}

return false;
return $this->categories->exists(
static fn (int $key, Category $category): bool => $category->hasProducts()
);
}

/**
Expand All @@ -313,13 +315,10 @@ public function hasTasks(): bool
if (!$this->hasCategories()) {
return false;
}
foreach ($this->categories as $category) {
if ($category->hasTasks()) {
return true;
}
}

return false;
return $this->categories->exists(
static fn (int $key, Category $category): bool => $category->hasTasks()
);
}

/**
Expand Down
13 changes: 6 additions & 7 deletions src/Entity/TaskItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,15 @@ public function count(): int

/**
* Gets the margin for the given quantity.
*
* @psalm-suppress MixedArgumentTypeCoercion
*/
public function findMargin(float $quantity): ?TaskItemMargin
{
foreach ($this->margins as $margin) {
if ($margin->contains($quantity)) {
return $margin;
}
}

return null;
/** @psalm-var TaskItemMargin|null */
return $this->margins->findFirst(
fn (int $key, TaskItemMargin $margin): bool => $margin->contains($quantity)
);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Interfaces/PropertyServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ interface PropertyServiceInterface
'security_special_char',
'security_case_diff',
'security_email',
'security_pwned',
'security_compromised',
];

/**
Expand Down
16 changes: 8 additions & 8 deletions src/Pivot/PivotNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,20 @@

use App\Interfaces\SortModeInterface;
use App\Pivot\Aggregator\AbstractAggregator;
use App\Traits\ArrayTrait;
use App\Utils\StringUtils;

/**
* Represents a pivot node.
*/
class PivotNode extends AbstractPivotAggregator implements \Countable, \Stringable, SortModeInterface
{
use ArrayTrait;

/**
* The children.
*
* @var PivotNode[]
* @var array<int, PivotNode>
*/
private array $children = [];

Expand Down Expand Up @@ -147,13 +150,10 @@ public function equalsKeys(array $keys): bool
*/
public function find(mixed $key): ?self
{
foreach ($this->children as $child) {
if ($child->equalsKey($key)) {
return $child;
}
}

return null;
return $this->findFirst(
$this->children,
fn (int $key, PivotNode $child): bool => $child->equalsKey($key)
);
}

/**
Expand Down
62 changes: 28 additions & 34 deletions src/Pivot/PivotTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@

use App\Pivot\Aggregator\AbstractAggregator;
use App\Pivot\Field\PivotField;
use App\Traits\ArrayTrait;
use App\Utils\StringUtils;

/**
* The pivot table.
*/
class PivotTable extends AbstractPivotAggregator
{
use ArrayTrait;

/**
* The default path separator.
*/
Expand All @@ -29,7 +32,7 @@ class PivotTable extends AbstractPivotAggregator
/**
* The cell data.
*
* @var PivotCell[]
* @var array<int, PivotCell>
*/
private array $cells = [];

Expand All @@ -53,7 +56,7 @@ class PivotTable extends AbstractPivotAggregator
/**
* The root column.
*/
private readonly PivotNode $rootCol;
private readonly PivotNode $rootColumn;

/**
* The root row.
Expand All @@ -79,7 +82,7 @@ class PivotTable extends AbstractPivotAggregator
public function __construct(AbstractAggregator $aggregator, private ?string $title = null)
{
parent::__construct($aggregator);
$this->rootCol = new PivotNode(clone $aggregator);
$this->rootColumn = new PivotNode(clone $aggregator);
$this->rootRow = new PivotNode(clone $aggregator);
}

Expand Down Expand Up @@ -123,13 +126,10 @@ public function addCellValue(AbstractAggregator $aggregator, PivotNode $column,
*/
public function findCellByKey(mixed $columnKey, mixed $rowKey): ?PivotCell
{
foreach ($this->cells as $cell) {
if ($cell->equalsKey($columnKey, $rowKey)) {
return $cell;
}
}

return null;
return $this->findFirst(
$this->cells,
fn (int $key, PivotCell $cell): bool => $cell->equalsKey($columnKey, $rowKey)
);
}

/**
Expand All @@ -142,13 +142,10 @@ public function findCellByKey(mixed $columnKey, mixed $rowKey): ?PivotCell
*/
public function findCellByNode(PivotNode $column, PivotNode $row): ?PivotCell
{
foreach ($this->cells as $cell) {
if ($cell->equalsNode($column, $row)) {
return $cell;
}
}

return null;
return $this->findFirst(
$this->cells,
fn (int $key, PivotCell $cell): bool => $cell->equalsNode($column, $row)
);
}

/**
Expand All @@ -163,13 +160,10 @@ public function findCellByNode(PivotNode $column, PivotNode $row): ?PivotCell
*/
public function findCellByPath(string $columnPath, string $rowPath): ?PivotCell
{
foreach ($this->cells as $cell) {
if ($cell->equalsPath($columnPath, $rowPath)) {
return $cell;
}
}

return null;
return $this->findFirst(
$this->cells,
fn (int $key, PivotCell $cell): bool => $cell->equalsPath($columnPath, $rowPath)
);
}

/**
Expand All @@ -184,14 +178,6 @@ public function getCells(): array
return $this->cells;
}

/**
* Gets the root column.
*/
public function getColumn(): PivotNode
{
return $this->rootCol;
}

/**
* Gets the column fields.
*
Expand Down Expand Up @@ -224,10 +210,18 @@ public function getKeyField(): ?PivotField
return $this->keyField;
}

/**
* Gets the root column.
*/
public function getRootColumn(): PivotNode
{
return $this->rootColumn;
}

/**
* Gets the root row.
*/
public function getRow(): PivotNode
public function getRootRow(): PivotNode
{
return $this->rootRow;
}
Expand Down Expand Up @@ -272,7 +266,7 @@ public function jsonSerialize(): array
'dataField' => $this->dataField,
'columnFields' => $this->columnFields,
'rowFields' => $this->rowFields,
'column' => $this->rootCol,
'column' => $this->rootColumn,
'row' => $this->rootRow,
'cells' => $this->cells,
]);
Expand Down
8 changes: 4 additions & 4 deletions src/Pivot/PivotTableFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ public function create(): ?PivotTable
$keys[] = $key;
}
$value = $dataField?->getValue($row);
$currentCol = $this->setNodeValue($columnFields, $row, $table->getColumn(), $value);
$currentRow = $this->setNodeValue($rowFields, $row, $table->getRow(), $value);
$currentCol = $this->setNodeValue($columnFields, $row, $table->getRootColumn(), $value);
$currentRow = $this->setNodeValue($rowFields, $row, $table->getRootRow(), $value);
$cell = $table->findCellByNode($currentCol, $currentRow);
if ($cell instanceof PivotCell) {
$cell->addValue($value);
Expand All @@ -112,8 +112,8 @@ public function create(): ?PivotTable
->updateDataField($table, $dataField)
->updateRowFields($table, $rowFields)
->updateColumnFields($table, $columnFields);
$table->getColumn()->setTitle($this->buildFieldsTitle($columnFields));
$table->getRow()->setTitle($this->buildFieldsTitle($rowFields));
$table->getRootColumn()->setTitle($this->buildFieldsTitle($columnFields));
$table->getRootRow()->setTitle($this->buildFieldsTitle($rowFields));

return $table;
}
Expand Down
Loading

0 comments on commit 6962d27

Please sign in to comment.