Skip to content

Commit

Permalink
Use ExprPrinter in more places
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jun 9, 2022
1 parent e12524e commit fba2929
Show file tree
Hide file tree
Showing 12 changed files with 38 additions and 32 deletions.
16 changes: 8 additions & 8 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Name;
use PhpParser\PrettyPrinter\Standard;
use PHPStan\Node\Printer\ExprPrinter;
use PHPStan\Reflection\ParametersAcceptor;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Reflection\ReflectionProvider;
Expand Down Expand Up @@ -84,7 +84,7 @@ class TypeSpecifier
* @param StaticMethodTypeSpecifyingExtension[] $staticMethodTypeSpecifyingExtensions
*/
public function __construct(
private Standard $printer,
private ExprPrinter $exprPrinter,
private ReflectionProvider $reflectionProvider,
private array $functionTypeSpecifyingExtensions,
private array $methodTypeSpecifyingExtensions,
Expand Down Expand Up @@ -332,8 +332,8 @@ public function specifyTypesInCondition(
return $types;
}

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

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

$sureTypes = [];
$sureNotTypes = [];
$exprString = $this->printer->prettyPrintExpr($expr);
$exprString = $this->exprPrinter->printExpr($expr);
if ($context->false()) {
$sureNotTypes[$exprString] = [$expr, $type];
} elseif ($context->true()) {
Expand Down Expand Up @@ -1297,7 +1297,7 @@ private function createRangeTypes(?Expr $rootExpr, Expr $expr, Type $type, TypeS
$sureNotTypes = [];

if ($type instanceof IntegerRangeType || $type instanceof ConstantIntegerType) {
$exprString = $this->printer->prettyPrintExpr($expr);
$exprString = $this->exprPrinter->printExpr($expr);
if ($context->false()) {
$sureNotTypes[$exprString] = [$expr, $type];
} elseif ($context->true()) {
Expand Down
4 changes: 2 additions & 2 deletions src/Analyser/TypeSpecifierFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace PHPStan\Analyser;

use PhpParser\PrettyPrinter\Standard;
use PHPStan\Broker\BrokerFactory;
use PHPStan\DependencyInjection\Container;
use PHPStan\Node\Printer\ExprPrinter;
use PHPStan\Reflection\ReflectionProvider;
use function array_merge;

Expand All @@ -22,7 +22,7 @@ public function __construct(private Container $container)
public function create(): TypeSpecifier
{
$typeSpecifier = new TypeSpecifier(
$this->container->getByType(Standard::class),
$this->container->getByType(ExprPrinter::class),
$this->container->getByType(ReflectionProvider::class),
$this->container->getServicesByTag(self::FUNCTION_TYPE_SPECIFYING_EXTENSION_TAG),
$this->container->getServicesByTag(self::METHOD_TYPE_SPECIFYING_EXTENSION_TAG),
Expand Down
8 changes: 4 additions & 4 deletions src/Dependency/ExportedNodeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PhpParser\PrettyPrinter\Standard;
use PHPStan\Dependency\ExportedNode\ExportedClassConstantNode;
use PHPStan\Dependency\ExportedNode\ExportedClassConstantsNode;
use PHPStan\Dependency\ExportedNode\ExportedClassNode;
Expand All @@ -21,6 +20,7 @@
use PHPStan\Dependency\ExportedNode\ExportedPropertiesNode;
use PHPStan\Dependency\ExportedNode\ExportedTraitNode;
use PHPStan\Dependency\ExportedNode\ExportedTraitUseAdaptation;
use PHPStan\Node\Printer\ExprPrinter;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\FileTypeMapper;
use function array_map;
Expand All @@ -30,7 +30,7 @@
class ExportedNodeResolver
{

public function __construct(private FileTypeMapper $fileTypeMapper, private Standard $printer)
public function __construct(private FileTypeMapper $fileTypeMapper, private ExprPrinter $exprPrinter)
{
}

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

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

return new ExportedEnumCaseNode(
$node->name->toString(),
$node->expr !== null ? $this->printer->prettyPrintExpr($node->expr) : null,
$node->expr !== null ? $this->exprPrinter->printExpr($node->expr) : null,
$this->exportPhpDocNode(
$fileName,
$namespacedName,
Expand Down
6 changes: 3 additions & 3 deletions src/Rules/Arrays/DuplicateKeysInLiteralArraysRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace PHPStan\Rules\Arrays;

use PhpParser\Node;
use PhpParser\PrettyPrinter\Standard;
use PHPStan\Analyser\Scope;
use PHPStan\Node\LiteralArrayNode;
use PHPStan\Node\Printer\ExprPrinter;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\ConstantScalarType;
Expand All @@ -22,7 +22,7 @@ class DuplicateKeysInLiteralArraysRule implements Rule
{

public function __construct(
private Standard $printer,
private ExprPrinter $exprPrinter,
)
{
}
Expand Down Expand Up @@ -55,7 +55,7 @@ public function processNode(Node $node, Scope $scope): array
continue;
}

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

Expand Down
6 changes: 3 additions & 3 deletions src/Rules/Cast/InvalidPartOfEncapsedStringRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace PHPStan\Rules\Cast;

use PhpParser\Node;
use PhpParser\PrettyPrinter\Standard;
use PHPStan\Analyser\Scope;
use PHPStan\Node\Printer\ExprPrinter;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Rules\RuleLevelHelper;
Expand All @@ -20,7 +20,7 @@ class InvalidPartOfEncapsedStringRule implements Rule
{

public function __construct(
private Standard $printer,
private ExprPrinter $exprPrinter,
private RuleLevelHelper $ruleLevelHelper,
)
{
Expand Down Expand Up @@ -56,7 +56,7 @@ public function processNode(Node $node, Scope $scope): array
}
$messages[] = RuleErrorBuilder::message(sprintf(
'Part %s (%s) of encapsed string cannot be cast to string.',
$this->printer->prettyPrintExpr($part),
$this->exprPrinter->printExpr($part),
$partType->describe(VerbosityLevel::value()),
))->line($part->getLine())->build();
}
Expand Down
6 changes: 3 additions & 3 deletions src/Rules/DeadCode/NoopRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace PHPStan\Rules\DeadCode;

use PhpParser\Node;
use PhpParser\PrettyPrinter\Standard;
use PHPStan\Analyser\Scope;
use PHPStan\Node\Printer\ExprPrinter;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use function sprintf;
Expand All @@ -15,7 +15,7 @@
class NoopRule implements Rule
{

public function __construct(private Standard $printer)
public function __construct(private ExprPrinter $exprPrinter)
{
}

Expand Down Expand Up @@ -54,7 +54,7 @@ public function processNode(Node $node, Scope $scope): array
return [
RuleErrorBuilder::message(sprintf(
'Expression "%s" on a separate line does not do anything.',
$this->printer->prettyPrintExpr($originalExpr),
$this->exprPrinter->printExpr($originalExpr),
))->line($expr->getLine())
->identifier('deadCode.noopExpression')
->metadata([
Expand Down
8 changes: 5 additions & 3 deletions src/Rules/Operators/InvalidBinaryOperationRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace PHPStan\Rules\Operators;

use PhpParser\Node;
use PhpParser\PrettyPrinter\Standard;
use PHPStan\Analyser\MutatingScope;
use PHPStan\Analyser\Scope;
use PHPStan\Node\Printer\ExprPrinter;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Rules\RuleLevelHelper;
Expand All @@ -24,7 +24,7 @@ class InvalidBinaryOperationRule implements Rule
{

public function __construct(
private Standard $printer,
private ExprPrinter $exprPrinter,
private RuleLevelHelper $ruleLevelHelper,
)
{
Expand All @@ -51,12 +51,14 @@ public function processNode(Node $node, Scope $scope): array
$rightVariable = new Node\Expr\Variable($rightName);
if ($node instanceof Node\Expr\AssignOp) {
$newNode = clone $node;
$newNode->setAttribute('phpstan_cache_printer', null);
$left = $node->var;
$right = $node->expr;
$newNode->var = $leftVariable;
$newNode->expr = $rightVariable;
} else {
$newNode = clone $node;
$newNode->setAttribute('phpstan_cache_printer', null);
$left = $node->left;
$right = $node->right;
$newNode->left = $leftVariable;
Expand Down Expand Up @@ -104,7 +106,7 @@ public function processNode(Node $node, Scope $scope): array
return [
RuleErrorBuilder::message(sprintf(
'Binary operation "%s" between %s and %s results in an error.',
substr(substr($this->printer->prettyPrintExpr($newNode), strlen($leftName) + 2), 0, -(strlen($rightName) + 2)),
substr(substr($this->exprPrinter->printExpr($newNode), strlen($leftName) + 2), 0, -(strlen($rightName) + 2)),
$scope->getType($left)->describe(VerbosityLevel::value()),
$scope->getType($right)->describe(VerbosityLevel::value()),
))->line($left->getLine())->build(),
Expand Down
4 changes: 2 additions & 2 deletions tests/PHPStan/Analyser/AnalyserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PHPStan\Dependency\DependencyResolver;
use PHPStan\Dependency\ExportedNodeResolver;
use PHPStan\DependencyInjection\Type\DynamicThrowTypeExtensionProvider;
use PHPStan\Node\Printer\ExprPrinter;
use PHPStan\Node\Printer\Printer;
use PHPStan\Parser\RichParser;
use PHPStan\Php\PhpVersion;
Expand Down Expand Up @@ -463,7 +464,6 @@ private function createAnalyser(bool $reportUnmatchedIgnoredErrors): Analyser
]);

$reflectionProvider = $this->createReflectionProvider();
$printer = new Printer();
$fileHelper = $this->getFileHelper();

$typeSpecifier = self::getContainer()->getService('typeSpecifier');
Expand Down Expand Up @@ -499,7 +499,7 @@ private function createAnalyser(bool $reportUnmatchedIgnoredErrors): Analyser
new NameResolver(),
self::getContainer(),
),
new DependencyResolver($fileHelper, $reflectionProvider, new ExportedNodeResolver($fileTypeMapper, $printer)),
new DependencyResolver($fileHelper, $reflectionProvider, new ExportedNodeResolver($fileTypeMapper, new ExprPrinter(new Printer()))),
$reportUnmatchedIgnoredErrors,
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\Rules\Arrays;

use PHPStan\Node\Printer\ExprPrinter;
use PHPStan\Node\Printer\Printer;
use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
Expand All @@ -16,7 +17,7 @@ class DuplicateKeysInLiteralArraysRuleTest extends RuleTestCase
protected function getRule(): Rule
{
return new DuplicateKeysInLiteralArraysRule(
new Printer(),
new ExprPrinter(new Printer()),
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\Rules\Cast;

use PHPStan\Node\Printer\ExprPrinter;
use PHPStan\Node\Printer\Printer;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleLevelHelper;
Expand All @@ -17,7 +18,7 @@ class InvalidPartOfEncapsedStringRuleTest extends RuleTestCase
protected function getRule(): Rule
{
return new InvalidPartOfEncapsedStringRule(
new Printer(),
new ExprPrinter(new Printer()),
new RuleLevelHelper($this->createReflectionProvider(), true, false, true, false),
);
}
Expand Down
3 changes: 2 additions & 1 deletion tests/PHPStan/Rules/DeadCode/NoopRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\Rules\DeadCode;

use PHPStan\Node\Printer\ExprPrinter;
use PHPStan\Node\Printer\Printer;
use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
Expand All @@ -14,7 +15,7 @@ class NoopRuleTest extends RuleTestCase

protected function getRule(): Rule
{
return new NoopRule(new Printer());
return new NoopRule(new ExprPrinter(new Printer()));
}

public function testRule(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\Rules\Operators;

use PHPStan\Node\Printer\ExprPrinter;
use PHPStan\Node\Printer\Printer;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleLevelHelper;
Expand All @@ -17,7 +18,7 @@ class InvalidBinaryOperationRuleTest extends RuleTestCase
protected function getRule(): Rule
{
return new InvalidBinaryOperationRule(
new Printer(),
new ExprPrinter(new Printer()),
new RuleLevelHelper($this->createReflectionProvider(), true, false, true, false),
);
}
Expand Down

0 comments on commit fba2929

Please sign in to comment.