From e6453a2b13bf2a338ece010b7b0c6e7614c9c9f7 Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Tue, 18 May 2021 00:23:01 +0100 Subject: [PATCH] code improvements --- .../Printer/PhpDocInfoPrinter.php | 19 +++++++++---------- ...ayKeysAndInArrayToArrayKeyExistsRector.php | 2 +- .../New_/DowngradeAnonymousClassRector.php | 4 ++-- .../DowngradeGeneratedScalarTypesRector.php | 4 ++-- ...SetCookieOptionsArrayToArgumentsRector.php | 5 +---- ...DefaultValueForUndefinedVariableRector.php | 2 +- .../Rector/Name/ReservedObjectRector.php | 2 +- .../PseudoNamespaceToNamespaceRector.php | 7 ++----- .../DeprecationWarningCompilerPass.php | 6 +++--- .../ClassMethodAssignManipulator.php | 2 +- 10 files changed, 23 insertions(+), 30 deletions(-) diff --git a/packages/BetterPhpDocParser/Printer/PhpDocInfoPrinter.php b/packages/BetterPhpDocParser/Printer/PhpDocInfoPrinter.php index 4ef4b56c14d..d208b4f7258 100644 --- a/packages/BetterPhpDocParser/Printer/PhpDocInfoPrinter.php +++ b/packages/BetterPhpDocParser/Printer/PhpDocInfoPrinter.php @@ -63,17 +63,15 @@ final class PhpDocInfoPrinter */ private const TAG_AND_SPACE_REGEX = '#(@.*?) \(#'; - private ?int $tokenCount = null; + private int $tokenCount = 0; - private ?int $currentTokenPosition = null; + private int $currentTokenPosition = 0; /** * @var mixed[] */ private array $tokens = []; - private ?PhpDocNode $phpDocNode = null; - private ?PhpDocInfo $phpDocInfo = null; private PhpDocNodeTraverser $changedPhpDocNodeTraverser; @@ -119,7 +117,7 @@ public function printFormatPreserving(PhpDocInfo $phpDocInfo): string return (string) $phpDocInfo->getPhpDocNode(); } - $this->phpDocNode = $phpDocInfo->getPhpDocNode(); + $phpDocNode = $phpDocInfo->getPhpDocNode(); $this->tokens = $phpDocInfo->getTokens(); $this->tokenCount = $phpDocInfo->getTokenCount(); @@ -127,7 +125,7 @@ public function printFormatPreserving(PhpDocInfo $phpDocInfo): string $this->currentTokenPosition = 0; - $phpDocString = $this->printPhpDocNode($this->phpDocNode); + $phpDocString = $this->printPhpDocNode($phpDocNode); // hotfix of extra space with callable () return Strings::replace($phpDocString, self::CALLABLE_REGEX, 'callable('); @@ -237,9 +235,9 @@ private function printDocChildNode( private function printEnd(string $output): string { - $lastTokenPosition = $this->phpDocNode->getAttribute( - PhpDocAttributeKey::LAST_PHP_DOC_TOKEN_POSITION - ) ?: $this->currentTokenPosition; + $lastTokenPosition = $this->getCurrentPhpDocInfo() + ->getPhpDocNode() + ->getAttribute(PhpDocAttributeKey::LAST_PHP_DOC_TOKEN_POSITION) ?: $this->currentTokenPosition; if ($lastTokenPosition === 0) { $lastTokenPosition = 1; } @@ -255,7 +253,8 @@ private function addTokensFromTo(string $output, int $from, int $to, bool $shoul $removedStartAndEnds = $this->removeNodesStartAndEndResolver->resolve( $this->getCurrentPhpDocInfo() ->getOriginalPhpDocNode(), - $this->phpDocNode, + $this->getCurrentPhpDocInfo() + ->getPhpDocNode(), $this->tokens ); diff --git a/rules/CodeQuality/Rector/FuncCall/ArrayKeysAndInArrayToArrayKeyExistsRector.php b/rules/CodeQuality/Rector/FuncCall/ArrayKeysAndInArrayToArrayKeyExistsRector.php index f5a41bbdacc..eb3abb783bc 100644 --- a/rules/CodeQuality/Rector/FuncCall/ArrayKeysAndInArrayToArrayKeyExistsRector.php +++ b/rules/CodeQuality/Rector/FuncCall/ArrayKeysAndInArrayToArrayKeyExistsRector.php @@ -78,7 +78,7 @@ public function refactor(Node $node): ?Node } if (! $node instanceof Assign) { - return ! (bool) $this->betterNodeFinder->find($node, function (Node $n) use ($arrayVariable) { + return ! (bool) $this->betterNodeFinder->find($node, function (Node $n) use ($arrayVariable): bool { return $this->nodeComparator->areNodesEqual($arrayVariable, $n); }); } diff --git a/rules/DowngradePhp70/Rector/New_/DowngradeAnonymousClassRector.php b/rules/DowngradePhp70/Rector/New_/DowngradeAnonymousClassRector.php index eb0ccb6c817..4c7b371fbc6 100644 --- a/rules/DowngradePhp70/Rector/New_/DowngradeAnonymousClassRector.php +++ b/rules/DowngradePhp70/Rector/New_/DowngradeAnonymousClassRector.php @@ -127,8 +127,8 @@ public function refactor(Node $node): ?Node private function createAnonymousClassName(): string { - $fileInfo = $this->file->getSmartFileInfo(); + $smartFileInfo = $this->file->getSmartFileInfo(); - return self::ANONYMOUS_CLASS_PREFIX . md5($fileInfo->getRealPath()) . '__' . count($this->classes); + return self::ANONYMOUS_CLASS_PREFIX . md5($smartFileInfo->getRealPath()) . '__' . count($this->classes); } } diff --git a/rules/DowngradePhp70/Rector/String_/DowngradeGeneratedScalarTypesRector.php b/rules/DowngradePhp70/Rector/String_/DowngradeGeneratedScalarTypesRector.php index 8a7d39dd7d7..198f242b578 100644 --- a/rules/DowngradePhp70/Rector/String_/DowngradeGeneratedScalarTypesRector.php +++ b/rules/DowngradePhp70/Rector/String_/DowngradeGeneratedScalarTypesRector.php @@ -96,10 +96,10 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - $fileInfo = $this->file->getSmartFileInfo(); + $smartFileInfo = $this->file->getSmartFileInfo(); // this rule is parsing strings, so it heavy on performance; to lower it, we'll process only known opt-in files - if (! $this->isRelevantFileInfo($fileInfo)) { + if (! $this->isRelevantFileInfo($smartFileInfo)) { return null; } diff --git a/rules/DowngradePhp73/Rector/FuncCall/SetCookieOptionsArrayToArgumentsRector.php b/rules/DowngradePhp73/Rector/FuncCall/SetCookieOptionsArrayToArgumentsRector.php index dcdbbfada47..b73b1a7558f 100644 --- a/rules/DowngradePhp73/Rector/FuncCall/SetCookieOptionsArrayToArgumentsRector.php +++ b/rules/DowngradePhp73/Rector/FuncCall/SetCookieOptionsArrayToArgumentsRector.php @@ -45,10 +45,7 @@ final class SetCookieOptionsArrayToArgumentsRector extends AbstractRector 6 => false, ]; - /** - * @var int - */ - private $highestIndex = 1; + private int $highestIndex = 1; public function getRuleDefinition(): RuleDefinition { diff --git a/rules/Php56/Rector/FunctionLike/AddDefaultValueForUndefinedVariableRector.php b/rules/Php56/Rector/FunctionLike/AddDefaultValueForUndefinedVariableRector.php index d83c2ecf9ff..f82b1394743 100644 --- a/rules/Php56/Rector/FunctionLike/AddDefaultValueForUndefinedVariableRector.php +++ b/rules/Php56/Rector/FunctionLike/AddDefaultValueForUndefinedVariableRector.php @@ -42,7 +42,7 @@ final class AddDefaultValueForUndefinedVariableRector extends AbstractRector /** * @var string[] */ - private $definedVariables = []; + private array $definedVariables = []; public function getRuleDefinition(): RuleDefinition { diff --git a/rules/Php71/Rector/Name/ReservedObjectRector.php b/rules/Php71/Rector/Name/ReservedObjectRector.php index 1e0dea43ccb..fb2f1f17c26 100644 --- a/rules/Php71/Rector/Name/ReservedObjectRector.php +++ b/rules/Php71/Rector/Name/ReservedObjectRector.php @@ -29,7 +29,7 @@ final class ReservedObjectRector extends AbstractRector implements ConfigurableR /** * @var array */ - private $reservedKeywordsToReplacements = []; + private array $reservedKeywordsToReplacements = []; public function getRuleDefinition(): RuleDefinition { diff --git a/rules/Renaming/Rector/FileWithoutNamespace/PseudoNamespaceToNamespaceRector.php b/rules/Renaming/Rector/FileWithoutNamespace/PseudoNamespaceToNamespaceRector.php index 4e75a410441..4fe98c6631b 100644 --- a/rules/Renaming/Rector/FileWithoutNamespace/PseudoNamespaceToNamespaceRector.php +++ b/rules/Renaming/Rector/FileWithoutNamespace/PseudoNamespaceToNamespaceRector.php @@ -43,12 +43,9 @@ final class PseudoNamespaceToNamespaceRector extends AbstractRector implements C /** * @var PseudoNamespaceToNamespace[] */ - private $pseudoNamespacesToNamespaces = []; + private array $pseudoNamespacesToNamespaces = []; - /** - * @var string|null - */ - private $newNamespace; + private ?string $newNamespace = null; public function __construct( private PhpDocTypeRenamer $phpDocTypeRenamer diff --git a/src/DependencyInjection/CompilerPass/DeprecationWarningCompilerPass.php b/src/DependencyInjection/CompilerPass/DeprecationWarningCompilerPass.php index 40b7546ddfa..f90f02733d4 100644 --- a/src/DependencyInjection/CompilerPass/DeprecationWarningCompilerPass.php +++ b/src/DependencyInjection/CompilerPass/DeprecationWarningCompilerPass.php @@ -19,14 +19,14 @@ final class DeprecationWarningCompilerPass implements CompilerPassInterface public function process(ContainerBuilder $containerBuilder): void { - $parametersBag = $containerBuilder->getParameterBag(); + $parameterBag = $containerBuilder->getParameterBag(); foreach (self::DEPRECATED_PARAMETERS as $parameter => $message) { - if (! $parametersBag->has($parameter)) { + if (! $parameterBag->has($parameter)) { continue; } - $setsParameters = $parametersBag->get($parameter); + $setsParameters = $parameterBag->get($parameter); if ($setsParameters === []) { continue; } diff --git a/src/NodeManipulator/ClassMethodAssignManipulator.php b/src/NodeManipulator/ClassMethodAssignManipulator.php index c06661bbb6d..869b133014b 100644 --- a/src/NodeManipulator/ClassMethodAssignManipulator.php +++ b/src/NodeManipulator/ClassMethodAssignManipulator.php @@ -36,7 +36,7 @@ final class ClassMethodAssignManipulator /** * @var array */ - private $alreadyAddedClassMethodNames = []; + private array $alreadyAddedClassMethodNames = []; public function __construct( private BetterNodeFinder $betterNodeFinder,