Skip to content

Commit

Permalink
[cs] re-order private methods by call order (#2220)
Browse files Browse the repository at this point in the history
[cs] re-order private methods by call order
  • Loading branch information
TomasVotruba authored Oct 30, 2019
2 parents 807f692 + 7850a0d commit c2aadb9
Show file tree
Hide file tree
Showing 115 changed files with 3,538 additions and 3,567 deletions.
35 changes: 3 additions & 32 deletions ecs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,6 @@ services:
- 'getNodeTypes'
- 'refactor'

Symplify\CodingStandard\Sniffs\DependencyInjection\NoClassInstantiationSniff:
extra_allowed_classes:
- 'PHPStan\Type\*'
- '*Type'
- 'PHPStan\Analyser\Scope'
- 'PhpParser\NodeVisitor\NameResolver'
- 'PhpParser\Node\*'
- '*Data'
- '*Recipe'
- '*ValueObject'
- 'PhpParser\Comment'
- 'PhpParser\Lexer'
- 'PhpParser\Comment\Doc'
- 'PhpParser\NodeTraverser'
- 'Rector\Reporting\FileDiff'
- 'Rector\RectorDefinition\*'
- 'Rector\Application\Error'
- 'Rector\DependencyInjection\Loader\*'
- 'Symplify\PackageBuilder\*'
- 'Symfony\Component\Console\Input\*Input'
- 'PHPStan\Analyser\NameScope'
- 'PHPStan\Rules\RuleErrors\RuleError*'
- '*\XdebugHandler'

Symplify\CodingStandard\Fixer\Naming\PropertyNameMatchingTypeFixer:
extra_skipped_classes:
- 'PhpParser\PrettyPrinter\Standard'
Expand All @@ -71,6 +47,9 @@ parameters:
- 'src/Rector/AbstractRector.php'

skip:
# rather useless
Symplify\CodingStandard\Sniffs\DependencyInjection\NoClassInstantiationSniff: ~

PHP_CodeSniffer\Standards\PSR2\Sniffs\Methods\MethodDeclarationSniff.Underscore: ~
Symplify\CodingStandard\Sniffs\Architecture\DuplicatedClassShortNameSniff: ~
# skip temporary due to missing "import" feature in PhpStorm
Expand Down Expand Up @@ -184,20 +163,12 @@ parameters:
- 'packages/DeadCode/src/Rector/ClassMethod/RemoveOverriddenValuesRector.php'
- 'packages/PhpSpecToPHPUnit/src/Rector/MethodCall/PhpSpecPromisesToPHPUnitAssertRector.php'

Symplify\CodingStandard\Sniffs\DependencyInjection\NoClassInstantiationSniff:
# 3rd party api
- 'src/PhpParser/Node/Value/ValueResolver.php'

PhpCsFixer\Fixer\PhpUnit\PhpUnitStrictFixer:
- 'packages/BetterPhpDocParser/tests/PhpDocInfo/PhpDocInfo/PhpDocInfoTest.php'
# intentional "assertEquals()"
- 'tests/PhpParser/Node/NodeFactoryTest.php'
- '*TypeResolverTest.php'

Symplify\CodingStandard\Fixer\LineLength\LineLengthFixer:
# buggy with PHP heredoc
- 'packages/SOLID/src/Rector/ClassConst/PrivatizeLocalClassConstantRector.php'

Symplify\CodingStandard\Sniffs\Commenting\AnnotationTypeExistsSniff:
- '*PhpDocNodeFactory.php'
- '*AnnotationReader.php'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,48 @@ public function refactor(Node $node): ?Node
return $node;
}

private function reset(): void
{
$this->propertyFetchToParams = [];
$this->propertyFetchToParamsToRemoveFromConstructor = [];
}

private function collectPropertyFetchToParams(ClassMethod $classMethod): void
{
foreach ((array) $classMethod->stmts as $constructorStmt) {
$propertyToVariable = $this->resolveAssignPropertyToVariableOrNull($constructorStmt);
if ($propertyToVariable === null) {
continue;
}

[$propertyFetchName, $variableName] = $propertyToVariable;

$param = $this->classManipulator->findMethodParamByName($classMethod, $variableName);
if ($param === null) {
continue;
}

// random type, we cannot autowire in action
if ($param->type === null) {
continue;
}

$paramType = $this->getName($param->type);
if ($paramType === null) {
continue;
}

if ($this->typeAnalyzer->isPhpReservedType($paramType)) {
continue;
}

// it's a match
$this->propertyFetchToParams[$propertyFetchName] = $param;
}

$this->propertyFetchToParamsToRemoveFromConstructor = $this->propertyFetchToParams;
}

private function changePropertyUsageToParameter(ClassMethod $classMethod, string $propertyName, Param $param): void
{
$currentlyAddedLocalVariables = [];
Expand Down Expand Up @@ -209,46 +251,15 @@ private function changePropertyUsageToParameter(ClassMethod $classMethod, string
}
}

private function collectPropertyFetchToParams(ClassMethod $classMethod): void
private function removeUnusedPropertiesAndConstructorParams(Class_ $class, ClassMethod $classMethod): void
{
foreach ((array) $classMethod->stmts as $constructorStmt) {
$propertyToVariable = $this->resolveAssignPropertyToVariableOrNull($constructorStmt);
if ($propertyToVariable === null) {
continue;
}

[$propertyFetchName, $variableName] = $propertyToVariable;

$param = $this->classManipulator->findMethodParamByName($classMethod, $variableName);
if ($param === null) {
continue;
}

// random type, we cannot autowire in action
if ($param->type === null) {
continue;
}

$paramType = $this->getName($param->type);
if ($paramType === null) {
continue;
}

if ($this->typeAnalyzer->isPhpReservedType($paramType)) {
continue;
}

// it's a match
$this->propertyFetchToParams[$propertyFetchName] = $param;
$this->removeAssignsFromConstructor($classMethod);
foreach ($this->propertyFetchToParamsToRemoveFromConstructor as $propertyFetchName => $param) {
$this->changePropertyUsageToParameter($classMethod, $propertyFetchName, $param);
}

$this->propertyFetchToParamsToRemoveFromConstructor = $this->propertyFetchToParams;
}

private function reset(): void
{
$this->propertyFetchToParams = [];
$this->propertyFetchToParamsToRemoveFromConstructor = [];
$this->classMethodManipulator->removeUnusedParameters($classMethod);
$this->removeUnusedProperties($class);
$this->removeConstructIfEmpty($class, $classMethod);
}

/**
Expand Down Expand Up @@ -286,17 +297,6 @@ private function resolveAssignPropertyToVariableOrNull(Node $node): ?array
return [$propertyFetchName, $variableName];
}

private function removeUnusedPropertiesAndConstructorParams(Class_ $class, ClassMethod $classMethod): void
{
$this->removeAssignsFromConstructor($classMethod);
foreach ($this->propertyFetchToParamsToRemoveFromConstructor as $propertyFetchName => $param) {
$this->changePropertyUsageToParameter($classMethod, $propertyFetchName, $param);
}
$this->classMethodManipulator->removeUnusedParameters($classMethod);
$this->removeUnusedProperties($class);
$this->removeConstructIfEmpty($class, $classMethod);
}

private function removeAssignsFromConstructor(ClassMethod $classMethod): void
{
foreach ((array) $classMethod->stmts as $key => $constructorStmt) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ public function readPropertyAnnotation(Property $property, string $annotationCla
return $propertyAnnotation;
}

private function createClassReflectionFromNode(Class_ $class): ReflectionClass
{
/** @var string $className */
$className = $this->nameResolver->getName($class);

return new ReflectionClass($className);
}

private function createPropertyReflectionFromPropertyNode(Property $property): ?ReflectionProperty
{
/** @var string $propertyName */
Expand All @@ -107,12 +115,4 @@ private function createPropertyReflectionFromPropertyNode(Property $property): ?
return null;
}
}

private function createClassReflectionFromNode(Class_ $class): ReflectionClass
{
/** @var string $className */
$className = $this->nameResolver->getName($class);

return new ReflectionClass($className);
}
}
22 changes: 11 additions & 11 deletions packages/BetterPhpDocParser/src/PhpDocInfo/PhpDocInfoFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,17 @@ public function createFromNode(Node $node): PhpDocInfo
return $phpDocInfo;
}

private function createUniqueDocNodeHash(Node $node): string
{
$this->ensureNodeHasDocComment($node);

$objectHash = spl_object_hash($node);
$docCommentHash = spl_object_hash($node->getDocComment());
$docCommentContentHash = sha1($node->getDocComment()->getText());

return $objectHash . $docCommentHash . $docCommentContentHash;
}

/**
* Needed for printing
*/
Expand All @@ -101,17 +112,6 @@ private function setPositionOfLastToken(
return $attributeAwarePhpDocNode;
}

private function createUniqueDocNodeHash(Node $node): string
{
$this->ensureNodeHasDocComment($node);

$objectHash = spl_object_hash($node);
$docCommentHash = spl_object_hash($node->getDocComment());
$docCommentContentHash = sha1($node->getDocComment()->getText());

return $objectHash . $docCommentHash . $docCommentContentHash;
}

private function ensureNodeHasDocComment(Node $node): void
{
if ($node->getDocComment() !== null) {
Expand Down
88 changes: 44 additions & 44 deletions packages/BetterPhpDocParser/src/PhpDocParser/BetterPhpDocParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,38 +206,6 @@ private function parseChildAndStoreItsPositions(TokenIterator $tokenIterator): N
return $attributeAwareNode;
}

private function getOriginalContentFromTokenIterator(TokenIterator $tokenIterator): string
{
// @todo iterate through tokens...
$originalTokens = $this->privatesAccessor->getPrivateProperty($tokenIterator, 'tokens');
$originalContent = '';

foreach ($originalTokens as $originalToken) {
// skip opening
if ($originalToken[1] === Lexer::TOKEN_OPEN_PHPDOC) {
continue;
}

// skip closing
if ($originalToken[1] === Lexer::TOKEN_CLOSE_PHPDOC) {
continue;
}

if ($originalToken[1] === Lexer::TOKEN_PHPDOC_EOL) {
$originalToken[0] = PHP_EOL;
}

$originalContent .= $originalToken[0];
}

return trim($originalContent);
}

private function getTokenIteratorIndex(TokenIterator $tokenIterator): int
{
return (int) $this->privatesAccessor->getPrivateProperty($tokenIterator, 'index');
}

private function resolveTag(TokenIterator $tokenIterator): string
{
$tag = $tokenIterator->currentTokenValue();
Expand Down Expand Up @@ -268,18 +236,6 @@ private function resolveTag(TokenIterator $tokenIterator): string
return $tag;
}

private function isTagMatchedByFactories(string $tag): bool
{
$currentPhpNode = $this->currentNodeProvider->getNode();
foreach ($this->phpDocNodeFactories as $phpDocNodeFactory) {
if ($this->isTagMatchingPhpDocNodeFactory($tag, $phpDocNodeFactory, $currentPhpNode)) {
return true;
}
}

return false;
}

private function isTagMatchingPhpDocNodeFactory(
string $tag,
PhpDocNodeFactoryInterface $phpDocNodeFactory,
Expand Down Expand Up @@ -307,6 +263,11 @@ private function isTagMatchingPhpDocNodeFactory(
return false;
}

private function getTokenIteratorIndex(TokenIterator $tokenIterator): int
{
return (int) $this->privatesAccessor->getPrivateProperty($tokenIterator, 'index');
}

/**
* @see https://github.com/rectorphp/rector/issues/2158
*
Expand All @@ -332,4 +293,43 @@ private function adjustTokenEndToFitClassAnnotation(TokenIterator $tokenIterator

return $tokenEnd;
}

private function getOriginalContentFromTokenIterator(TokenIterator $tokenIterator): string
{
// @todo iterate through tokens...
$originalTokens = $this->privatesAccessor->getPrivateProperty($tokenIterator, 'tokens');
$originalContent = '';

foreach ($originalTokens as $originalToken) {
// skip opening
if ($originalToken[1] === Lexer::TOKEN_OPEN_PHPDOC) {
continue;
}

// skip closing
if ($originalToken[1] === Lexer::TOKEN_CLOSE_PHPDOC) {
continue;
}

if ($originalToken[1] === Lexer::TOKEN_PHPDOC_EOL) {
$originalToken[0] = PHP_EOL;
}

$originalContent .= $originalToken[0];
}

return trim($originalContent);
}

private function isTagMatchedByFactories(string $tag): bool
{
$currentPhpNode = $this->currentNodeProvider->getNode();
foreach ($this->phpDocNodeFactories as $phpDocNodeFactory) {
if ($this->isTagMatchingPhpDocNodeFactory($tag, $phpDocNodeFactory, $currentPhpNode)) {
return true;
}
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,6 @@ public function isTagMatchToNodeAndClass(string $tag, Node $node, string $matchi
return Strings::lower($fullyQualifiedClassNode) === Strings::lower($matchingClass);
}

private function isUseMatchingName(string $tag, UseUse $useUse): bool
{
$shortName = $useUse->alias ? $useUse->alias->name : $useUse->name->getLast();
$shortNamePattern = preg_quote($shortName, '#');

return (bool) Strings::match($tag, '#' . $shortNamePattern . '(\\\\[\w]+)?#i');
}

/**
* @param Use_[] $uses
*/
Expand All @@ -67,4 +59,12 @@ private function matchFullAnnotationClassWithUses(string $tag, array $uses): ?st

return null;
}

private function isUseMatchingName(string $tag, UseUse $useUse): bool
{
$shortName = $useUse->alias ? $useUse->alias->name : $useUse->name->getLast();
$shortNamePattern = preg_quote($shortName, '#');

return (bool) Strings::match($tag, '#' . $shortNamePattern . '(\\\\[\w]+)?#i');
}
}
Loading

0 comments on commit c2aadb9

Please sign in to comment.