Skip to content

Commit e06c529

Browse files
committedApr 3, 2023
RuleErrorBuilder - support multiple tips nicely
1 parent 79d8f13 commit e06c529

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed
 

‎phpstan-baseline.neon

+5
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,11 @@ parameters:
623623
count: 1
624624
path: src/Rules/PhpDoc/VarTagTypeRuleHelper.php
625625

626+
-
627+
message: "#^Access to an undefined property PHPStan\\\\Rules\\\\RuleError\\:\\:\\$tip\\.$#"
628+
count: 2
629+
path: src/Rules/RuleErrorBuilder.php
630+
626631
-
627632
message: "#^Doing instanceof PHPStan\\\\Type\\\\NullType is error\\-prone and deprecated\\. Use Type\\:\\:isNull\\(\\) instead\\.$#"
628633
count: 2

‎src/Rules/RuleErrorBuilder.php

+23-8
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ class RuleErrorBuilder
2626
/** @var mixed[] */
2727
private array $properties;
2828

29+
/** @var list<string> */
30+
private array $tips = [];
31+
2932
private function __construct(string $message)
3033
{
3134
$this->properties['message'] = $message;
@@ -106,7 +109,15 @@ public function file(string $file): self
106109

107110
public function tip(string $tip): self
108111
{
109-
$this->properties['tip'] = $tip;
112+
$this->tips = [$tip];
113+
$this->type |= self::TYPE_TIP;
114+
115+
return $this;
116+
}
117+
118+
public function addTip(string $tip): self
119+
{
120+
$this->tips[] = $tip;
110121
$this->type |= self::TYPE_TIP;
111122

112123
return $this;
@@ -122,15 +133,11 @@ public function discoveringSymbolsTip(): self
122133
*/
123134
public function acceptsReasonsTip(array $reasons): self
124135
{
125-
if (count($reasons) === 0) {
126-
return $this;
127-
}
128-
129-
if (count($reasons) === 1) {
130-
return $this->tip($reasons[0]);
136+
foreach ($reasons as $reason) {
137+
$this->addTip($reason);
131138
}
132139

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

136143
public function identifier(string $identifier): self
@@ -172,6 +179,14 @@ public function build(): RuleError
172179
$ruleError->{$propertyName} = $value;
173180
}
174181

182+
if (count($this->tips) > 0) {
183+
if (count($this->tips) === 1) {
184+
$ruleError->tip = $this->tips[0];
185+
} else {
186+
$ruleError->tip = implode("\n", array_map(static fn (string $tip) => sprintf('• %s', $tip), $this->tips));
187+
}
188+
}
189+
175190
return $ruleError;
176191
}
177192

0 commit comments

Comments
 (0)
Please sign in to comment.