1 | <?php |
||
38 | class DuplicatedArrayKey extends AbstractRule implements MethodAware, FunctionAware |
||
39 | { |
||
40 | /** |
||
41 | * Retrieves all arrays from single node and performs comparison logic on it |
||
42 | * |
||
43 | * @param AbstractNode $node |
||
44 | * @return void |
||
45 | */ |
||
46 | 17 | public function apply(AbstractNode $node) |
|
53 | |||
54 | /** |
||
55 | * This method checks if a given function or method contains an array literal |
||
56 | * with duplicated entries for any key and emits a rule violation if so. |
||
57 | * |
||
58 | * @param ASTNode $node Array node. |
||
59 | * @return void |
||
60 | */ |
||
61 | 15 | private function checkForDuplicatedArrayKeys(ASTNode $node) |
|
80 | |||
81 | /** |
||
82 | * Changes key name to its string format. |
||
83 | * |
||
84 | * To compare keys, we have to cast them to string. |
||
85 | * Non-associative keys have to use index as its key, |
||
86 | * while boolean and nulls have to be casted respectively. |
||
87 | * As current logic doesn't evaluate expressions nor constants, |
||
88 | * statics, globals, etc. we simply skip them. |
||
89 | * |
||
90 | * @param AbstractASTNode $node Array key to evaluate. |
||
91 | * @param int $index Fallback in case of non-associative arrays |
||
92 | * @return AbstractASTNode Key name |
||
93 | */ |
||
94 | 15 | private function normalizeKey(AbstractASTNode $node, $index) |
|
111 | |||
112 | /** |
||
113 | * Cleans string literals and casts boolean and null values as PHP engine does |
||
114 | * |
||
115 | * @param PDependASTNode $key |
||
116 | * @return string |
||
117 | */ |
||
118 | 15 | private function castStringFromLiteral(PDependASTNode $key) |
|
132 | } |
||
133 |
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: