Skip to content

Commit fba2929

Browse files
committed
Use ExprPrinter in more places
1 parent e12524e commit fba2929

12 files changed

+38
-32
lines changed

src/Analyser/TypeSpecifier.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use PhpParser\Node\Expr\StaticCall;
1919
use PhpParser\Node\Expr\StaticPropertyFetch;
2020
use PhpParser\Node\Name;
21-
use PhpParser\PrettyPrinter\Standard;
21+
use PHPStan\Node\Printer\ExprPrinter;
2222
use PHPStan\Reflection\ParametersAcceptor;
2323
use PHPStan\Reflection\ParametersAcceptorSelector;
2424
use PHPStan\Reflection\ReflectionProvider;
@@ -84,7 +84,7 @@ class TypeSpecifier
8484
* @param StaticMethodTypeSpecifyingExtension[] $staticMethodTypeSpecifyingExtensions
8585
*/
8686
public function __construct(
87-
private Standard $printer,
87+
private ExprPrinter $exprPrinter,
8888
private ReflectionProvider $reflectionProvider,
8989
private array $functionTypeSpecifyingExtensions,
9090
private array $methodTypeSpecifyingExtensions,
@@ -332,8 +332,8 @@ public function specifyTypesInCondition(
332332
return $types;
333333
}
334334

335-
$leftExprString = $this->printer->prettyPrintExpr($expr->left);
336-
$rightExprString = $this->printer->prettyPrintExpr($expr->right);
335+
$leftExprString = $this->exprPrinter->printExpr($expr->left);
336+
$rightExprString = $this->exprPrinter->printExpr($expr->right);
337337
if ($leftExprString === $rightExprString) {
338338
if (!$expr->left instanceof Expr\Variable || !$expr->right instanceof Expr\Variable) {
339339
return new SpecifiedTypes([], [], false, [], $rootExpr);
@@ -475,8 +475,8 @@ public function specifyTypesInCondition(
475475
return $this->specifyTypesInCondition($scope, new Expr\BinaryOp\Identical($expr->left, $expr->right), $context, $rootExpr);
476476
}
477477

478-
$leftExprString = $this->printer->prettyPrintExpr($expr->left);
479-
$rightExprString = $this->printer->prettyPrintExpr($expr->right);
478+
$leftExprString = $this->exprPrinter->printExpr($expr->left);
479+
$rightExprString = $this->exprPrinter->printExpr($expr->right);
480480
if ($leftExprString === $rightExprString) {
481481
if (!$expr->left instanceof Expr\Variable || !$expr->right instanceof Expr\Variable) {
482482
return new SpecifiedTypes([], [], false, [], $rootExpr);
@@ -1228,7 +1228,7 @@ public function create(
12281228

12291229
$sureTypes = [];
12301230
$sureNotTypes = [];
1231-
$exprString = $this->printer->prettyPrintExpr($expr);
1231+
$exprString = $this->exprPrinter->printExpr($expr);
12321232
if ($context->false()) {
12331233
$sureNotTypes[$exprString] = [$expr, $type];
12341234
} elseif ($context->true()) {
@@ -1297,7 +1297,7 @@ private function createRangeTypes(?Expr $rootExpr, Expr $expr, Type $type, TypeS
12971297
$sureNotTypes = [];
12981298

12991299
if ($type instanceof IntegerRangeType || $type instanceof ConstantIntegerType) {
1300-
$exprString = $this->printer->prettyPrintExpr($expr);
1300+
$exprString = $this->exprPrinter->printExpr($expr);
13011301
if ($context->false()) {
13021302
$sureNotTypes[$exprString] = [$expr, $type];
13031303
} elseif ($context->true()) {

src/Analyser/TypeSpecifierFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace PHPStan\Analyser;
44

5-
use PhpParser\PrettyPrinter\Standard;
65
use PHPStan\Broker\BrokerFactory;
76
use PHPStan\DependencyInjection\Container;
7+
use PHPStan\Node\Printer\ExprPrinter;
88
use PHPStan\Reflection\ReflectionProvider;
99
use function array_merge;
1010

@@ -22,7 +22,7 @@ public function __construct(private Container $container)
2222
public function create(): TypeSpecifier
2323
{
2424
$typeSpecifier = new TypeSpecifier(
25-
$this->container->getByType(Standard::class),
25+
$this->container->getByType(ExprPrinter::class),
2626
$this->container->getByType(ReflectionProvider::class),
2727
$this->container->getServicesByTag(self::FUNCTION_TYPE_SPECIFYING_EXTENSION_TAG),
2828
$this->container->getServicesByTag(self::METHOD_TYPE_SPECIFYING_EXTENSION_TAG),

src/Dependency/ExportedNodeResolver.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use PhpParser\Node\Stmt\Class_;
88
use PhpParser\Node\Stmt\ClassMethod;
99
use PhpParser\Node\Stmt\Function_;
10-
use PhpParser\PrettyPrinter\Standard;
1110
use PHPStan\Dependency\ExportedNode\ExportedClassConstantNode;
1211
use PHPStan\Dependency\ExportedNode\ExportedClassConstantsNode;
1312
use PHPStan\Dependency\ExportedNode\ExportedClassNode;
@@ -21,6 +20,7 @@
2120
use PHPStan\Dependency\ExportedNode\ExportedPropertiesNode;
2221
use PHPStan\Dependency\ExportedNode\ExportedTraitNode;
2322
use PHPStan\Dependency\ExportedNode\ExportedTraitUseAdaptation;
23+
use PHPStan\Node\Printer\ExprPrinter;
2424
use PHPStan\ShouldNotHappenException;
2525
use PHPStan\Type\FileTypeMapper;
2626
use function array_map;
@@ -30,7 +30,7 @@
3030
class ExportedNodeResolver
3131
{
3232

33-
public function __construct(private FileTypeMapper $fileTypeMapper, private Standard $printer)
33+
public function __construct(private FileTypeMapper $fileTypeMapper, private ExprPrinter $exprPrinter)
3434
{
3535
}
3636

@@ -356,7 +356,7 @@ private function exportClassStatement(Node\Stmt $node, string $fileName, string
356356
foreach ($node->consts as $const) {
357357
$constants[] = new ExportedClassConstantNode(
358358
$const->name->toString(),
359-
$this->printer->prettyPrintExpr($const->value),
359+
$this->exprPrinter->printExpr($const->value),
360360
);
361361
}
362362

@@ -379,7 +379,7 @@ private function exportClassStatement(Node\Stmt $node, string $fileName, string
379379

380380
return new ExportedEnumCaseNode(
381381
$node->name->toString(),
382-
$node->expr !== null ? $this->printer->prettyPrintExpr($node->expr) : null,
382+
$node->expr !== null ? $this->exprPrinter->printExpr($node->expr) : null,
383383
$this->exportPhpDocNode(
384384
$fileName,
385385
$namespacedName,

src/Rules/Arrays/DuplicateKeysInLiteralArraysRule.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
namespace PHPStan\Rules\Arrays;
44

55
use PhpParser\Node;
6-
use PhpParser\PrettyPrinter\Standard;
76
use PHPStan\Analyser\Scope;
87
use PHPStan\Node\LiteralArrayNode;
8+
use PHPStan\Node\Printer\ExprPrinter;
99
use PHPStan\Rules\Rule;
1010
use PHPStan\Rules\RuleErrorBuilder;
1111
use PHPStan\Type\ConstantScalarType;
@@ -22,7 +22,7 @@ class DuplicateKeysInLiteralArraysRule implements Rule
2222
{
2323

2424
public function __construct(
25-
private Standard $printer,
25+
private ExprPrinter $exprPrinter,
2626
)
2727
{
2828
}
@@ -55,7 +55,7 @@ public function processNode(Node $node, Scope $scope): array
5555
continue;
5656
}
5757

58-
$printedValue = $this->printer->prettyPrintExpr($key);
58+
$printedValue = $this->exprPrinter->printExpr($key);
5959
$value = $keyType->getValue();
6060
$printedValues[$value][] = $printedValue;
6161

src/Rules/Cast/InvalidPartOfEncapsedStringRule.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
namespace PHPStan\Rules\Cast;
44

55
use PhpParser\Node;
6-
use PhpParser\PrettyPrinter\Standard;
76
use PHPStan\Analyser\Scope;
7+
use PHPStan\Node\Printer\ExprPrinter;
88
use PHPStan\Rules\Rule;
99
use PHPStan\Rules\RuleErrorBuilder;
1010
use PHPStan\Rules\RuleLevelHelper;
@@ -20,7 +20,7 @@ class InvalidPartOfEncapsedStringRule implements Rule
2020
{
2121

2222
public function __construct(
23-
private Standard $printer,
23+
private ExprPrinter $exprPrinter,
2424
private RuleLevelHelper $ruleLevelHelper,
2525
)
2626
{
@@ -56,7 +56,7 @@ public function processNode(Node $node, Scope $scope): array
5656
}
5757
$messages[] = RuleErrorBuilder::message(sprintf(
5858
'Part %s (%s) of encapsed string cannot be cast to string.',
59-
$this->printer->prettyPrintExpr($part),
59+
$this->exprPrinter->printExpr($part),
6060
$partType->describe(VerbosityLevel::value()),
6161
))->line($part->getLine())->build();
6262
}

src/Rules/DeadCode/NoopRule.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
namespace PHPStan\Rules\DeadCode;
44

55
use PhpParser\Node;
6-
use PhpParser\PrettyPrinter\Standard;
76
use PHPStan\Analyser\Scope;
7+
use PHPStan\Node\Printer\ExprPrinter;
88
use PHPStan\Rules\Rule;
99
use PHPStan\Rules\RuleErrorBuilder;
1010
use function sprintf;
@@ -15,7 +15,7 @@
1515
class NoopRule implements Rule
1616
{
1717

18-
public function __construct(private Standard $printer)
18+
public function __construct(private ExprPrinter $exprPrinter)
1919
{
2020
}
2121

@@ -54,7 +54,7 @@ public function processNode(Node $node, Scope $scope): array
5454
return [
5555
RuleErrorBuilder::message(sprintf(
5656
'Expression "%s" on a separate line does not do anything.',
57-
$this->printer->prettyPrintExpr($originalExpr),
57+
$this->exprPrinter->printExpr($originalExpr),
5858
))->line($expr->getLine())
5959
->identifier('deadCode.noopExpression')
6060
->metadata([

src/Rules/Operators/InvalidBinaryOperationRule.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
namespace PHPStan\Rules\Operators;
44

55
use PhpParser\Node;
6-
use PhpParser\PrettyPrinter\Standard;
76
use PHPStan\Analyser\MutatingScope;
87
use PHPStan\Analyser\Scope;
8+
use PHPStan\Node\Printer\ExprPrinter;
99
use PHPStan\Rules\Rule;
1010
use PHPStan\Rules\RuleErrorBuilder;
1111
use PHPStan\Rules\RuleLevelHelper;
@@ -24,7 +24,7 @@ class InvalidBinaryOperationRule implements Rule
2424
{
2525

2626
public function __construct(
27-
private Standard $printer,
27+
private ExprPrinter $exprPrinter,
2828
private RuleLevelHelper $ruleLevelHelper,
2929
)
3030
{
@@ -51,12 +51,14 @@ public function processNode(Node $node, Scope $scope): array
5151
$rightVariable = new Node\Expr\Variable($rightName);
5252
if ($node instanceof Node\Expr\AssignOp) {
5353
$newNode = clone $node;
54+
$newNode->setAttribute('phpstan_cache_printer', null);
5455
$left = $node->var;
5556
$right = $node->expr;
5657
$newNode->var = $leftVariable;
5758
$newNode->expr = $rightVariable;
5859
} else {
5960
$newNode = clone $node;
61+
$newNode->setAttribute('phpstan_cache_printer', null);
6062
$left = $node->left;
6163
$right = $node->right;
6264
$newNode->left = $leftVariable;
@@ -104,7 +106,7 @@ public function processNode(Node $node, Scope $scope): array
104106
return [
105107
RuleErrorBuilder::message(sprintf(
106108
'Binary operation "%s" between %s and %s results in an error.',
107-
substr(substr($this->printer->prettyPrintExpr($newNode), strlen($leftName) + 2), 0, -(strlen($rightName) + 2)),
109+
substr(substr($this->exprPrinter->printExpr($newNode), strlen($leftName) + 2), 0, -(strlen($rightName) + 2)),
108110
$scope->getType($left)->describe(VerbosityLevel::value()),
109111
$scope->getType($right)->describe(VerbosityLevel::value()),
110112
))->line($left->getLine())->build(),

tests/PHPStan/Analyser/AnalyserTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PHPStan\Dependency\DependencyResolver;
99
use PHPStan\Dependency\ExportedNodeResolver;
1010
use PHPStan\DependencyInjection\Type\DynamicThrowTypeExtensionProvider;
11+
use PHPStan\Node\Printer\ExprPrinter;
1112
use PHPStan\Node\Printer\Printer;
1213
use PHPStan\Parser\RichParser;
1314
use PHPStan\Php\PhpVersion;
@@ -463,7 +464,6 @@ private function createAnalyser(bool $reportUnmatchedIgnoredErrors): Analyser
463464
]);
464465

465466
$reflectionProvider = $this->createReflectionProvider();
466-
$printer = new Printer();
467467
$fileHelper = $this->getFileHelper();
468468

469469
$typeSpecifier = self::getContainer()->getService('typeSpecifier');
@@ -499,7 +499,7 @@ private function createAnalyser(bool $reportUnmatchedIgnoredErrors): Analyser
499499
new NameResolver(),
500500
self::getContainer(),
501501
),
502-
new DependencyResolver($fileHelper, $reflectionProvider, new ExportedNodeResolver($fileTypeMapper, $printer)),
502+
new DependencyResolver($fileHelper, $reflectionProvider, new ExportedNodeResolver($fileTypeMapper, new ExprPrinter(new Printer()))),
503503
$reportUnmatchedIgnoredErrors,
504504
);
505505

tests/PHPStan/Rules/Arrays/DuplicateKeysInLiteralArraysRuleTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PHPStan\Rules\Arrays;
44

5+
use PHPStan\Node\Printer\ExprPrinter;
56
use PHPStan\Node\Printer\Printer;
67
use PHPStan\Rules\Rule;
78
use PHPStan\Testing\RuleTestCase;
@@ -16,7 +17,7 @@ class DuplicateKeysInLiteralArraysRuleTest extends RuleTestCase
1617
protected function getRule(): Rule
1718
{
1819
return new DuplicateKeysInLiteralArraysRule(
19-
new Printer(),
20+
new ExprPrinter(new Printer()),
2021
);
2122
}
2223

tests/PHPStan/Rules/Cast/InvalidPartOfEncapsedStringRuleTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PHPStan\Rules\Cast;
44

5+
use PHPStan\Node\Printer\ExprPrinter;
56
use PHPStan\Node\Printer\Printer;
67
use PHPStan\Rules\Rule;
78
use PHPStan\Rules\RuleLevelHelper;
@@ -17,7 +18,7 @@ class InvalidPartOfEncapsedStringRuleTest extends RuleTestCase
1718
protected function getRule(): Rule
1819
{
1920
return new InvalidPartOfEncapsedStringRule(
20-
new Printer(),
21+
new ExprPrinter(new Printer()),
2122
new RuleLevelHelper($this->createReflectionProvider(), true, false, true, false),
2223
);
2324
}

tests/PHPStan/Rules/DeadCode/NoopRuleTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PHPStan\Rules\DeadCode;
44

5+
use PHPStan\Node\Printer\ExprPrinter;
56
use PHPStan\Node\Printer\Printer;
67
use PHPStan\Rules\Rule;
78
use PHPStan\Testing\RuleTestCase;
@@ -14,7 +15,7 @@ class NoopRuleTest extends RuleTestCase
1415

1516
protected function getRule(): Rule
1617
{
17-
return new NoopRule(new Printer());
18+
return new NoopRule(new ExprPrinter(new Printer()));
1819
}
1920

2021
public function testRule(): void

tests/PHPStan/Rules/Operators/InvalidBinaryOperationRuleTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PHPStan\Rules\Operators;
44

5+
use PHPStan\Node\Printer\ExprPrinter;
56
use PHPStan\Node\Printer\Printer;
67
use PHPStan\Rules\Rule;
78
use PHPStan\Rules\RuleLevelHelper;
@@ -17,7 +18,7 @@ class InvalidBinaryOperationRuleTest extends RuleTestCase
1718
protected function getRule(): Rule
1819
{
1920
return new InvalidBinaryOperationRule(
20-
new Printer(),
21+
new ExprPrinter(new Printer()),
2122
new RuleLevelHelper($this->createReflectionProvider(), true, false, true, false),
2223
);
2324
}

0 commit comments

Comments
 (0)