mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Refactor complex if statement (#14398)
This commit is contained in:
@@ -1489,22 +1489,31 @@ namespace Microsoft.PowerShell.Commands
|
||||
private bool IsCommandInResult(CommandInfo command)
|
||||
{
|
||||
bool isPresent = false;
|
||||
bool commandHasModule = command.Module != null;
|
||||
foreach (CommandInfo commandInfo in _accumulatedResults)
|
||||
|
||||
if (command.Module is not null)
|
||||
{
|
||||
if ((command.CommandType == commandInfo.CommandType &&
|
||||
(string.Equals(command.Name, commandInfo.Name, StringComparison.OrdinalIgnoreCase) ||
|
||||
// If the command has been imported with a prefix, then just checking the names for duplication will not be enough.
|
||||
// Hence, an additional check is done with the prefix information
|
||||
string.Equals(ModuleCmdletBase.RemovePrefixFromCommandName(commandInfo.Name, commandInfo.Prefix), command.Name, StringComparison.OrdinalIgnoreCase))
|
||||
) && commandInfo.Module != null && commandHasModule &&
|
||||
( // We do reference equal comparison if both command are imported. If either one is not imported, we compare the module path
|
||||
(commandInfo.IsImported && command.IsImported && commandInfo.Module.Equals(command.Module)) ||
|
||||
((!commandInfo.IsImported || !command.IsImported) && commandInfo.Module.Path.Equals(command.Module.Path, StringComparison.OrdinalIgnoreCase))
|
||||
))
|
||||
foreach (CommandInfo commandInfo in _accumulatedResults)
|
||||
{
|
||||
isPresent = true;
|
||||
break;
|
||||
if (commandInfo.Module is null || commandInfo.CommandType != command.CommandType)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// We do reference equal comparison if both command are imported. If either one is not imported, we compare the module path
|
||||
if ((!commandInfo.IsImported || !command.IsImported || !commandInfo.Module.Equals(command.Module))
|
||||
&& ((commandInfo.IsImported && command.IsImported) || !commandInfo.Module.Path.Equals(command.Module.Path, StringComparison.OrdinalIgnoreCase)))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// If the command has been imported with a prefix, then just checking the names for duplication will not be enough.
|
||||
// Hence, an additional check is done with the prefix information
|
||||
if (commandInfo.Name.Equals(command.Name, StringComparison.OrdinalIgnoreCase)
|
||||
|| ModuleCmdletBase.RemovePrefixFromCommandName(commandInfo.Name, commandInfo.Prefix).Equals(command.Name, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
isPresent = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user