Skip to content

Commit 4520cd7

Browse files
authored
Allow passing an array to errorTip (#367)
You can now add multiple tips per error as an array and PHPStan will print all the tips like this: 💡• a tip • another one (PHPStan is adding the `•` character, yes) Close #366
2 parents 4be6274 + e69679c commit 4520cd7

30 files changed

+223
-77
lines changed

extension.neon

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ parametersSchema:
4242
?disallowInClassWithMethodAttributes: listOf(string()),
4343
?allowInUse: bool(),
4444
?errorIdentifier: string(),
45-
?errorTip: string(),
45+
?errorTip: anyOf(string(), listOf(string())),
4646
])
4747
)
4848
disallowedClasses: listOf(
@@ -72,7 +72,7 @@ parametersSchema:
7272
?disallowInClassWithMethodAttributes: listOf(string()),
7373
?allowInUse: bool(),
7474
?errorIdentifier: string(),
75-
?errorTip: string(),
75+
?errorTip: anyOf(string(), listOf(string())),
7676
])
7777
)
7878
disallowedMethodCalls: listOf(
@@ -125,7 +125,7 @@ parametersSchema:
125125
?allowExceptInClassWithMethodAttributes: listOf(string()),
126126
?disallowInClassWithMethodAttributes: listOf(string()),
127127
?errorIdentifier: string(),
128-
?errorTip: string(),
128+
?errorTip: anyOf(string(), listOf(string())),
129129
])
130130
)
131131
disallowedStaticCalls: listOf(
@@ -178,7 +178,7 @@ parametersSchema:
178178
?allowExceptInClassWithMethodAttributes: listOf(string()),
179179
?disallowInClassWithMethodAttributes: listOf(string()),
180180
?errorIdentifier: string(),
181-
?errorTip: string(),
181+
?errorTip: anyOf(string(), listOf(string())),
182182
])
183183
)
184184
disallowedFunctionCalls: listOf(
@@ -231,7 +231,7 @@ parametersSchema:
231231
?allowExceptInClassWithMethodAttributes: listOf(string()),
232232
?disallowInClassWithMethodAttributes: listOf(string()),
233233
?errorIdentifier: string(),
234-
?errorTip: string(),
234+
?errorTip: anyOf(string(), listOf(string())),
235235
])
236236
)
237237
disallowedConstants: listOf(
@@ -264,7 +264,7 @@ parametersSchema:
264264
?allowExceptInClassWithMethodAttributes: listOf(string()),
265265
?disallowInClassWithMethodAttributes: listOf(string()),
266266
?errorIdentifier: string(),
267-
?errorTip: string(),
267+
?errorTip: anyOf(string(), listOf(string())),
268268
])
269269
)
270270
disallowedEnums: listOf(
@@ -296,7 +296,7 @@ parametersSchema:
296296
?allowExceptInClassWithMethodAttributes: listOf(string()),
297297
?disallowInClassWithMethodAttributes: listOf(string()),
298298
?errorIdentifier: string(),
299-
?errorTip: string(),
299+
?errorTip: anyOf(string(), listOf(string())),
300300
])
301301
)
302302
disallowedSuperglobals: listOf(
@@ -328,7 +328,7 @@ parametersSchema:
328328
?allowExceptInClassWithMethodAttributes: listOf(string()),
329329
?disallowInClassWithMethodAttributes: listOf(string()),
330330
?errorIdentifier: string(),
331-
?errorTip: string(),
331+
?errorTip: anyOf(string(), listOf(string())),
332332
])
333333
)
334334
disallowedAttributes: listOf(
@@ -379,7 +379,7 @@ parametersSchema:
379379
?allowExceptInClassWithMethodAttributes: listOf(string()),
380380
?disallowInClassWithMethodAttributes: listOf(string()),
381381
?errorIdentifier: string(),
382-
?errorTip: string(),
382+
?errorTip: anyOf(string(), listOf(string())),
383383
])
384384
)
385385
disallowedControlStructures: listOf(
@@ -412,7 +412,7 @@ parametersSchema:
412412
?allowExceptInClassWithMethodAttributes: listOf(string()),
413413
?disallowInClassWithMethodAttributes: listOf(string()),
414414
?errorIdentifier: string(),
415-
?errorTip: string(),
415+
?errorTip: anyOf(string(), listOf(string())),
416416
])
417417
)
418418

@@ -440,6 +440,7 @@ services:
440440
- Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedNamespaceRuleErrors
441441
- Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedCallsRuleErrors
442442
- Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedVariableRuleErrors
443+
- Spaze\PHPStan\Rules\Disallowed\RuleErrors\ErrorTips
443444
- Spaze\PHPStan\Rules\Disallowed\Type\TypeResolver
444445
- Spaze\PHPStan\Rules\Disallowed\UsageFactory\NamespaceUsageFactory
445446
-

phpstan.neon

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ parameters:
1111
AllowParamDirectives: 'allowParamsInAllowed?:CallParamConfig, allowParamsInAllowedAnyValue?:CallParamAnyValueConfig, allowParamFlagsInAllowed?:CallParamFlagAnyValueConfig, allowParamsAnywhere?:CallParamConfig, allowParamsAnywhereAnyValue?:CallParamAnyValueConfig, allowParamFlagsAnywhere?:CallParamFlagAnyValueConfig, allowExceptParamsInAllowed?:CallParamConfig, allowExceptParamFlagsInAllowed?:CallParamFlagAnyValueConfig, disallowParamFlagsInAllowed?:CallParamFlagAnyValueConfig, disallowParamsInAllowed?:CallParamConfig, allowExceptParams?:CallParamConfig, disallowParams?:CallParamConfig, allowExceptParamsAnyValue?:CallParamAnyValueConfig, disallowParamsAnyValue?:CallParamAnyValueConfig, allowExceptParamFlags?:CallParamFlagAnyValueConfig, disallowParamFlags?:CallParamFlagAnyValueConfig, allowExceptCaseInsensitiveParams?:CallParamConfig, disallowCaseInsensitiveParams?:CallParamConfig'
1212
AllowAttributesDirectives: 'allowInClassWithAttributes?:list<string>, allowExceptInClassWithAttributes?:list<string>, disallowInClassWithAttributes?:list<string>, allowInFunctionsWithAttributes?:list<string>, allowInMethodsWithAttributes?:list<string>, allowExceptInFunctionsWithAttributes?:list<string>, allowExceptInMethodsWithAttributes?:list<string>, disallowInFunctionsWithAttributes?:list<string>, disallowInMethodsWithAttributes?:list<string>, allowInClassWithMethodAttributes?:list<string>, allowExceptInClassWithMethodAttributes?:list<string>, disallowInClassWithMethodAttributes?:list<string>'
1313
AllowDirectives: 'allowIn?:list<string>, allowExceptIn?:list<string>, disallowIn?:list<string>, allowInFunctions?:list<string>, allowInMethods?:list<string>, allowExceptInFunctions?:list<string>, allowExceptInMethods?:list<string>, disallowInFunctions?:list<string>, disallowInMethods?:list<string>, allowInInstanceOf?:list<string>, allowExceptInInstanceOf?:list<string>, disallowInInstanceOf?:list<string>, %typeAliases.AllowParamDirectives%, %typeAliases.AllowAttributesDirectives%'
14-
ForbiddenCallsConfig: 'array<array{function?:string|list<string>, method?:string|list<string>, exclude?:string|list<string>, definedIn?:string|list<string>, message?:string, %typeAliases.AllowDirectives%, errorIdentifier?:string, errorTip?:string}>'
15-
DisallowedAttributesConfig: 'array<array{attribute:string|list<string>, exclude?:string|list<string>, message?:string, %typeAliases.AllowDirectives%, errorIdentifier?:string, errorTip?:string}>'
14+
ForbiddenCallsConfig: 'array<array{function?:string|list<string>, method?:string|list<string>, exclude?:string|list<string>, definedIn?:string|list<string>, message?:string, %typeAliases.AllowDirectives%, errorIdentifier?:string, errorTip?:string|list<string>}>'
15+
DisallowedAttributesConfig: 'array<array{attribute:string|list<string>, exclude?:string|list<string>, message?:string, %typeAliases.AllowDirectives%, errorIdentifier?:string, errorTip?:string|list<string>}>'
1616
AllowDirectivesConfig: 'array{%typeAliases.AllowDirectives%}'
1717
excludePaths:
1818
analyse:

src/DisallowedAttribute.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ class DisallowedAttribute implements DisallowedWithParams
1919

2020
private ?string $errorIdentifier;
2121

22-
private ?string $errorTip;
22+
/** @var list<string> */
23+
private array $errorTip;
2324

2425

2526
/**
@@ -28,22 +29,22 @@ class DisallowedAttribute implements DisallowedWithParams
2829
* @param string|null $message
2930
* @param AllowedConfig $allowedConfig
3031
* @param string|null $errorIdentifier
31-
* @param string|null $errorTip
32+
* @param string|list<string> $errorTip
3233
*/
3334
public function __construct(
3435
string $attribute,
3536
array $excludes,
3637
?string $message,
3738
AllowedConfig $allowedConfig,
3839
?string $errorIdentifier,
39-
?string $errorTip
40+
$errorTip
4041
) {
4142
$this->attribute = $attribute;
4243
$this->excludes = $excludes;
4344
$this->message = $message;
4445
$this->allowedConfig = $allowedConfig;
4546
$this->errorIdentifier = $errorIdentifier;
46-
$this->errorTip = $errorTip;
47+
$this->errorTip = (array)$errorTip;
4748
}
4849

4950

@@ -172,7 +173,10 @@ public function getErrorIdentifier(): ?string
172173
}
173174

174175

175-
public function getErrorTip(): ?string
176+
/**
177+
* @return list<string>
178+
*/
179+
public function getErrorTip(): array
176180
{
177181
return $this->errorTip;
178182
}

src/DisallowedAttributeFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function createFromConfig(array $config): array
4444
$disallowed['message'] ?? null,
4545
$this->allowedConfigFactory->getConfig($disallowed),
4646
$disallowed['errorIdentifier'] ?? null,
47-
$disallowed['errorTip'] ?? null
47+
$disallowed['errorTip'] ?? []
4848
);
4949
$disallowedAttributes[$disallowedAttribute->getAttribute()] = $disallowedAttribute;
5050
}

src/DisallowedCall.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ class DisallowedCall implements DisallowedWithParams
2222

2323
private ?string $errorIdentifier;
2424

25-
private ?string $errorTip;
25+
/** @var list<string> */
26+
private array $errorTip;
2627

2728

2829
/**
@@ -32,7 +33,7 @@ class DisallowedCall implements DisallowedWithParams
3233
* @param string|null $message
3334
* @param AllowedConfig $allowedConfig
3435
* @param string|null $errorIdentifier
35-
* @param string|null $errorTip
36+
* @param string|list<string> $errorTip
3637
*/
3738
public function __construct(
3839
string $call,
@@ -41,15 +42,15 @@ public function __construct(
4142
?string $message,
4243
AllowedConfig $allowedConfig,
4344
?string $errorIdentifier,
44-
?string $errorTip
45+
$errorTip
4546
) {
4647
$this->call = $call;
4748
$this->excludes = $excludes;
4849
$this->definedIn = $definedIn;
4950
$this->message = $message;
5051
$this->allowedConfig = $allowedConfig;
5152
$this->errorIdentifier = $errorIdentifier;
52-
$this->errorTip = $errorTip;
53+
$this->errorTip = (array)$errorTip;
5354
}
5455

5556

@@ -187,7 +188,10 @@ public function getErrorIdentifier(): ?string
187188
}
188189

189190

190-
public function getErrorTip(): ?string
191+
/**
192+
* @return list<string>
193+
*/
194+
public function getErrorTip(): array
191195
{
192196
return $this->errorTip;
193197
}

src/DisallowedCallFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function createFromConfig(array $config): array
5757
$disallowed['message'] ?? null,
5858
$this->allowedConfigFactory->getConfig($disallowed),
5959
$disallowed['errorIdentifier'] ?? null,
60-
$disallowed['errorTip'] ?? null
60+
$disallowed['errorTip'] ?? []
6161
);
6262
$disallowedCalls[$disallowedCall->getKey()] = $disallowedCall;
6363
}

src/DisallowedConstant.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,29 @@ class DisallowedConstant implements Disallowed
1616

1717
private ?string $errorIdentifier;
1818

19-
private ?string $errorTip;
19+
/** @var list<string> */
20+
private array $errorTip;
2021

2122

2223
/**
2324
* @param string $constant
2425
* @param string|null $message
2526
* @param AllowedConfig $allowedConfig
2627
* @param string|null $errorIdentifier
27-
* @param string|null $errorTip
28+
* @param string|list<string> $errorTip
2829
*/
2930
public function __construct(
3031
string $constant,
3132
?string $message,
3233
AllowedConfig $allowedConfig,
3334
?string $errorIdentifier,
34-
?string $errorTip
35+
$errorTip
3536
) {
3637
$this->constant = $constant;
3738
$this->message = $message;
3839
$this->allowedConfig = $allowedConfig;
3940
$this->errorIdentifier = $errorIdentifier;
40-
$this->errorTip = $errorTip;
41+
$this->errorTip = (array)$errorTip;
4142
}
4243

4344

@@ -133,7 +134,10 @@ public function getErrorIdentifier(): ?string
133134
}
134135

135136

136-
public function getErrorTip(): ?string
137+
/**
138+
* @return list<string>
139+
*/
140+
public function getErrorTip(): array
137141
{
138142
return $this->errorTip;
139143
}

src/DisallowedConstantFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function __construct(Formatter $formatter, Normalizer $normalizer, Allowe
2828

2929

3030
/**
31-
* @param array<array{class?:string, enum?:string, constant?:string|list<string>, case?:string|list<string>, message?:string, allowIn?:list<string>, allowExceptIn?:list<string>, disallowIn?:list<string>, errorIdentifier?:string, errorTip?:string}> $config
31+
* @param array<array{class?:string, enum?:string, constant?:string|list<string>, case?:string|list<string>, message?:string, allowIn?:list<string>, allowExceptIn?:list<string>, disallowIn?:list<string>, errorIdentifier?:string, errorTip?:string|list<string>}> $config
3232
* @return list<DisallowedConstant>
3333
* @throws ShouldNotHappenException
3434
*/
@@ -50,7 +50,7 @@ public function createFromConfig(array $config): array
5050
$disallowed['message'] ?? null,
5151
$this->allowedConfigFactory->getConfig($disallowed),
5252
$disallowed['errorIdentifier'] ?? null,
53-
$disallowed['errorTip'] ?? null
53+
$disallowed['errorTip'] ?? []
5454
);
5555
$disallowedConstants[$disallowedConstant->getConstant()] = $disallowedConstant;
5656
}

src/DisallowedControlStructure.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,29 @@ class DisallowedControlStructure implements Disallowed
1616

1717
private ?string $errorIdentifier;
1818

19-
private ?string $errorTip;
19+
/** @var list<string> */
20+
private array $errorTip;
2021

2122

2223
/**
2324
* @param string $controlStructure
2425
* @param string|null $message
2526
* @param AllowedConfig $allowedConfig
2627
* @param string|null $errorIdentifier
27-
* @param string|null $errorTip
28+
* @param string|list<string> $errorTip
2829
*/
2930
public function __construct(
3031
string $controlStructure,
3132
?string $message,
3233
AllowedConfig $allowedConfig,
3334
?string $errorIdentifier,
34-
?string $errorTip
35+
$errorTip
3536
) {
3637
$this->controlStructure = $controlStructure;
3738
$this->message = $message;
3839
$this->allowedConfig = $allowedConfig;
3940
$this->errorIdentifier = $errorIdentifier;
40-
$this->errorTip = $errorTip;
41+
$this->errorTip = (array)$errorTip;
4142
}
4243

4344

@@ -133,7 +134,10 @@ public function getErrorIdentifier(): ?string
133134
}
134135

135136

136-
public function getErrorTip(): ?string
137+
/**
138+
* @return list<string>
139+
*/
140+
public function getErrorTip(): array
137141
{
138142
return $this->errorTip;
139143
}

src/DisallowedControlStructureFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function __construct(Formatter $formatter, AllowedConfigFactory $allowedC
4848

4949

5050
/**
51-
* @param array<array{controlStructure?:string|list<string>, structure?:string|list<string>, message?:string, allowIn?:list<string>, allowExceptIn?:list<string>, disallowIn?:list<string>, errorIdentifier?:string, errorTip?:string}> $config
51+
* @param array<array{controlStructure?:string|list<string>, structure?:string|list<string>, message?:string, allowIn?:list<string>, allowExceptIn?:list<string>, disallowIn?:list<string>, errorIdentifier?:string, errorTip?:string|list<string>}> $config
5252
* @return list<DisallowedControlStructure>
5353
* @throws ShouldNotHappenException
5454
*/
@@ -75,7 +75,7 @@ public function getDisallowedControlStructures(array $config): array
7575
$disallowed['message'] ?? null,
7676
$this->allowedConfigFactory->getConfig($disallowed),
7777
$disallowed['errorIdentifier'] ?? null,
78-
$disallowed['errorTip'] ?? null
78+
$disallowed['errorTip'] ?? []
7979
);
8080
$disallowedControlStructures[$disallowedControlStructure->getControlStructure()] = $disallowedControlStructure;
8181
}

0 commit comments

Comments
 (0)