Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Iterate over keys or values of hash, not hash itself #3037

Closed
stefanhaerter opened this issue Feb 23, 2024 · 5 comments
Closed

Iterate over keys or values of hash, not hash itself #3037

stefanhaerter opened this issue Feb 23, 2024 · 5 comments
Assignees
Labels
tidying Tidying of the code

Comments

@stefanhaerter
Copy link
Contributor

At a few places, for loops are iterating over a hash, not keys or values. While this is functionaly fine because everywhere there is a check if the iterator is a valid key of the hash, I would consider it a bad coding practice and thus fix it nonetheless.

@stefanhaerter stefanhaerter added the tidying Tidying of the code label Feb 23, 2024
@stefanhaerter stefanhaerter self-assigned this Feb 23, 2024
@stefanhaerter
Copy link
Contributor Author

stefanhaerter commented Feb 23, 2024

On rel-11_0, I already identified the relevant places (see below), but I also want to check rel-10_1 and rel-10_0, therefor PR has to wait a bit.

  • Kernel/Output/HTML/TicketOverview/Preview.pm
    my $ArticleObject = $Kernel::OM->Get('Kernel::System::Ticket::Article');
    my $PreviewArticleSenderTypes = $ConfigObject->Get('Ticket::Frontend::Overview::PreviewArticleSenderTypes');
    my %PreviewArticleSenderTypeIDs;
    if ( IsHashRefWithData($PreviewArticleSenderTypes) ) {
    KEY:
    for my $Key ( %{$PreviewArticleSenderTypes} ) {
    next KEY if !$PreviewArticleSenderTypes->{$Key};
  • Kernel/System/Auth/OpenIDConnect.pm
    KEY:
    for my $Key ( %{ $Param{Map} } ) {
    if ( IsHashRefWithData( $Param{Map}{$Key} ) ) {
  • Kernel/System/Package.pm

    otobo/Kernel/System/Package.pm

    Lines 2380 to 2389 in c0a2b2f

    TAG:
    for my $Item (qw(DatabaseInstall DatabaseUpgrade DatabaseReinstall DatabaseUninstall)) {
    if ( ref $Param{$Item} ne 'HASH' ) {
    next TAG;
    }
    for my $Type ( sort %{ $Param{$Item} } ) {
    if ( $Param{$Item}->{$Type} ) {
  • Kernel/System/ZnunyHelper.pm
    my %ScreenConfig = %Param;
    my @Settings;
    my $NoConfigRebuild = 0;
    if ( $Param{NoConfigRebuild} ) {
    $NoConfigRebuild = 1;
    delete $Param{NoConfigRebuild};
    }
    VIEW:
    for my $View (%ScreenConfig) {
    next VIEW if !IsHashRefWithData( $ScreenConfig{$View} );

    my %ScreenConfig = %Param;
    VIEW:
    for my $View (%ScreenConfig) {
    next VIEW if !IsHashRefWithData( $ScreenConfig{$View} );

@bschmalhofer
Copy link
Contributor

Yes, these loops are strange. @stefanhaerter, could you check whether there is a Perl::Critic policy that covers these cases?

@stefanhaerter
Copy link
Contributor Author

Yes, I think this is a very good idea - I thought about writing a CodePolicy plugin by myself, but if Perl::Critic already covers this, this would be great.

@stefanhaerter
Copy link
Contributor Author

@bschmalhofer At a quick glanze, I couldn't find anything that matches this need at https://metacpan.org/dist/Perl-Critic/view/lib/Perl/Critic/PolicySummary.pod. Perhaps a plugin is needed.

@stefanhaerter
Copy link
Contributor Author

Closed with merging the PRs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tidying Tidying of the code
Projects
None yet
Development

No branches or pull requests

2 participants