Skip to content

Commit

Permalink
Prevent repetative calls to Type::getConstantArrays()
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm authored and ondrejmirtes committed Jan 11, 2024
1 parent 52e2406 commit 6d11aa9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
5 changes: 3 additions & 2 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -1401,9 +1401,10 @@ private function resolveType(string $exprString, Expr $node): Type
return $this->initializerExprTypeResolver->getType($node, InitializerExprContext::fromScope($this));
} elseif ($node instanceof Object_) {
$castToObject = static function (Type $type): Type {
if (count($type->getConstantArrays()) > 0) {
$constantArrays = $type->getConstantArrays();
if (count($constantArrays) > 0) {
$objects = [];
foreach ($type->getConstantArrays() as $constantArray) {
foreach ($constantArrays as $constantArray) {
$properties = [];
$optionalProperties = [];
foreach ($constantArray->getKeyTypes() as $i => $keyType) {
Expand Down
12 changes: 7 additions & 5 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2134,11 +2134,12 @@ static function (): void {
) {
$extractedArg = $expr->getArgs()[0]->value;
$extractedType = $scope->getType($extractedArg);
if (count($extractedType->getConstantArrays()) > 0) {
$constantArrays = $extractedType->getConstantArrays();
if (count($constantArrays) > 0) {
$properties = [];
$optionalProperties = [];
$refCount = [];
foreach ($extractedType->getConstantArrays() as $constantArray) {
foreach ($constantArrays as $constantArray) {
foreach ($constantArray->getKeyTypes() as $i => $keyType) {
if ($keyType->isString()->no()) {
// integers as variable names not allowed
Expand All @@ -2160,7 +2161,7 @@ static function (): void {
}
}
foreach ($properties as $name => $type) {
$optional = in_array($name, $optionalProperties, true) || $refCount[$name] < count($extractedType->getConstantArrays());
$optional = in_array($name, $optionalProperties, true) || $refCount[$name] < count($constantArrays);
$scope = $scope->assignVariable($name, $type, $type, $optional ? TrinaryLogic::createMaybe() : TrinaryLogic::createYes());
}
} else {
Expand Down Expand Up @@ -2995,8 +2996,9 @@ private function getArrayFunctionAppendingType(FunctionReflection $functionRefle
foreach ($callArgs as $callArg) {
$callArgType = $scope->getType($callArg->value);
if ($callArg->unpack) {
if (count($callArgType->getConstantArrays()) === 1) {
$iterableValueTypes = $callArgType->getConstantArrays()[0]->getValueTypes();
$constantArrays = $callArgType->getConstantArrays();
if (count($constantArrays) === 1) {
$iterableValueTypes = $constantArrays[0]->getValueTypes();
} else {
$iterableValueTypes = [$callArgType->getIterableValueType()];
$nonConstantArrayWasUnpacked = true;
Expand Down
5 changes: 3 additions & 2 deletions src/Reflection/InitializerExprTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -501,8 +501,9 @@ public function getArrayType(Expr\Array_ $expr, callable $getTypeCallback): Type

$valueType = $getTypeCallback($arrayItem->value);
if ($arrayItem->unpack) {
if (count($valueType->getConstantArrays()) === 1) {
$constantArrayType = $valueType->getConstantArrays()[0];
$constantArrays = $valueType->getConstantArrays();
if (count($constantArrays) === 1) {
$constantArrayType = $constantArrays[0];
$hasStringKey = false;
foreach ($constantArrayType->getKeyTypes() as $keyType) {
if ($keyType->isString()->yes()) {
Expand Down

0 comments on commit 6d11aa9

Please sign in to comment.