From a45eff72dfe81b010702ac3618e013c64636e5b5 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sat, 9 Feb 2019 20:08:08 +0100 Subject: [PATCH] apply code quality + add number of applied changes --- .../Rector/Foreach_/SimplifyForeachToCoalescingRector.php | 6 +----- .../src/Console/Formatter/DiffConsoleFormatter.php | 4 +--- packages/Php/src/EregToPcreTransformer.php | 8 +++----- .../Rector/FunctionLike/AbstractTypeDeclarationRector.php | 6 +----- .../Symfony/src/Bridge/SymfonyKernelParameterGuard.php | 3 +-- .../Symfony/src/Rector/HttpKernel/GetRequestRector.php | 6 +----- src/Application/RectorApplication.php | 8 +++----- src/Console/Command/ProcessCommand.php | 5 +++-- src/PhpParser/Node/Maintainer/CallMaintainer.php | 7 ++++++- src/PhpParser/Node/NodeFactory.php | 8 +++----- src/Rector/Function_/FunctionReplaceRector.php | 6 +----- src/Rector/Namespace_/NamespaceReplacerRector.php | 8 +++----- 12 files changed, 27 insertions(+), 48 deletions(-) diff --git a/packages/CodeQuality/src/Rector/Foreach_/SimplifyForeachToCoalescingRector.php b/packages/CodeQuality/src/Rector/Foreach_/SimplifyForeachToCoalescingRector.php index 07b5961ac47..29696bd5252 100644 --- a/packages/CodeQuality/src/Rector/Foreach_/SimplifyForeachToCoalescingRector.php +++ b/packages/CodeQuality/src/Rector/Foreach_/SimplifyForeachToCoalescingRector.php @@ -111,11 +111,7 @@ CODE_SAMPLE return null; } - if ($node->stmts[0] instanceof Expression) { - $innerNode = $node->stmts[0]->expr; - } else { - $innerNode = $node->stmts[0]; - } + $innerNode = $node->stmts[0] instanceof Expression ? $node->stmts[0]->expr : $node->stmts[0]; if ($innerNode instanceof Assign || $innerNode instanceof Return_) { return $innerNode; diff --git a/packages/ConsoleDiffer/src/Console/Formatter/DiffConsoleFormatter.php b/packages/ConsoleDiffer/src/Console/Formatter/DiffConsoleFormatter.php index e0cbae7d0f5..8d046a2ecd0 100644 --- a/packages/ConsoleDiffer/src/Console/Formatter/DiffConsoleFormatter.php +++ b/packages/ConsoleDiffer/src/Console/Formatter/DiffConsoleFormatter.php @@ -21,9 +21,7 @@ final class DiffConsoleFormatter public function __construct() { $this->template = sprintf( - ' ---------- begin diff ----------' . - '%s%%s%s' . - ' ----------- end diff -----------', + ' ---------- begin diff ----------%s%%s%s ----------- end diff -----------', PHP_EOL, PHP_EOL ); diff --git a/packages/Php/src/EregToPcreTransformer.php b/packages/Php/src/EregToPcreTransformer.php index 01de4941313..91453da7fd5 100644 --- a/packages/Php/src/EregToPcreTransformer.php +++ b/packages/Php/src/EregToPcreTransformer.php @@ -101,7 +101,7 @@ final class EregToPcreTransformer $i + 1 < $l && strpos('.=:', $s[$i + 1]) !== false) { $ii = strpos($s, ']', $i); if ($ii === false) { - throw new InvalidEregException('"[" does not have a matching ' . '"]"'); + throw new InvalidEregException('"[" does not have a matching "]"'); } $ccls = substr($s, $i + 1, $ii - ($i + 1)); $cclsmap = [ @@ -120,8 +120,7 @@ final class EregToPcreTransformer ]; if (! isset($cclsmap[$ccls])) { throw new InvalidEregException( - 'an invalid or unsupported ' . - 'character class [' . $ccls . ']' + 'an invalid or unsupported character class [' . $ccls . ']' ); } $cls .= $cclsmap[$ccls]; @@ -130,8 +129,7 @@ final class EregToPcreTransformer $a = $s[$i++]; if ($a === '-' && ! $start && ! ($i < $l && $s[$i] === ']')) { throw new InvalidEregException( - '"-" is invalid for the start ' . - 'character in the brackets' + '"-" is invalid for the start character in the brackets' ); } if ($i < $l && $s[$i] === '-') { diff --git a/packages/Php/src/Rector/FunctionLike/AbstractTypeDeclarationRector.php b/packages/Php/src/Rector/FunctionLike/AbstractTypeDeclarationRector.php index 7f553e92c7c..bf9590c93ae 100644 --- a/packages/Php/src/Rector/FunctionLike/AbstractTypeDeclarationRector.php +++ b/packages/Php/src/Rector/FunctionLike/AbstractTypeDeclarationRector.php @@ -181,11 +181,7 @@ abstract class AbstractTypeDeclarationRector extends AbstractRector Node $node, Node $childClassMethodOrParam ): ?Node { - if ($returnTypeInfo->getTypeNode() instanceof NullableType) { - $nakedType = $returnTypeInfo->getTypeNode()->type; - } else { - $nakedType = $returnTypeInfo->getTypeNode(); - } + $nakedType = $returnTypeInfo->getTypeNode() instanceof NullableType ? $returnTypeInfo->getTypeNode()->type : $returnTypeInfo->getTypeNode(); if ($nakedType === null) { return null; diff --git a/packages/Symfony/src/Bridge/SymfonyKernelParameterGuard.php b/packages/Symfony/src/Bridge/SymfonyKernelParameterGuard.php index b583885e6b3..711874a1838 100644 --- a/packages/Symfony/src/Bridge/SymfonyKernelParameterGuard.php +++ b/packages/Symfony/src/Bridge/SymfonyKernelParameterGuard.php @@ -29,8 +29,7 @@ final class SymfonyKernelParameterGuard } throw new InvalidConfigurationException(sprintf( - 'Kernel class "%s" provided in "parameters > %s" is not autoloadable. ' . - 'Make sure composer.json of your application is valid and rector is loading "vendor/autoload.php" of your application.', + 'Kernel class "%s" provided in "parameters > %s" is not autoloadable. Make sure composer.json of your application is valid and rector is loading "vendor/autoload.php" of your application.', $kernelClass, Option::KERNEL_CLASS_PARAMETER )); diff --git a/packages/Symfony/src/Rector/HttpKernel/GetRequestRector.php b/packages/Symfony/src/Rector/HttpKernel/GetRequestRector.php index d5a5f43e96f..997f48663d3 100644 --- a/packages/Symfony/src/Rector/HttpKernel/GetRequestRector.php +++ b/packages/Symfony/src/Rector/HttpKernel/GetRequestRector.php @@ -115,11 +115,7 @@ CODE_SAMPLE */ private function resolveUniqueName(Node $node, string $name): string { - if ($node instanceof ClassMethod) { - $candidates = $node->params; - } else { - $candidates = $node->args; - } + $candidates = $node instanceof ClassMethod ? $node->params : $node->args; $candidateNames = []; foreach ($candidates as $candidate) { diff --git a/src/Application/RectorApplication.php b/src/Application/RectorApplication.php index 0711ca310a2..54f03c4e5c6 100644 --- a/src/Application/RectorApplication.php +++ b/src/Application/RectorApplication.php @@ -148,11 +148,9 @@ final class RectorApplication } else { $oldContent = $fileInfo->getContents(); - if ($this->configuration->isDryRun()) { - $newContent = $this->fileProcessor->printToString($fileInfo); - } else { - $newContent = $this->fileProcessor->printToFile($fileInfo); - } + $newContent = $this->configuration->isDryRun() ? $this->fileProcessor->printToString( + $fileInfo + ) : $this->fileProcessor->printToFile($fileInfo); $this->errorAndDiffCollector->addFileDiff($fileInfo, $newContent, $oldContent); diff --git a/src/Console/Command/ProcessCommand.php b/src/Console/Command/ProcessCommand.php index 12ec84a1964..7772837d7ef 100644 --- a/src/Console/Command/ProcessCommand.php +++ b/src/Console/Command/ProcessCommand.php @@ -155,8 +155,9 @@ final class ProcessCommand extends AbstractCommand } $this->symfonyStyle->success(sprintf( - 'Rector is done! %d changed files', count($this->errorAndDiffCollector->getFileDiffs())) - ); + 'Rector is done! %d changed files', + count($this->errorAndDiffCollector->getFileDiffs()) + )); if ($this->configuration->isDryRun() && count($this->errorAndDiffCollector->getFileDiffs())) { return Shell::CODE_ERROR; diff --git a/src/PhpParser/Node/Maintainer/CallMaintainer.php b/src/PhpParser/Node/Maintainer/CallMaintainer.php index 1f57c9120d3..ce51559332e 100644 --- a/src/PhpParser/Node/Maintainer/CallMaintainer.php +++ b/src/PhpParser/Node/Maintainer/CallMaintainer.php @@ -120,7 +120,12 @@ final class CallMaintainer return false; } - $externalFileContent = $this->parser->parseFile($reflectionFunctionAbstract->getFileName()); + $filePath = $reflectionFunctionAbstract->getFileName(); + if (file_exists($filePath) === false) { + return true; + } + + $externalFileContent = $this->parser->parseFile($filePath); $requiredExternalType = $this->resolveMotherType($callNode); $functionName = $reflectionFunctionAbstract->getName(); diff --git a/src/PhpParser/Node/NodeFactory.php b/src/PhpParser/Node/NodeFactory.php index 05a7337fc73..00b4b120f25 100644 --- a/src/PhpParser/Node/NodeFactory.php +++ b/src/PhpParser/Node/NodeFactory.php @@ -49,11 +49,9 @@ final class NodeFactory */ public function createClassConstant(string $className, string $constantName): ClassConstFetch { - if (in_array($className, ['static', 'parent', 'self'], true)) { - $classNameNode = new Name($className); - } else { - $classNameNode = new FullyQualified($className); - } + $classNameNode = in_array($className, ['static', 'parent', 'self'], true) ? new Name( + $className + ) : new FullyQualified($className); $classConstFetchNode = $this->builderFactory->classConstFetch($classNameNode, $constantName); $classConstFetchNode->class->setAttribute(Attribute::RESOLVED_NAME, $classNameNode); diff --git a/src/Rector/Function_/FunctionReplaceRector.php b/src/Rector/Function_/FunctionReplaceRector.php index b5d9ee3a05b..8469d42541f 100644 --- a/src/Rector/Function_/FunctionReplaceRector.php +++ b/src/Rector/Function_/FunctionReplaceRector.php @@ -77,11 +77,7 @@ final class FunctionReplaceRector extends AbstractRector $newFunctions = array_reverse($newFunctions); foreach ($newFunctions as $wrapFunction) { - if ($previousNode === null) { - $arguments = $funcCallNode->args; - } else { - $arguments = [new Arg($previousNode)]; - } + $arguments = $previousNode === null ? $funcCallNode->args : [new Arg($previousNode)]; $funcCallNode = new FuncCall(new FullyQualified($wrapFunction), $arguments); $previousNode = $funcCallNode; diff --git a/src/Rector/Namespace_/NamespaceReplacerRector.php b/src/Rector/Namespace_/NamespaceReplacerRector.php index 00297f8501a..22e70c28410 100644 --- a/src/Rector/Namespace_/NamespaceReplacerRector.php +++ b/src/Rector/Namespace_/NamespaceReplacerRector.php @@ -86,11 +86,9 @@ final class NamespaceReplacerRector extends AbstractRector return $node; } - if ($this->isPartialNamespace($node)) { - $newName = $this->resolvePartialNewName($node); - } else { - $newName = $this->resolveNewNameFromNode($name); - } + $newName = $this->isPartialNamespace($node) ? $this->resolvePartialNewName( + $node + ) : $this->resolveNewNameFromNode($name); if ($newName === null) { return null;