Skip to content

Commit

Permalink
Optimization of printing expressions using ExprPrinter
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jun 9, 2022
1 parent ee386a2 commit e12524e
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 19 deletions.
3 changes: 3 additions & 0 deletions conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,9 @@ services:
-
class: PhpParser\NodeVisitor\NodeConnectingVisitor

-
class: PHPStan\Node\Printer\ExprPrinter

-
class: PHPStan\Node\Printer\Printer

Expand Down
6 changes: 3 additions & 3 deletions src/Analyser/DirectScopeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace PHPStan\Analyser;

use PhpParser\PrettyPrinter\Standard;
use PHPStan\DependencyInjection\Type\DynamicReturnTypeExtensionRegistryProvider;
use PHPStan\Node\Printer\ExprPrinter;
use PHPStan\Parser\Parser;
use PHPStan\Php\PhpVersion;
use PHPStan\Reflection\FunctionReflection;
Expand All @@ -30,7 +30,7 @@ public function __construct(
private ReflectionProvider $reflectionProvider,
private InitializerExprTypeResolver $initializerExprTypeResolver,
private DynamicReturnTypeExtensionRegistryProvider $dynamicReturnTypeExtensionRegistryProvider,
private Standard $printer,
private ExprPrinter $exprPrinter,
private TypeSpecifier $typeSpecifier,
private PropertyReflectionFinder $propertyReflectionFinder,
private Parser $parser,
Expand Down Expand Up @@ -84,7 +84,7 @@ public function create(
$this->reflectionProvider,
$this->initializerExprTypeResolver,
$this->dynamicReturnTypeExtensionRegistryProvider->getRegistry(),
$this->printer,
$this->exprPrinter,
$this->typeSpecifier,
$this->propertyReflectionFinder,
$this->parser,
Expand Down
4 changes: 2 additions & 2 deletions src/Analyser/LazyScopeFactory.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\DependencyInjection\Container;
use PHPStan\DependencyInjection\Type\DynamicReturnTypeExtensionRegistryProvider;
use PHPStan\Node\Printer\ExprPrinter;
use PHPStan\Php\PhpVersion;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\InitializerExprTypeResolver;
Expand Down Expand Up @@ -76,7 +76,7 @@ public function create(
$this->container->getByType(ReflectionProvider::class),
$this->container->getByType(InitializerExprTypeResolver::class),
$this->container->getByType(DynamicReturnTypeExtensionRegistryProvider::class)->getRegistry(),
$this->container->getByType(Standard::class),
$this->container->getByType(ExprPrinter::class),
$this->container->getByType(TypeSpecifier::class),
$this->container->getByType(PropertyReflectionFinder::class),
$this->container->getService('currentPhpVersionSimpleParser'),
Expand Down
17 changes: 5 additions & 12 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Scalar\String_;
use PhpParser\NodeFinder;
use PhpParser\PrettyPrinter\Standard;
use PHPStan\Node\ExecutionEndNode;
use PHPStan\Node\Expr\GetIterableKeyTypeExpr;
use PHPStan\Node\Expr\GetIterableValueTypeExpr;
use PHPStan\Node\Expr\GetOffsetValueTypeExpr;
use PHPStan\Node\Expr\OriginalPropertyTypeExpr;
use PHPStan\Node\Expr\SetOffsetValueTypeExpr;
use PHPStan\Node\Expr\TypeExpr;
use PHPStan\Node\Printer\ExprPrinter;
use PHPStan\Parser\Parser;
use PHPStan\Parser\ParserErrorsException;
use PHPStan\Php\PhpVersion;
Expand Down Expand Up @@ -159,7 +159,7 @@ public function __construct(
private ReflectionProvider $reflectionProvider,
private InitializerExprTypeResolver $initializerExprTypeResolver,
private DynamicReturnTypeExtensionRegistry $dynamicReturnTypeExtensionRegistry,
private Standard $printer,
private ExprPrinter $exprPrinter,
private TypeSpecifier $typeSpecifier,
private PropertyReflectionFinder $propertyReflectionFinder,
private Parser $parser,
Expand Down Expand Up @@ -645,14 +645,7 @@ public function getType(Expr $node): Type

private function getNodeKey(Expr $node): string
{
/** @var string|null $key */
$key = $node->getAttribute('phpstan_cache_printer');
if ($key === null) {
$key = $this->printer->prettyPrintExpr($node);
$node->setAttribute('phpstan_cache_printer', $key);
}

return $key;
return $this->exprPrinter->printExpr($node);
}

private function resolveType(Expr $node): Type
Expand Down Expand Up @@ -2114,7 +2107,7 @@ public function doNotTreatPhpDocTypesAsCertain(): Scope
$this->reflectionProvider,
$this->initializerExprTypeResolver,
$this->dynamicReturnTypeExtensionRegistry,
$this->printer,
$this->exprPrinter,
$this->typeSpecifier,
$this->propertyReflectionFinder,
$this->parser,
Expand Down Expand Up @@ -3284,7 +3277,7 @@ public function assignVariable(string $variableName, Type $type, ?TrinaryLogic $
$nativeTypes = $this->nativeExpressionTypes;
$nativeTypes[sprintf('$%s', $variableName)] = $type;

$variableString = $this->printer->prettyPrintExpr(new Variable($variableName));
$variableString = $this->exprPrinter->printExpr(new Variable($variableName));
$moreSpecificTypeHolders = $this->moreSpecificTypes;
foreach (array_keys($moreSpecificTypeHolders) as $key) {
$matches = Strings::matchAll((string) $key, '#\$[a-zA-Z_\x7f-\xff][a-zA-Z_0-9\x7f-\xff]*#');
Expand Down
26 changes: 26 additions & 0 deletions src/Node/Printer/ExprPrinter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php declare(strict_types = 1);

namespace PHPStan\Node\Printer;

use PhpParser\Node\Expr;

class ExprPrinter
{

public function __construct(private Printer $printer)
{
}

public function printExpr(Expr $expr): string
{
/** @var string|null $exprString */
$exprString = $expr->getAttribute('phpstan_cache_printer');
if ($exprString === null) {
$exprString = $this->printer->prettyPrintExpr($expr);
$expr->setAttribute('phpstan_cache_printer', $exprString);
}

return $exprString;
}

}
4 changes: 2 additions & 2 deletions src/Testing/PHPStanTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
use PHPStan\DependencyInjection\Type\DynamicReturnTypeExtensionRegistryProvider;
use PHPStan\DependencyInjection\Type\OperatorTypeSpecifyingExtensionRegistryProvider;
use PHPStan\File\FileHelper;
use PHPStan\Node\Printer\Printer;
use PHPStan\Node\Printer\ExprPrinter;
use PHPStan\Parser\Parser;
use PHPStan\Php\PhpVersion;
use PHPStan\PhpDoc\TypeNodeResolver;
Expand Down Expand Up @@ -167,7 +167,7 @@ public function createScopeFactory(ReflectionProvider $reflectionProvider, TypeS
$reflectionProvider,
new InitializerExprTypeResolver($constantResolver, $reflectionProviderProvider, new PhpVersion(PHP_VERSION_ID), $container->getByType(OperatorTypeSpecifyingExtensionRegistryProvider::class)),
$container->getByType(DynamicReturnTypeExtensionRegistryProvider::class),
new Printer(),
$container->getByType(ExprPrinter::class),
$typeSpecifier,
new PropertyReflectionFinder(),
$this->getParser(),
Expand Down

0 comments on commit e12524e

Please sign in to comment.