Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent repetitive calls to Type::getConstantArrays() #2864

Merged
merged 1 commit into from
Jan 11, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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