Skip to content

Commit

Permalink
RuleErrorBuilder - support multiple tips nicely
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Apr 3, 2023
1 parent 79d8f13 commit e06c529
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
5 changes: 5 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,11 @@ parameters:
count: 1
path: src/Rules/PhpDoc/VarTagTypeRuleHelper.php

-
message: "#^Access to an undefined property PHPStan\\\\Rules\\\\RuleError\\:\\:\\$tip\\.$#"
count: 2
path: src/Rules/RuleErrorBuilder.php

-
message: "#^Doing instanceof PHPStan\\\\Type\\\\NullType is error\\-prone and deprecated\\. Use Type\\:\\:isNull\\(\\) instead\\.$#"
count: 2
Expand Down
31 changes: 23 additions & 8 deletions src/Rules/RuleErrorBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ class RuleErrorBuilder
/** @var mixed[] */
private array $properties;

/** @var list<string> */
private array $tips = [];

private function __construct(string $message)
{
$this->properties['message'] = $message;
Expand Down Expand Up @@ -106,7 +109,15 @@ public function file(string $file): self

public function tip(string $tip): self
{
$this->properties['tip'] = $tip;
$this->tips = [$tip];
$this->type |= self::TYPE_TIP;

return $this;
}

public function addTip(string $tip): self
{
$this->tips[] = $tip;
$this->type |= self::TYPE_TIP;

return $this;
Expand All @@ -122,15 +133,11 @@ public function discoveringSymbolsTip(): self
*/
public function acceptsReasonsTip(array $reasons): self
{
if (count($reasons) === 0) {
return $this;
}

if (count($reasons) === 1) {
return $this->tip($reasons[0]);
foreach ($reasons as $reason) {
$this->addTip($reason);
}

return $this->tip(implode("\n", array_map(static fn (string $reason) => sprintf('• %s', $reason), $reasons)));
return $this;
}

public function identifier(string $identifier): self
Expand Down Expand Up @@ -172,6 +179,14 @@ public function build(): RuleError
$ruleError->{$propertyName} = $value;
}

if (count($this->tips) > 0) {
if (count($this->tips) === 1) {
$ruleError->tip = $this->tips[0];
} else {
$ruleError->tip = implode("\n", array_map(static fn (string $tip) => sprintf('• %s', $tip), $this->tips));
}
}

return $ruleError;
}

Expand Down

0 comments on commit e06c529

Please sign in to comment.