| Conditions | 1 |
| Paths | 1 |
| Total Lines | 112 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 56 | function TestArrayInsert () { |
||
| 57 | // Pos not exists, number indexed |
||
| 58 | $ar_srce = array('a', 'b', 'c'); |
||
| 59 | $x = $ar_srce; |
||
| 60 | $x = ArrayInsert($x, 'd', array('d')); |
||
| 61 | $this->assertEqual(var_export($x, true) |
||
| 62 | , var_export(array('a', 'b', 'c', 'd'), true)); |
||
| 63 | |||
| 64 | // Pos not exists, assoc indexed |
||
| 65 | $ar_srce = array( |
||
| 66 | 'a' => 1, |
||
| 67 | 'b' => 2, |
||
| 68 | 'c' => 3, |
||
| 69 | ); |
||
| 70 | $x = $ar_srce; |
||
| 71 | $x = ArrayInsert($x, 'd', array('d')); |
||
| 72 | $this->assertEqual(var_export($x, true), var_export(array( |
||
| 73 | 'a' => 1, |
||
| 74 | 'b' => 2, |
||
| 75 | 'c' => 3, |
||
| 76 | 0 => 'd', |
||
| 77 | ), true)); |
||
| 78 | |||
| 79 | // Assoc indexed, normal |
||
| 80 | $ar_srce = array( |
||
| 81 | 'a' => 1, |
||
| 82 | 'b' => 2, |
||
| 83 | 'c' => 3, |
||
| 84 | 'd' => 4, |
||
| 85 | 'e' => 5, |
||
| 86 | ); |
||
| 87 | $ar_ins = array( |
||
| 88 | 'ins1' => 'ins1', |
||
| 89 | 'ins2' => 'ins2', |
||
| 90 | ); |
||
| 91 | // Insert before a key |
||
| 92 | $x = $ar_srce; |
||
| 93 | ArrayInsert($x, 'c', $ar_ins, -2); |
||
| 94 | $this->assertEqual(var_export($x, true), var_export(array( |
||
| 95 | 'a' => 1, |
||
| 96 | 'ins1' => 'ins1', |
||
| 97 | 'ins2' => 'ins2', |
||
| 98 | 'b' => 2, |
||
| 99 | 'c' => 3, |
||
| 100 | 'd' => 4, |
||
| 101 | 'e' => 5, |
||
| 102 | ), true)); |
||
| 103 | |||
| 104 | // Insert after a key |
||
| 105 | $x = $ar_srce; |
||
| 106 | ArrayInsert($x, 'c', $ar_ins, 2); |
||
| 107 | $this->assertEqual(var_export($x, true), var_export(array( |
||
| 108 | 'a' => 1, |
||
| 109 | 'b' => 2, |
||
| 110 | 'c' => 3, |
||
| 111 | 'd' => 4, |
||
| 112 | 'ins1' => 'ins1', |
||
| 113 | 'ins2' => 'ins2', |
||
| 114 | 'e' => 5, |
||
| 115 | ), true)); |
||
| 116 | |||
| 117 | // Replace |
||
| 118 | $x = $ar_srce; |
||
| 119 | ArrayInsert($x, 'a', $ar_ins, 0); |
||
| 120 | $this->assertEqual(var_export($x, true), var_export(array( |
||
| 121 | 'ins1' => 'ins1', |
||
| 122 | 'ins2' => 'ins2', |
||
| 123 | 'b' => 2, |
||
| 124 | 'c' => 3, |
||
| 125 | 'd' => 4, |
||
| 126 | 'e' => 5, |
||
| 127 | ), true)); |
||
| 128 | |||
| 129 | // Replace & not exist = append |
||
| 130 | $x = $ar_srce; |
||
| 131 | ArrayInsert($x, 'f', $ar_ins, 0); |
||
| 132 | $this->assertEqual(var_export($x, true), var_export(array( |
||
| 133 | 'a' => 1, |
||
| 134 | 'b' => 2, |
||
| 135 | 'c' => 3, |
||
| 136 | 'd' => 4, |
||
| 137 | 'e' => 5, |
||
| 138 | 'ins1' => 'ins1', |
||
| 139 | 'ins2' => 'ins2', |
||
| 140 | ), true)); |
||
| 141 | |||
| 142 | // Insert far before |
||
| 143 | $x = $ar_srce; |
||
| 144 | ArrayInsert($x, 'a', $ar_ins, -10); |
||
| 145 | $this->assertEqual(var_export($x, true), var_export(array( |
||
| 146 | 'ins1' => 'ins1', |
||
| 147 | 'ins2' => 'ins2', |
||
| 148 | 'a' => 1, |
||
| 149 | 'b' => 2, |
||
| 150 | 'c' => 3, |
||
| 151 | 'd' => 4, |
||
| 152 | 'e' => 5, |
||
| 153 | ), true)); |
||
| 154 | |||
| 155 | // Insert far after |
||
| 156 | $x = $ar_srce; |
||
| 157 | ArrayInsert($x, 'e', $ar_ins, 10); |
||
| 158 | $this->assertEqual(var_export($x, true), var_export(array( |
||
| 159 | 'a' => 1, |
||
| 160 | 'b' => 2, |
||
| 161 | 'c' => 3, |
||
| 162 | 'd' => 4, |
||
| 163 | 'e' => 5, |
||
| 164 | 'ins1' => 'ins1', |
||
| 165 | 'ins2' => 'ins2', |
||
| 166 | ), true)); |
||
| 167 | } // end of func TestTestArrayInsert |
||
| 168 | |||
| 206 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.