Skip to content

Commit

Permalink
Introduce RuleErrorBuilder::treatPhpDocTypesAsCertainTip() for a bi…
Browse files Browse the repository at this point in the history
…t more DRYness
  • Loading branch information
axlon committed May 1, 2024
1 parent b323fc3 commit a16184e
Show file tree
Hide file tree
Showing 22 changed files with 46 additions and 40 deletions.
2 changes: 1 addition & 1 deletion src/Rules/Classes/ImpossibleInstanceOfRule.php
Expand Up @@ -71,7 +71,7 @@ public function processNode(Node $node, Scope $scope): array
return $ruleErrorBuilder;
}

return $ruleErrorBuilder->tip('Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.');
return $ruleErrorBuilder->treatPhpDocTypesAsCertainTip();
};

if (!$instanceofType->getValue()) {
Expand Down
13 changes: 6 additions & 7 deletions src/Rules/Comparison/BooleanAndConstantConditionRule.php
Expand Up @@ -42,9 +42,8 @@ public function processNode(
$nodeText = $this->bleedingEdge ? $originalNode->getOperatorSigil() : '&&';
$leftType = $this->helper->getBooleanType($scope, $originalNode->left);
$identifierType = $originalNode instanceof Node\Expr\BinaryOp\BooleanAnd ? 'booleanAnd' : 'logicalAnd';
$tipText = 'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.';
if ($leftType instanceof ConstantBooleanType) {
$addTipLeft = function (RuleErrorBuilder $ruleErrorBuilder) use ($scope, $tipText, $originalNode): RuleErrorBuilder {
$addTipLeft = function (RuleErrorBuilder $ruleErrorBuilder) use ($scope, $originalNode): RuleErrorBuilder {
if (!$this->treatPhpDocTypesAsCertain) {
return $ruleErrorBuilder;
}
Expand All @@ -54,7 +53,7 @@ public function processNode(
return $ruleErrorBuilder;
}

return $ruleErrorBuilder->tip($tipText);
return $ruleErrorBuilder->treatPhpDocTypesAsCertainTip();
};

$isLast = $node->getAttribute(LastConditionVisitor::ATTRIBUTE_NAME);
Expand All @@ -79,7 +78,7 @@ public function processNode(
$originalNode->right,
);
if ($rightType instanceof ConstantBooleanType && !$scope->isInFirstLevelStatement()) {
$addTipRight = function (RuleErrorBuilder $ruleErrorBuilder) use ($rightScope, $originalNode, $tipText): RuleErrorBuilder {
$addTipRight = function (RuleErrorBuilder $ruleErrorBuilder) use ($rightScope, $originalNode): RuleErrorBuilder {
if (!$this->treatPhpDocTypesAsCertain) {
return $ruleErrorBuilder;
}
Expand All @@ -92,7 +91,7 @@ public function processNode(
return $ruleErrorBuilder;
}

return $ruleErrorBuilder->tip($tipText);
return $ruleErrorBuilder->treatPhpDocTypesAsCertainTip();
};

$isLast = $node->getAttribute(LastConditionVisitor::ATTRIBUTE_NAME);
Expand All @@ -114,7 +113,7 @@ public function processNode(
if (count($errors) === 0 && !$scope->isInFirstLevelStatement()) {
$nodeType = $this->treatPhpDocTypesAsCertain ? $scope->getType($originalNode) : $scope->getNativeType($originalNode);
if ($nodeType instanceof ConstantBooleanType) {
$addTip = function (RuleErrorBuilder $ruleErrorBuilder) use ($scope, $originalNode, $tipText): RuleErrorBuilder {
$addTip = function (RuleErrorBuilder $ruleErrorBuilder) use ($scope, $originalNode): RuleErrorBuilder {
if (!$this->treatPhpDocTypesAsCertain) {
return $ruleErrorBuilder;
}
Expand All @@ -124,7 +123,7 @@ public function processNode(
return $ruleErrorBuilder;
}

return $ruleErrorBuilder->tip($tipText);
return $ruleErrorBuilder->treatPhpDocTypesAsCertainTip();
};

$isLast = $node->getAttribute(LastConditionVisitor::ATTRIBUTE_NAME);
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Comparison/BooleanNotConstantConditionRule.php
Expand Up @@ -46,7 +46,7 @@ public function processNode(
return $ruleErrorBuilder;
}

return $ruleErrorBuilder->tip('Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.');
return $ruleErrorBuilder->treatPhpDocTypesAsCertainTip();
};

$isLast = $node->getAttribute(LastConditionVisitor::ATTRIBUTE_NAME);
Expand Down
13 changes: 6 additions & 7 deletions src/Rules/Comparison/BooleanOrConstantConditionRule.php
Expand Up @@ -42,9 +42,8 @@ public function processNode(
$messages = [];
$leftType = $this->helper->getBooleanType($scope, $originalNode->left);
$identifierType = $originalNode instanceof Node\Expr\BinaryOp\BooleanOr ? 'booleanOr' : 'logicalOr';
$tipText = 'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.';
if ($leftType instanceof ConstantBooleanType) {
$addTipLeft = function (RuleErrorBuilder $ruleErrorBuilder) use ($scope, $originalNode, $tipText): RuleErrorBuilder {
$addTipLeft = function (RuleErrorBuilder $ruleErrorBuilder) use ($scope, $originalNode): RuleErrorBuilder {
if (!$this->treatPhpDocTypesAsCertain) {
return $ruleErrorBuilder;
}
Expand All @@ -54,7 +53,7 @@ public function processNode(
return $ruleErrorBuilder;
}

return $ruleErrorBuilder->tip($tipText);
return $ruleErrorBuilder->treatPhpDocTypesAsCertainTip();
};

$isLast = $node->getAttribute(LastConditionVisitor::ATTRIBUTE_NAME);
Expand All @@ -79,7 +78,7 @@ public function processNode(
$originalNode->right,
);
if ($rightType instanceof ConstantBooleanType && !$scope->isInFirstLevelStatement()) {
$addTipRight = function (RuleErrorBuilder $ruleErrorBuilder) use ($rightScope, $originalNode, $tipText): RuleErrorBuilder {
$addTipRight = function (RuleErrorBuilder $ruleErrorBuilder) use ($rightScope, $originalNode): RuleErrorBuilder {
if (!$this->treatPhpDocTypesAsCertain) {
return $ruleErrorBuilder;
}
Expand All @@ -92,7 +91,7 @@ public function processNode(
return $ruleErrorBuilder;
}

return $ruleErrorBuilder->tip($tipText);
return $ruleErrorBuilder->treatPhpDocTypesAsCertainTip();
};

$isLast = $node->getAttribute(LastConditionVisitor::ATTRIBUTE_NAME);
Expand All @@ -114,7 +113,7 @@ public function processNode(
if (count($messages) === 0 && !$scope->isInFirstLevelStatement()) {
$nodeType = $this->treatPhpDocTypesAsCertain ? $scope->getType($originalNode) : $scope->getNativeType($originalNode);
if ($nodeType instanceof ConstantBooleanType) {
$addTip = function (RuleErrorBuilder $ruleErrorBuilder) use ($scope, $originalNode, $tipText): RuleErrorBuilder {
$addTip = function (RuleErrorBuilder $ruleErrorBuilder) use ($scope, $originalNode): RuleErrorBuilder {
if (!$this->treatPhpDocTypesAsCertain) {
return $ruleErrorBuilder;
}
Expand All @@ -124,7 +123,7 @@ public function processNode(
return $ruleErrorBuilder;
}

return $ruleErrorBuilder->tip($tipText);
return $ruleErrorBuilder->treatPhpDocTypesAsCertainTip();
};

$isLast = $node->getAttribute(LastConditionVisitor::ATTRIBUTE_NAME);
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Comparison/ConstantLooseComparisonRule.php
Expand Up @@ -51,7 +51,7 @@ public function processNode(Node $node, Scope $scope): array
return $ruleErrorBuilder;
}

return $ruleErrorBuilder->tip('Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.');
return $ruleErrorBuilder->treatPhpDocTypesAsCertainTip();
};

if (!$nodeType->getValue()) {
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Comparison/DoWhileLoopConstantConditionRule.php
Expand Up @@ -71,7 +71,7 @@ public function processNode(Node $node, Scope $scope): array
return $ruleErrorBuilder;
}

return $ruleErrorBuilder->tip('Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.');
return $ruleErrorBuilder->treatPhpDocTypesAsCertainTip();
};

return [
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Comparison/ElseIfConstantConditionRule.php
Expand Up @@ -46,7 +46,7 @@ public function processNode(
return $ruleErrorBuilder;
}

return $ruleErrorBuilder->tip('Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.');
return $ruleErrorBuilder->treatPhpDocTypesAsCertainTip();
};

$isLast = $node->cond->getAttribute(LastConditionVisitor::ATTRIBUTE_NAME);
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Comparison/IfConstantConditionRule.php
Expand Up @@ -44,7 +44,7 @@ public function processNode(
return $ruleErrorBuilder;
}

return $ruleErrorBuilder->tip('Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.');
return $ruleErrorBuilder->treatPhpDocTypesAsCertainTip();
};

return [
Expand Down
Expand Up @@ -55,7 +55,7 @@ public function processNode(Node $node, Scope $scope): array
return $ruleErrorBuilder;
}

return $ruleErrorBuilder->tip('Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.');
return $ruleErrorBuilder->treatPhpDocTypesAsCertainTip();
};

if (!$isAlways) {
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Comparison/ImpossibleCheckTypeMethodCallRule.php
Expand Up @@ -53,7 +53,7 @@ public function processNode(Node $node, Scope $scope): array
return $ruleErrorBuilder;
}

return $ruleErrorBuilder->tip('Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.');
return $ruleErrorBuilder->treatPhpDocTypesAsCertainTip();
};

if (!$isAlways) {
Expand Down
Expand Up @@ -53,7 +53,7 @@ public function processNode(Node $node, Scope $scope): array
return $ruleErrorBuilder;
}

return $ruleErrorBuilder->tip('Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.');
return $ruleErrorBuilder->treatPhpDocTypesAsCertainTip();
};

if (!$isAlways) {
Expand Down
9 changes: 4 additions & 5 deletions src/Rules/Comparison/LogicalXorConstantConditionRule.php
Expand Up @@ -34,9 +34,8 @@ public function processNode(Node $node, Scope $scope): array
{
$errors = [];
$leftType = $this->helper->getBooleanType($scope, $node->left);
$tipText = 'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.';
if ($leftType instanceof ConstantBooleanType) {
$addTipLeft = function (RuleErrorBuilder $ruleErrorBuilder) use ($scope, $tipText, $node): RuleErrorBuilder {
$addTipLeft = function (RuleErrorBuilder $ruleErrorBuilder) use ($scope, $node): RuleErrorBuilder {
if (!$this->treatPhpDocTypesAsCertain) {
return $ruleErrorBuilder;
}
Expand All @@ -46,7 +45,7 @@ public function processNode(Node $node, Scope $scope): array
return $ruleErrorBuilder;
}

return $ruleErrorBuilder->tip($tipText);
return $ruleErrorBuilder->treatPhpDocTypesAsCertainTip();
};

$isLast = $node->getAttribute(LastConditionVisitor::ATTRIBUTE_NAME);
Expand All @@ -66,7 +65,7 @@ public function processNode(Node $node, Scope $scope): array

$rightType = $this->helper->getBooleanType($scope, $node->right);
if ($rightType instanceof ConstantBooleanType) {
$addTipRight = function (RuleErrorBuilder $ruleErrorBuilder) use ($scope, $node, $tipText): RuleErrorBuilder {
$addTipRight = function (RuleErrorBuilder $ruleErrorBuilder) use ($scope, $node): RuleErrorBuilder {
if (!$this->treatPhpDocTypesAsCertain) {
return $ruleErrorBuilder;
}
Expand All @@ -79,7 +78,7 @@ public function processNode(Node $node, Scope $scope): array
return $ruleErrorBuilder;
}

return $ruleErrorBuilder->tip($tipText);
return $ruleErrorBuilder->treatPhpDocTypesAsCertainTip();
};

$isLast = $node->getAttribute(LastConditionVisitor::ATTRIBUTE_NAME);
Expand Down
Expand Up @@ -56,7 +56,7 @@ public function processNode(
return $ruleErrorBuilder;
}

return $ruleErrorBuilder->tip('Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.');
return $ruleErrorBuilder->treatPhpDocTypesAsCertainTip();
};

switch (get_class($node)) {
Expand Down
Expand Up @@ -54,7 +54,7 @@ public function processNode(Node $node, Scope $scope): array
return $ruleErrorBuilder;
}

return $ruleErrorBuilder->tip('Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.');
return $ruleErrorBuilder->treatPhpDocTypesAsCertainTip();
};

if (!$nodeType->getValue()) {
Expand Down
Expand Up @@ -44,7 +44,7 @@ public function processNode(
return $ruleErrorBuilder;
}

return $ruleErrorBuilder->tip('Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.');
return $ruleErrorBuilder->treatPhpDocTypesAsCertainTip();
};
return [
$addTip(RuleErrorBuilder::message(sprintf(
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Comparison/UnreachableIfBranchesRule.php
Expand Up @@ -49,7 +49,7 @@ public function processNode(Node $node, Scope $scope): array
return $ruleErrorBuilder;
}

return $ruleErrorBuilder->tip('Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.');
return $ruleErrorBuilder->treatPhpDocTypesAsCertainTip();
};

foreach ($node->elseifs as $elseif) {
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Comparison/UnreachableTernaryElseBranchRule.php
Expand Up @@ -50,7 +50,7 @@ public function processNode(Node $node, Scope $scope): array
return $ruleErrorBuilder;
}

return $ruleErrorBuilder->tip('Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.');
return $ruleErrorBuilder->treatPhpDocTypesAsCertainTip();
};
return [
$addTip(RuleErrorBuilder::message('Else branch is unreachable because ternary operator condition is always true.'))
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Comparison/WhileLoopAlwaysFalseConditionRule.php
Expand Up @@ -44,7 +44,7 @@ public function processNode(
return $ruleErrorBuilder;
}

return $ruleErrorBuilder->tip('Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.');
return $ruleErrorBuilder->treatPhpDocTypesAsCertainTip();
};

return [
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Comparison/WhileLoopAlwaysTrueConditionRule.php
Expand Up @@ -71,7 +71,7 @@ public function processNode(
return $ruleErrorBuilder;
}

return $ruleErrorBuilder->tip('Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.');
return $ruleErrorBuilder->treatPhpDocTypesAsCertainTip();
};

return [
Expand Down
6 changes: 3 additions & 3 deletions src/Rules/Functions/ArrayFilterRule.php
Expand Up @@ -80,7 +80,7 @@ public function processNode(Node $node, Scope $scope): array
if ($this->treatPhpDocTypesAsCertain) {
$nativeArrayType = $scope->getNativeType($args[0]->value);
if (!$nativeArrayType->isIterableAtLeastOnce()->no()) {
$errorBuilder->tip('Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.');
$errorBuilder->treatPhpDocTypesAsCertainTip();
}
}
return [
Expand All @@ -102,7 +102,7 @@ public function processNode(Node $node, Scope $scope): array
$nativeArrayType = $scope->getNativeType($args[0]->value);
$isNativeSuperType = $falsyType->isSuperTypeOf($nativeArrayType->getIterableValueType());
if (!$isNativeSuperType->no()) {
$errorBuilder->tip('Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.');
$errorBuilder->treatPhpDocTypesAsCertainTip();
}
}

Expand All @@ -122,7 +122,7 @@ public function processNode(Node $node, Scope $scope): array
$nativeArrayType = $scope->getNativeType($args[0]->value);
$isNativeSuperType = $falsyType->isSuperTypeOf($nativeArrayType->getIterableValueType());
if (!$isNativeSuperType->yes()) {
$errorBuilder->tip('Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.');
$errorBuilder->treatPhpDocTypesAsCertainTip();
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Rules/Functions/ArrayValuesRule.php
Expand Up @@ -85,7 +85,7 @@ public function processNode(Node $node, Scope $scope): array
if ($this->treatPhpDocTypesAsCertain) {
$nativeArrayType = $scope->getNativeType($args[0]->value);
if (!$nativeArrayType->isIterableAtLeastOnce()->no()) {
$errorBuilder->tip('Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.');
$errorBuilder->treatPhpDocTypesAsCertainTip();
}
}

Expand All @@ -103,7 +103,7 @@ public function processNode(Node $node, Scope $scope): array
if ($this->treatPhpDocTypesAsCertain) {
$nativeArrayType = $scope->getNativeType($args[0]->value);
if (!$nativeArrayType->isList()->yes()) {
$errorBuilder->tip('Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.');
$errorBuilder->treatPhpDocTypesAsCertainTip();
}
}

Expand Down
9 changes: 9 additions & 0 deletions src/Rules/RuleErrorBuilder.php
Expand Up @@ -201,6 +201,15 @@ public function acceptsReasonsTip(array $reasons): self
return $this;
}

/**
* @phpstan-this-out self<T&TipRuleError>
* @return self<T&TipRuleError>
*/
public function treatPhpDocTypesAsCertainTip(): self
{
return $this->tip('Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.');
}

/**
* Sets an error identifier.
*
Expand Down

0 comments on commit a16184e

Please sign in to comment.