@@ -42,12 +42,8 @@ public function __construct(array $data = [])
4242 */
4343 public function append (string $ key , $ value = null ): void
4444 {
45- if (0 == strlen ($ key )) {
46- throw new InvalidPathException ("Key cannot be an empty string " );
47- }
48-
4945 $ currentValue =& $ this ->data ;
50- $ keyPath = explode ( ' . ' , $ key );
46+ $ keyPath = $ this -> keyToPathArray ( $ key );
5147
5248 if (1 == count ($ keyPath )) {
5349 if (!isset ($ currentValue [$ key ])) {
@@ -88,12 +84,8 @@ public function append(string $key, $value = null): void
8884 */
8985 public function set (string $ key , $ value = null ): void
9086 {
91- if (0 == strlen ($ key )) {
92- throw new InvalidPathException ("Key cannot be an empty string " );
93- }
94-
9587 $ currentValue =& $ this ->data ;
96- $ keyPath = explode ( ' . ' , $ key );
88+ $ keyPath = $ this -> keyToPathArray ( $ key );
9789
9890 if (1 == count ($ keyPath )) {
9991 $ currentValue [$ key ] = $ value ;
@@ -120,12 +112,8 @@ public function set(string $key, $value = null): void
120112 */
121113 public function remove (string $ key ): void
122114 {
123- if (0 == strlen ($ key )) {
124- throw new InvalidPathException ("Key cannot be an empty string " );
125- }
126-
127115 $ currentValue =& $ this ->data ;
128- $ keyPath = explode ( ' . ' , $ key );
116+ $ keyPath = $ this -> keyToPathArray ( $ key );
129117
130118 if (1 == count ($ keyPath )) {
131119 unset($ currentValue [$ key ]);
@@ -152,7 +140,7 @@ public function remove(string $key): void
152140 public function get (string $ key , $ default = null )
153141 {
154142 $ currentValue = $ this ->data ;
155- $ keyPath = explode ( ' . ' , $ key );
143+ $ keyPath = $ this -> keyToPathArray ( $ key );
156144
157145 for ($ i = 0 ; $ i < count ($ keyPath ); $ i ++) {
158146 $ currentKey = $ keyPath [$ i ];
@@ -176,7 +164,7 @@ public function get(string $key, $default = null)
176164 public function has (string $ key ): bool
177165 {
178166 $ currentValue = &$ this ->data ;
179- $ keyPath = explode ( ' . ' , $ key );
167+ $ keyPath = $ this -> keyToPathArray ( $ key );
180168
181169 for ($ i = 0 ; $ i < count ($ keyPath ); $ i ++) {
182170 $ currentKey = $ keyPath [$ i ];
@@ -266,4 +254,20 @@ public function offsetUnset($key)
266254 {
267255 $ this ->remove ($ key );
268256 }
257+
258+ /**
259+ * @return string[]
260+ *
261+ * @psalm-return non-empty-list<string>
262+ *
263+ * @psalm-pure
264+ */
265+ protected function keyToPathArray (string $ path ): array
266+ {
267+ if (\strlen ($ path ) === 0 ) {
268+ throw new InvalidPathException ('Path cannot be an empty string ' );
269+ }
270+
271+ return \explode ('. ' , $ path );
272+ }
269273}
0 commit comments