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 overly greedy $scope->getType() calls in Yield*Rules #2100

Merged
merged 2 commits into from Dec 13, 2022
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
3 changes: 2 additions & 1 deletion src/Rules/Arrays/OffsetAccessValueAssignmentRule.php
Expand Up @@ -53,7 +53,6 @@ public function processNode(Node $node, Scope $scope): array
$assignedValueType = $scope->getType($node);
}

$originalArrayType = $scope->getType($arrayDimFetch->var);
$arrayTypeResult = $this->ruleLevelHelper->findTypeToCheck(
$scope,
$arrayDimFetch->var,
Expand All @@ -76,6 +75,8 @@ static function (Type $varType) use ($assignedValueType): bool {
return [];
}

$originalArrayType = $scope->getType($arrayDimFetch->var);

return [
RuleErrorBuilder::message(sprintf(
'%s does not accept %s.',
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Generators/YieldFromTypeRule.php
Expand Up @@ -126,7 +126,7 @@ public function processNode(Node $node, Scope $scope): array
))->build();
}

if ($scope->getType($node)->isVoid()->yes() && !$scope->isInFirstLevelStatement()) {
if (!$scope->isInFirstLevelStatement() && $scope->getType($node)->isVoid()->yes()) {
$messages[] = RuleErrorBuilder::message('Result of yield from (void) is used.')->build();
}

Expand Down
15 changes: 8 additions & 7 deletions src/Rules/Generators/YieldTypeRule.php
Expand Up @@ -53,12 +53,6 @@ public function processNode(Node $node, Scope $scope): array
$keyType = $scope->getType($node->key);
}

if ($node->value === null) {
$valueType = new NullType();
} else {
$valueType = $scope->getType($node->value);
}

$messages = [];
if (!$this->ruleLevelHelper->accepts($returnType->getIterableKeyType(), $keyType, $scope->isDeclareStrictTypes())) {
$verbosityLevel = VerbosityLevel::getRecommendedLevelByType($returnType->getIterableKeyType(), $keyType);
Expand All @@ -68,6 +62,13 @@ public function processNode(Node $node, Scope $scope): array
$keyType->describe($verbosityLevel),
))->build();
}

if ($node->value === null) {
$valueType = new NullType();
} else {
$valueType = $scope->getType($node->value);
}

if (!$this->ruleLevelHelper->accepts($returnType->getIterableValueType(), $valueType, $scope->isDeclareStrictTypes())) {
$verbosityLevel = VerbosityLevel::getRecommendedLevelByType($returnType->getIterableValueType(), $valueType);
$messages[] = RuleErrorBuilder::message(sprintf(
Expand All @@ -76,7 +77,7 @@ public function processNode(Node $node, Scope $scope): array
$valueType->describe($verbosityLevel),
))->build();
}
if ($scope->getType($node)->isVoid()->yes() && !$scope->isInFirstLevelStatement()) {
if (!$scope->isInFirstLevelStatement() && $scope->getType($node)->isVoid()->yes()) {
$messages[] = RuleErrorBuilder::message('Result of yield (void) is used.')->build();
}

Expand Down