Skip to content

Commit 1801390

Browse files
author
Kirill Nesmeyanov
committed
psalm est mort, vive le phpstan!
1 parent f84f413 commit 1801390

6 files changed

Lines changed: 37 additions & 56 deletions

File tree

composer.json

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
},
2020
"require-dev": {
2121
"friendsofphp/php-cs-fixer": "^3.53",
22+
"phpstan/extension-installer": "^1.3",
23+
"phpstan/phpstan": "^1.10",
24+
"phpstan/phpstan-strict-rules": "^1.5",
2225
"phpunit/phpunit": "^10.5",
23-
"rector/rector": "^1.0",
24-
"vimeo/psalm": "^5.23"
26+
"rector/rector": "^1.0"
2527
},
2628
"autoload-dev": {
2729
"psr-4": {
@@ -41,6 +43,9 @@
4143
"optimize-autoloader": true,
4244
"preferred-install": {
4345
"*": "dist"
46+
},
47+
"allow-plugins": {
48+
"phpstan/extension-installer": true
4449
}
4550
},
4651
"scripts": {
@@ -49,8 +54,8 @@
4954
"test:functional": "phpunit --testsuite=functional",
5055

5156
"linter": "@linter:check",
52-
"linter:check": "psalm --no-cache",
53-
"linter:fix": "psalm --no-cache --alter",
57+
"linter:check": "phpstan analyse --configuration phpstan.neon",
58+
"linter:baseline": "phpstan analyse --configuration phpstan.neon --generate-baseline",
5459

5560
"phpcs": "@phpcs:check",
5661
"phpcs:check": "php-cs-fixer fix --config=.php-cs-fixer.php --allow-risky=yes --dry-run --verbose --diff",

phpstan.neon

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
includes:
2+
- phar://phpstan.phar/conf/bleedingEdge.neon
3+
parameters:
4+
level: 9
5+
strictRules:
6+
allRules: true
7+
fileExtensions:
8+
- php
9+
paths:
10+
- src
11+
tmpDir: vendor/.cache.phpstan
12+
reportUnmatchedIgnoredErrors: false

psalm.xml

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/NativeTypePrinter.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@
2020
use TypeLang\Parser\Node\Stmt\UnionTypeNode;
2121
use TypeLang\Printer\Exception\NonPrintableNodeException;
2222

23-
/**
24-
* @psalm-suppress UndefinedAttributeClass : For "Override" attribute support.
25-
* @psalm-suppress InvalidAttribute : For "Override" attribute support.
26-
*/
2723
class NativeTypePrinter extends PrettyPrinter
2824
{
2925
/**
@@ -121,20 +117,6 @@ public function __construct(
121117
*/
122118
public function addTypeAlias(string $alias, string $type): void
123119
{
124-
if (\str_contains($type, '|')) {
125-
/** @psalm-suppress ArgumentTypeCoercion */
126-
$this->addUnionTypeAlias($alias, \explode('|', $type));
127-
128-
return;
129-
}
130-
131-
if (\str_contains($type, '&')) {
132-
/** @psalm-suppress ArgumentTypeCoercion */
133-
$this->addUnionTypeAlias($alias, \explode('&', $type));
134-
135-
return;
136-
}
137-
138120
$this->aliases[\strtolower($alias)] = $type;
139121
}
140122

@@ -210,7 +192,6 @@ protected function printClassConstNode(ClassConstNode $node): string
210192
protected function printUnionTypeNode(UnionTypeNode $node): string
211193
{
212194
try {
213-
/** @var non-empty-string */
214195
return \vsprintf($this->nesting > 0 ? '(%s)' : '%s', [
215196
\implode('|', [...$this->unwrapAndPrint($node)]),
216197
]);
@@ -223,7 +204,6 @@ protected function printUnionTypeNode(UnionTypeNode $node): string
223204
protected function printIntersectionTypeNode(IntersectionTypeNode $node): string
224205
{
225206
try {
226-
/** @var non-empty-string */
227207
return \vsprintf($this->nesting > 0 ? '(%s)' : '%s', [
228208
\implode('&', [...$this->unwrapAndPrint($node)]),
229209
]);

src/PrettyPrinter.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ protected function make(Statement $stmt): string
120120
}
121121

122122
/**
123+
* @param TypesListNode<TypeStatement> $node
123124
* @return non-empty-string
124125
* @throws NonPrintableNodeException
125126
*/
@@ -136,7 +137,6 @@ protected function printTypeListNode(TypesListNode $node): string
136137
*/
137138
protected function printTernaryType(TernaryConditionNode $node): string
138139
{
139-
/** @var non-empty-string */
140140
return \vsprintf('(%s %s %s ? %s : %s)', [
141141
$this->make($node->condition->subject),
142142
$this->printCondition($node->condition),
@@ -164,6 +164,7 @@ protected function printCondition(Condition $node): string
164164
}
165165

166166
/**
167+
* @param NullableTypeNode<TypeStatement> $node
167168
* @return non-empty-string
168169
* @throws NonPrintableNodeException
169170
*/
@@ -177,7 +178,6 @@ protected function printNullableType(NullableTypeNode $node): string
177178
*/
178179
protected function printClassConstNode(ClassConstNode $node): string
179180
{
180-
/** @var non-empty-string */
181181
return \vsprintf('%s::%s', [
182182
$node->class->toString(),
183183
(string) $node->constant?->toString(),
@@ -189,7 +189,6 @@ protected function printClassConstNode(ClassConstNode $node): string
189189
*/
190190
protected function printClassConstMaskNode(ClassConstMaskNode $node): string
191191
{
192-
/** @var non-empty-string */
193192
return \vsprintf('%s::%s', [
194193
$node->class->toString(),
195194
(string) ($node->constant?->toString()) . '*',
@@ -296,6 +295,7 @@ protected function printCallableArgumentNode(ParameterNode $node): string
296295
}
297296

298297
if ($node->name !== null) {
298+
// @phpstan-ignore-next-line : VariableLiteralNode is a subtype of LiteralNode
299299
$result[] = $this->printLiteralNode($node->name);
300300
}
301301

@@ -307,14 +307,14 @@ protected function printCallableArgumentNode(ParameterNode $node): string
307307
}
308308

309309
/**
310+
* @param UnionTypeNode<TypeStatement> $node
310311
* @return non-empty-string
311312
*/
312313
protected function printUnionTypeNode(UnionTypeNode $node): string
313314
{
314315
$delimiter = $this->wrapUnionType ? ' | ' : '|';
315316

316317
try {
317-
/** @var non-empty-string */
318318
return \vsprintf($this->nesting > 0 ? '(%s)' : '%s', [
319319
\implode($delimiter, [
320320
...$this->unwrapAndPrint($node),
@@ -326,14 +326,14 @@ protected function printUnionTypeNode(UnionTypeNode $node): string
326326
}
327327

328328
/**
329+
* @param IntersectionTypeNode<TypeStatement> $node
329330
* @return non-empty-string
330331
*/
331332
protected function printIntersectionTypeNode(IntersectionTypeNode $node): string
332333
{
333334
$delimiter = $this->wrapIntersectionType ? ' & ' : '&';
334335

335336
try {
336-
/** @var non-empty-string */
337337
return \vsprintf($this->nesting > 0 ? '(%s)' : '%s', [
338338
\implode($delimiter, [
339339
...$this->unwrapAndPrint($node),
@@ -345,6 +345,7 @@ protected function printIntersectionTypeNode(IntersectionTypeNode $node): string
345345
}
346346

347347
/**
348+
* @param LiteralNode<mixed> $node
348349
* @return non-empty-string
349350
*/
350351
protected function printLiteralNode(LiteralNode $node): string
@@ -383,7 +384,6 @@ protected function printTemplateArgumentsNode(TemplateArgumentsListNode $params)
383384
$result[] = $this->printTemplateArgumentNode($param);
384385
}
385386

386-
/** @var non-empty-string */
387387
return \sprintf('<%s>', \implode(', ', $result));
388388
}
389389

@@ -409,13 +409,11 @@ protected function printTemplateArgumentNode(TemplateArgumentNode $param): strin
409409
protected function printShapeFieldsNode(NamedTypeNode $node, FieldsListNode $shape): string
410410
{
411411
if (\count($shape->items) <= $this->multilineShape) {
412-
/** @var non-empty-string */
413412
return \vsprintf('{%s}', [
414413
\implode(', ', $this->getShapeFieldsNodes($node, $shape)),
415414
]);
416415
}
417416

418-
/** @var non-empty-string */
419417
return \vsprintf('{%s%s%s}', [
420418
$this->newLine,
421419
\implode(',' . $this->newLine, $this->nested(
@@ -466,7 +464,6 @@ protected function printShapeFieldNode(FieldNode $field): string
466464
$name .= '?';
467465
}
468466

469-
/** @var non-empty-string */
470467
return \vsprintf('%s: %s', [
471468
$name,
472469
$this->make($field->getType()),

src/Printer.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use TypeLang\Parser\Node\Statement;
88
use TypeLang\Parser\Node\Stmt\LogicalTypeNode;
9+
use TypeLang\Parser\Node\Stmt\TypeStatement;
910

1011
abstract class Printer implements PrinterInterface
1112
{
@@ -95,23 +96,25 @@ protected function printMap(iterable $stmts): array
9596
$result[] = $this->make($stmt);
9697
}
9798

98-
/** @var list<non-empty-string> */
9999
$result = \array_unique($result);
100100

101101
if (\in_array('mixed', $result, true)) {
102102
return ['mixed'];
103103
}
104104

105-
return $result;
105+
/** @var list<non-empty-string> */
106+
return $result;
106107
}
107108

108109
/**
109-
* @return iterable<array-key, Statement>
110+
* @template T of TypeStatement
111+
* @param LogicalTypeNode<T> $logical
112+
* @return iterable<array-key, T>
110113
*/
111114
protected function unwrap(LogicalTypeNode $logical): iterable
112115
{
113116
foreach ($logical->statements as $statement) {
114-
if ($statement instanceof $logical && $statement instanceof LogicalTypeNode) {
117+
if ($statement instanceof $logical) {
115118
yield from $this->unwrap($statement);
116119
} else {
117120
yield $statement;
@@ -120,7 +123,9 @@ protected function unwrap(LogicalTypeNode $logical): iterable
120123
}
121124

122125
/**
123-
* @return iterable<array-key, non-empty-string>
126+
* @param LogicalTypeNode<TypeStatement> $stmt
127+
*
128+
* @return list<non-empty-string>
124129
*/
125130
protected function unwrapAndPrint(LogicalTypeNode $stmt): iterable
126131
{

0 commit comments

Comments
 (0)