Skip to content

Commit

Permalink
[Naming] Register RenameForeachValueVariableToMatchExprVariableRector…
Browse files Browse the repository at this point in the history
… to naming config set (#5696)

* [Naming] Register RenameForeachValueVariableToMatchExprVariableRector to naming config set

* fix property fetch not from this

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

* fix

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

* fixture fix

* phpstan

* [ci-review] Rector Rectify

* phpstan

* extract to separate method for collect assigns by name

* adding InflectorSingularResolver service

* skip single prefix and length >= 40

* add failing fixture to skip plural camel case

* use regex to get camel cases

* implemented singularize camel cased

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

* phpstan

* handle singular verb news -> new

* [ci-review] Rector Rectify

* fixture fix

* handle has

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

* phpstan

* phpstan

* handle left side By

* [ci-review] Rector Rectify

* re-use resolve call

* [ci-review] Rector Rectify

* phpstan

* [ci-review] Rector Rectify

* final touch

* final touch

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

* use previous By in the middle

* update $childClassReflection->hasProperty($propertyName)

* [ci-review] Rector Rectify

* catchKeys

* regex fix

* fixture

* [ci-review] Rector Rectify

* try use check array

* Revert "try use check array"

This reverts commit adb9f76.

* use files

Co-authored-by: kaizen-ci <[email protected]>
  • Loading branch information
samsonasik and kaizen-ci authored Mar 5, 2021
1 parent 273e1d2 commit d7592f5
Show file tree
Hide file tree
Showing 90 changed files with 545 additions and 309 deletions.
2 changes: 2 additions & 0 deletions config/set/naming.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Rector\Naming\Rector\ClassMethod\MakeIsserClassMethodNameStartWithIsRector;
use Rector\Naming\Rector\ClassMethod\RenameParamToMatchTypeRector;
use Rector\Naming\Rector\ClassMethod\RenameVariableToMatchNewTypeRector;
use Rector\Naming\Rector\Foreach_\RenameForeachValueVariableToMatchExprVariableRector;
use Rector\Naming\Rector\Foreach_\RenameForeachValueVariableToMatchMethodCallReturnTypeRector;
use Rector\Naming\Rector\Property\MakeBoolPropertyRespectIsHasWasMethodNamingRector;
use Rector\Naming\Rector\Property\UnderscoreToCamelCasePropertyNameRector;
Expand All @@ -26,4 +27,5 @@
$services->set(MakeBoolPropertyRespectIsHasWasMethodNamingRector::class);
$services->set(UnderscoreToCamelCasePropertyNameRector::class);
$services->set(UnderscoreToCamelCaseVariableNameRector::class);
$services->set(RenameForeachValueVariableToMatchExprVariableRector::class);
};
Original file line number Diff line number Diff line change
Expand Up @@ -166,20 +166,20 @@ private function createClassReflectionFromNode(Class_ $class): ClassReflection
*/
private function matchNextAnnotation(array $annotations, string $annotationClassName, Node $node): ?object
{
foreach ($annotations as $annotatoin) {
if (! is_a($annotatoin, $annotationClassName, true)) {
foreach ($annotations as $annotation) {
if (! is_a($annotation, $annotationClassName, true)) {
continue;
}

$objectHash = md5(spl_object_hash($node) . serialize($annotatoin));
$objectHash = md5(spl_object_hash($node) . serialize($annotation));
if (in_array($objectHash, $this->alreadyProvidedAnnotations, true)) {
continue;
}

$this->alreadyProvidedAnnotations[] = $objectHash;
$this->constantReferenceIdentifierRestorer->restoreObject($annotatoin);
$this->constantReferenceIdentifierRestorer->restoreObject($annotation);

return $annotatoin;
return $annotation;
}

return null;
Expand Down
4 changes: 2 additions & 2 deletions packages/better-php-doc-parser/src/Comment/CommentsMerger.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ public function keepChildren(Node $newNode, Node $oldNode): void
}

$commentContent = '';
foreach ($childrenComments as $comment) {
$commentContent .= $comment->getText() . PHP_EOL;
foreach ($childrenComments as $childComment) {
$commentContent .= $childComment->getText() . PHP_EOL;
}

$newNode->setAttribute(AttributeKey::COMMENTS, [new Comment($commentContent)]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ public function provideData(): Iterator
{
foreach ($this->getDirectoriesByTagValueNodes() as $tagValueNode => $directory) {
$filesInDirectory = $this->findFilesFromDirectory($directory);
foreach ($filesInDirectory as $fileInfos) {
foreach ($fileInfos as $fileInfo) {
yield [$fileInfo, $tagValueNode];
foreach ($filesInDirectory as $fileInDirectory) {
foreach ($fileInDirectory as $singleFileInDirectory) {
yield [$singleFileInDirectory, $tagValueNode];
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/caching/src/FileSystem/DependencyResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public function resolveDependencies(Node $node, Scope $scope): array
$fileInfos = $this->configuration->getFileInfos();

$analysedFileAbsolutesPaths = [];
foreach ($fileInfos as $analysedFile) {
$analysedFileAbsolutesPaths[] = $analysedFile->getRealPath();
foreach ($fileInfos as $fileInfo) {
$analysedFileAbsolutesPaths[] = $fileInfo->getRealPath();
}

$dependencyFiles = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ public function provideParameterTypesFromMethodReflection(MethodReflection $meth
$parameterTypes = [];

$parameterReflections = $this->getParameterReflectionsFromMethodReflection($methodReflection);
foreach ($parameterReflections as $phpParameterReflection) {
$parameterTypes[] = $phpParameterReflection->getType();
foreach ($parameterReflections as $parameterReflection) {
$parameterTypes[] = $parameterReflection->getType();
}

return $parameterTypes;
Expand Down
8 changes: 4 additions & 4 deletions packages/node-name-resolver/src/NodeNameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -449,17 +449,17 @@ private function reportInvalidNodeForName(Node $node): void
*/
private function matchRectorBacktraceCall(array $backtrace): ?array
{
foreach ($backtrace as $singleTrace) {
if (! isset($singleTrace['object'])) {
foreach ($backtrace as $singleBacktrace) {
if (! isset($singleBacktrace['object'])) {
continue;
}

// match a Rector class
if (! is_a($singleTrace['object'], RectorInterface::class)) {
if (! is_a($singleBacktrace['object'], RectorInterface::class)) {
continue;
}

return $singleTrace;
return $singleBacktrace;
}

return $backtrace[1] ?? null;
Expand Down
6 changes: 3 additions & 3 deletions packages/node-removal/src/ClassMethodRemover.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ public function removeClassMethodAndUsages(ClassMethod $classMethod): void
$this->nodeRemover->removeNode($classMethod);

$calls = $this->nodeRepository->findCallsByClassMethod($classMethod);
foreach ($calls as $classMethodCall) {
if ($classMethodCall instanceof ArrayCallable) {
foreach ($calls as $call) {
if ($call instanceof ArrayCallable) {
continue;
}

$this->removeMethodCall($classMethodCall);
$this->removeMethodCall($call);
}
}

Expand Down
5 changes: 3 additions & 2 deletions packages/node-type-resolver/src/NodeTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,9 @@ public function getFullyQualifiedClassName(TypeWithClassName $typeWithClassName)
*/
public function isSameObjectTypes(ObjectType $objectType, array $desiredTypes): bool
{
foreach ($desiredTypes as $abstractClassConstructorParamType) {
if ($abstractClassConstructorParamType->equals($objectType)) {
foreach ($desiredTypes as $desiredType) {
$desiredTypeEquals = $desiredType->equals($objectType);
if ($desiredTypeEquals) {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ private function createArgsFromItems(array $items, ?string $silentKey = null): a
$args[] = new Arg($value, false, false, [], $argumentName);
}
} else {
foreach ($items as $value) {
$value = BuilderHelpers::normalizeValue($value);
$args[] = new Arg($value);
foreach ($items as $item) {
$item = BuilderHelpers::normalizeValue($item);
$args[] = new Arg($item);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ public function narrowToGenericClassStringType(UnionType $unionType): Type
$genericClassStrings = [];
if ($unionedType->getGenericType() instanceof ObjectType) {
$parentClassReflections = $this->resolveClassParentClassesAndInterfaces($unionedType->getGenericType());
foreach ($parentClassReflections as $classReflection) {
$genericClassStrings[] = $classReflection->getName();
foreach ($parentClassReflections as $parentClassReflection) {
$genericClassStrings[] = $parentClassReflection->getName();
}
}

Expand Down Expand Up @@ -118,8 +118,8 @@ private function narrowToSharedTypes(UnionType $unionType): array

$typeClassReflections = $this->resolveClassParentClassesAndInterfaces($unionedType);
$typeClassNames = [];
foreach ($typeClassReflections as $classReflection) {
$typeClassNames[] = $classReflection->getName();
foreach ($typeClassReflections as $typeClassReflection) {
$typeClassNames[] = $typeClassReflection->getName();
}

$availableTypes[] = $typeClassNames;
Expand Down
8 changes: 4 additions & 4 deletions packages/post-rector/src/Collector/UseNodesToAddCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,15 @@ public function isImportShortable(Node $node, FullyQualifiedObjectType $fullyQua

$fileUseImportTypes = $this->useImportTypesInFilePath[$filePath] ?? [];

foreach ($fileUseImportTypes as $useImportType) {
if ($fullyQualifiedObjectType->equals($useImportType)) {
foreach ($fileUseImportTypes as $fileUseImportType) {
if ($fullyQualifiedObjectType->equals($fileUseImportType)) {
return true;
}
}

$functionImports = $this->functionUseImportTypesInFilePath[$filePath] ?? [];
foreach ($functionImports as $useImportType) {
if ($fullyQualifiedObjectType->equals($useImportType)) {
foreach ($functionImports as $functionImport) {
if ($fullyQualifiedObjectType->equals($functionImport)) {
return true;
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/rector-generator/src/Generator/FileGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ public function generateFiles(
): array {
$generatedFilePaths = [];

foreach ($templateFileInfos as $fileInfo) {
foreach ($templateFileInfos as $templateFileInfo) {
$generatedFilePaths[] = $this->generateFileInfoWithTemplateVariables(
$fileInfo,
$templateFileInfo,
$templateVariables,
$rectorRecipe,
$destinationDirectory
Expand Down
8 changes: 4 additions & 4 deletions packages/testing/src/Finder/RectorsFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,16 @@ public function findInDirectoriesAndCreate(array $directories): array
$foundClasses = $this->findClassesInDirectoriesByName($directories, '*Rector.php');

$rectors = [];
foreach ($foundClasses as $class) {
if ($this->shouldSkipClass($class)) {
foreach ($foundClasses as $foundClass) {
if ($this->shouldSkipClass($foundClass)) {
continue;
}

$reflectionClass = new ReflectionClass($class);
$reflectionClass = new ReflectionClass($foundClass);
$rector = $reflectionClass->newInstanceWithoutConstructor();
if (! $rector instanceof RectorInterface) {
// lowercase letter bug in RobotLoader
if (Strings::endsWith($class, 'rector')) {
if (Strings::endsWith($foundClass, 'rector')) {
continue;
}

Expand Down
12 changes: 6 additions & 6 deletions packages/testing/src/PHPUnit/AbstractRectorTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,22 +183,22 @@ protected function doTestFileInfo(SmartFileInfo $fixtureFileInfo, array $extraFi
protected function doTestExtraFile(string $expectedExtraFileName, string $expectedExtraContentFilePath): void
{
$addedFilesWithContents = $this->removedAndAddedFilesCollector->getAddedFilesWithContent();
foreach ($addedFilesWithContents as $addedFilesWithContent) {
if (! Strings::endsWith($addedFilesWithContent->getFilePath(), $expectedExtraFileName)) {
foreach ($addedFilesWithContents as $addedFileWithContent) {
if (! Strings::endsWith($addedFileWithContent->getFilePath(), $expectedExtraFileName)) {
continue;
}

$this->assertStringEqualsFile($expectedExtraContentFilePath, $addedFilesWithContent->getFileContent());
$this->assertStringEqualsFile($expectedExtraContentFilePath, $addedFileWithContent->getFileContent());
return;
}

$addedFilesWithNodes = $this->removedAndAddedFilesCollector->getAddedFilesWithNodes();
foreach ($addedFilesWithNodes as $addedFileWithNodes) {
if (! Strings::endsWith($addedFileWithNodes->getFilePath(), $expectedExtraFileName)) {
foreach ($addedFilesWithNodes as $addedFileWithNode) {
if (! Strings::endsWith($addedFileWithNode->getFilePath(), $expectedExtraFileName)) {
continue;
}

$printedFileContent = $this->betterStandardPrinter->prettyPrintFile($addedFileWithNodes->getNodes());
$printedFileContent = $this->betterStandardPrinter->prettyPrintFile($addedFileWithNode->getNodes());
$this->assertStringEqualsFile($expectedExtraContentFilePath, $printedFileContent);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,23 +131,23 @@ private function processGroupNamesBySuffix(
FileNode $fileNode,
array $groupNamesBySuffix
): void {
foreach ($groupNamesBySuffix as $groupName) {
foreach ($groupNamesBySuffix as $groupNames) {
// has class suffix
$suffixPattern = '\w+' . $groupName . '(Test)?\.php$';
$suffixPattern = '\w+' . $groupNames . '(Test)?\.php$';
if (! Strings::match($smartFileInfo->getRealPath(), '#' . $suffixPattern . '#')) {
continue;
}

if ($this->isLocatedInExpectedLocation($groupName, $suffixPattern, $smartFileInfo)) {
if ($this->isLocatedInExpectedLocation($groupNames, $suffixPattern, $smartFileInfo)) {
continue;
}

// file is already in the group
if (Strings::match($smartFileInfo->getPath(), '#' . $groupName . '$#')) {
if (Strings::match($smartFileInfo->getPath(), '#' . $groupNames . '$#')) {
continue;
}

$this->moveFileToGroupName($smartFileInfo, $fileNode, $groupName);
$this->moveFileToGroupName($smartFileInfo, $fileNode, $groupNames);
return;
}
}
Expand Down
11 changes: 5 additions & 6 deletions rules/cakephp/src/Rector/MethodCall/ArrayToFluentCallRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,20 +193,19 @@ private function extractFluentMethods(array $originalArrayItems, array $arrayMap
$newArrayItems = [];
$fluentCalls = [];

foreach ($originalArrayItems as $arrayItem) {
if ($arrayItem === null) {
foreach ($originalArrayItems as $originalArrayItem) {
if ($originalArrayItem === null) {
continue;
}

/** @var ArrayItem $arrayItem */
$key = $arrayItem->key;
$key = $originalArrayItem->key;

if ($key instanceof String_ && isset($arrayMap[$key->value])) {
/** @var string $methodName */
$methodName = $arrayMap[$key->value];
$fluentCalls[$methodName] = $arrayItem->value;
$fluentCalls[$methodName] = $originalArrayItem->value;
} else {
$newArrayItems[] = $arrayItem;
$newArrayItems[] = $originalArrayItem;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ private function hasReAssign(Expression $expression, Expr $expr): bool
}

while ($next) {
foreach ($exprValues as $value) {
foreach ($exprValues as $exprValue) {
$isReAssign = (bool) $this->betterNodeFinder->findFirst($next, function (Node $node): bool {
$parent = $node->getAttribute(AttributeKey::PARENT_NODE);
$node = $this->mayBeArrayDimFetch($node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ private function createReturnCoalesceNode(array $coalescingNodes): Return_
$right = array_shift($coalescingNodes);

$coalesceNode = new Coalesce($left, $right);
foreach ($coalescingNodes as $nextCoalescingNode) {
$coalesceNode = new Coalesce($coalesceNode, $nextCoalescingNode);
foreach ($coalescingNodes as $coalescingNode) {
$coalesceNode = new Coalesce($coalesceNode, $coalescingNode);
}

return new Return_($coalesceNode);
Expand Down
4 changes: 2 additions & 2 deletions rules/coding-style/src/Node/NameImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ private function importNameAndCollectNewUseStatement(
$this->addUseImport($name, $fullyQualifiedObjectType);

// possibly aliased
foreach ($this->aliasedUses as $aliasUse) {
if ($fullyQualifiedObjectType->getClassName() === $aliasUse) {
foreach ($this->aliasedUses as $aliasedUse) {
if ($fullyQualifiedObjectType->getClassName() === $aliasedUse) {
return null;
}
}
Expand Down
8 changes: 4 additions & 4 deletions rules/coding-style/src/Node/UseManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,19 @@ private function resolveUsedNames(Node $node): void
/** @var Name[] $namedNodes */
$namedNodes = $this->betterNodeFinder->findInstanceOf($node, Name::class);

foreach ($namedNodes as $nameNode) {
foreach ($namedNodes as $namedNode) {
/** node name before becoming FQN - attribute from @see NameResolver */
$originalName = $nameNode->getAttribute(AttributeKey::ORIGINAL_NAME);
$originalName = $namedNode->getAttribute(AttributeKey::ORIGINAL_NAME);
if (! $originalName instanceof Name) {
continue;
}

$parentNode = $nameNode->getAttribute(AttributeKey::PARENT_NODE);
$parentNode = $namedNode->getAttribute(AttributeKey::PARENT_NODE);
if (! $parentNode instanceof Node) {
throw new ShouldNotHappenException();
}

$this->resolvedNodeNames[$originalName->toString()][] = new NameAndParent($nameNode, $parentNode);
$this->resolvedNodeNames[$originalName->toString()][] = new NameAndParent($namedNode, $parentNode);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ public function refactor(Node $node): ?Node
$firstPropertyProperty = array_shift($allProperties);
$node->props = [$firstPropertyProperty];

foreach ($allProperties as $anotherProperty) {
$nextProperty = new Property($node->flags, [$anotherProperty], $node->getAttributes());
foreach ($allProperties as $allProperty) {
$nextProperty = new Property($node->flags, [$allProperty], $node->getAttributes());
$this->addNodeAfterNode($nextProperty, $node);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ public function configure(array $configuration): void
$typeToPreference = $configuration[self::TYPE_TO_PREFERENCE] ?? [];
Assert::allString($typeToPreference);

foreach ($typeToPreference as $preference) {
$this->ensurePreferenceIsValid($preference);
foreach ($typeToPreference as $singleTypeToPreference) {
$this->ensurePreferenceIsValid($singleTypeToPreference);
}

$this->typeToPreference = $typeToPreference;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ public function __construct(VersionGuard $versionGuard)

public function refactor(ComposerJson $composerJson): void
{
foreach ($this->packagesAndVersions as $packagesAndVersion) {
foreach ($this->packagesAndVersions as $packageAndVersion) {
$composerJson->changePackageVersion(
$packagesAndVersion->getPackageName(),
$packagesAndVersion->getVersion()
$packageAndVersion->getPackageName(),
$packageAndVersion->getVersion()
);
}
}
Expand Down
Loading

0 comments on commit d7592f5

Please sign in to comment.