开发者

Why am I getting the ReSharper error "The extracted code has multiple entry points"?

I am using the ReSharper to re-factor my code. When I try to move a block of code to the method, I get the following warning:

The extracted code has multiple entry points

Here is the method signature I am planning to use:

private void GetRatePlanComponents(ProductPlan productPlan, 
    ProductRatePlan produ开发者_如何学CctRatePlan)    

I searched the web to understand what does it mean. But didn't have much luck. Would someone explain it?

For your reference, here is the code snippet I am trying to move to a separate method:

QueryResult productRatePlanChargeQueryResult = 
    _zuoraService.query(string.Format(@"select Id, Name, IncludedUnits from
        ProductRatePlanCharge where ProductRatePlanId = '{0}' and 
        ChargeModel = 'Overage Pricing'", productRatePlan.Id));

if (productRatePlanChargeQueryResult.size > 0)
{
    foreach (ProductRatePlanCharge productRatePlanCharge 
        in productRatePlanChargeQueryResult.records)
    {
        string numberOfUnits = productRatePlanCharge.IncludedUnits.ToString();

        if (productRatePlanCharge.Name.Equals("Users"))
        {
            productPlan.NumberofUsers = numberOfUnits;
        }
        else if (productRatePlanCharge.Name.Equals("Projects"))
        {
            productPlan.NumberofProjects = numberOfUnits;
        }
        else if (productRatePlanCharge.Name.Equals("Storage"))
        {
            decimal volumeOfStorage;
            if (decimal.TryParse(productRatePlanCharge.IncludedUnits.ToString(), 
                out volumeOfStorage))
            {
                if (volumeOfStorage < 1) volumeOfStorage *= 1000;
                    productPlan.VolumeofStorage = volumeOfStorage.ToString();
                }
                else
                {
                    productPlan.VolumeofStorage = numberOfUnits;
                }
            }
        }
    }
}


It looks like you may have encountered a known issue:

public static IEnumerable<ITagPrefixHolder> GetRelevantHolders(IPsiSourceFile sourceFile )
{
  var targetPath = FileSystemPath.Empty;
  var projectFile = sourceFile.ToProjectFile();
  if (projectFile != null)
    targetPath = projectFile.Location;

  foreach(var holder in GetRelevantHoldersBeforeFile(sourceFile, targetPath))
    yield return holder;

  foreach(var holder in GetHoldersInFile(sourceFile, targetPath))
    yield return holder;
}

Select both foreach-loops and extract method. It gives strange warning that the fragment has multiple entry points (??!) and results in the following code:

public static IEnumerable<ITagPrefixHolder> GetRelevantHolders(IPsiSourceFile sourceFile )
{
  var targetPath = FileSystemPath.Empty;
  var projectFile = sourceFile.ToProjectFile();
  if (projectFile != null)
    targetPath = projectFile.Location;

  foreach(var tagPrefixHolder in Foo(sourceFile, targetPath))
       yield return tagPrefixHolder;
}

private static IEnumerable<ITagPrefixHolder> Foo(IPsiSourceFile sourceFile, FileSystemPath targetPath)
{
  foreach(var holder in GetRelevantHoldersBeforeFile(sourceFile, targetPath))
    yield return holder;
  foreach(var holder in GetHoldersInFile(sourceFile, targetPath))
    yield return holder;
}

It would be better to replace generated foreach with simple return Foo(sourceFile, targetPath);.


I have seen ReSharper do this same thing when the code I was trying to extract had a couple of throw statements.

You can do what I did in that case--systematically comment out one line at a time until you find the one that ReSharper trips over. Then you can extract the method and uncomment the line afterwards.

Or you can just refactor it by hand.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜