1.2.42. Array Access On Literal Array

Accessing an element on a literal array makes that array non-reusable.

It is recommended to make this array a constant or a property, for easier reusage. It also make that content more visiblem in the class definitions.

<?php

class Suit {
     const NAMES = ['Club' => 1, 'Spade' => 2, 'Heart' => 3, 'Diamond' => 4];

     function __construct($name) {
             if (!isset(self::NAMES[$name]) {
                     throw new Exception('Not a suit color');
             }
     }
}

class HiddenSuitList {
     function __construct($name) {
             if (!isset(['Club' => 1, 'Spade' => 2, 'Heart' => 3, 'Diamond' => 4][$name]) {
                     throw new Exception('Not a suit color');
             }
     }
}

?>

1.2.42.1. Specs

Short name

Structures/ArrayAccessOnLiteralArray

Rulesets

All, Analyze, Changed Behavior, Semantics

Exakat since

2.5.2

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

High

Available in

Entreprise Edition, Exakat Cloud