Skip to content

Commit

Permalink
Improve performance of ArrayContentDetector (#813)
Browse files Browse the repository at this point in the history
* Improve performance of `ArrayContentDetector`

* Review changes
  • Loading branch information
stloyd committed Nov 21, 2023
1 parent f59a980 commit 89a4ce1
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/core/etl/src/Flow/ETL/PHP/Type/ArrayContentDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@

final class ArrayContentDetector
{
private readonly int $uniqueValuesCount;

public function __construct(private readonly Types $uniqueKeysType, private readonly Types $uniqueValuesType)
{
$this->uniqueValuesCount = $this->uniqueValuesType->without(ArrayType::empty(), new NullType())->count();
}

public function firstKeyType() : ?ScalarType
Expand All @@ -37,21 +40,20 @@ public function isList() : bool
return false;
}

return 1 === $this->uniqueValuesType->without(ArrayType::empty(), new NullType())->count();
return 1 === $this->uniqueValuesCount;
}

public function isMap() : bool
{
if (1 === $this->uniqueValuesType->without(ArrayType::empty(), new NullType())->count()) {
if ($this->isList()) {
return false;
}
if (!$this->firstKeyType()?->isValidArrayKey()) {
return false;
}

if (!$this->firstKeyType()?->isValidArrayKey()) {
return false;
if (1 === $this->uniqueValuesCount) {
/** @psalm-suppress PossiblyNullReference */
if (!$this->firstKeyType()->isInteger()) {
return 1 === $this->uniqueKeysType->count();
}

return 1 === $this->uniqueKeysType->count();
}

return false;
Expand All @@ -65,6 +67,6 @@ public function isStructure() : bool

return $this->firstKeyType()?->isString()
&& 1 === $this->uniqueKeysType->count()
&& 0 !== $this->uniqueValuesType->without(ArrayType::empty(), new NullType())->count();
&& 0 !== $this->uniqueValuesCount;
}
}

0 comments on commit 89a4ce1

Please sign in to comment.