Skip to content

Commit

Permalink
code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed May 17, 2021
1 parent e394cab commit e6453a2
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 30 deletions.
19 changes: 9 additions & 10 deletions packages/BetterPhpDocParser/Printer/PhpDocInfoPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -119,15 +117,15 @@ 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();
$this->phpDocInfo = $phpDocInfo;

$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(');
Expand Down Expand Up @@ -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;
}
Expand All @@ -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
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ final class SetCookieOptionsArrayToArgumentsRector extends AbstractRector
6 => false,
];

/**
* @var int
*/
private $highestIndex = 1;
private int $highestIndex = 1;

public function getRuleDefinition(): RuleDefinition
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ final class AddDefaultValueForUndefinedVariableRector extends AbstractRector
/**
* @var string[]
*/
private $definedVariables = [];
private array $definedVariables = [];

public function getRuleDefinition(): RuleDefinition
{
Expand Down
2 changes: 1 addition & 1 deletion rules/Php71/Rector/Name/ReservedObjectRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ final class ReservedObjectRector extends AbstractRector implements ConfigurableR
/**
* @var array<string, string>
*/
private $reservedKeywordsToReplacements = [];
private array $reservedKeywordsToReplacements = [];

public function getRuleDefinition(): RuleDefinition
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/NodeManipulator/ClassMethodAssignManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ final class ClassMethodAssignManipulator
/**
* @var array<string, string[]>
*/
private $alreadyAddedClassMethodNames = [];
private array $alreadyAddedClassMethodNames = [];

public function __construct(
private BetterNodeFinder $betterNodeFinder,
Expand Down

0 comments on commit e6453a2

Please sign in to comment.