@@ -75,7 +75,7 @@ public function expanding($kind, $expansion) {
7575 public function load ($ in , $ charset = 'utf-8 ' ): self {
7676 $ reader = $ in instanceof Reader ? $ in : new TextReader ($ in , $ charset );
7777 $ this ->_data = [];
78- $ section = null ;
78+ $ section = '' ;
7979
8080 while (null !== ($ t = $ reader ->readLine ())) {
8181 $ trimmedToken = trim ($ t );
@@ -233,6 +233,7 @@ public function sections(): Traversable {
233233 */
234234 public function readSection ($ name , $ default = []) {
235235 $ this ->_load ();
236+ $ name ??= '' ;
236237 if (null === ($ section = $ this ->_data [$ name ] ?? null )) return $ default ;
237238
238239 $ expansion = $ this ->expansion ?? self ::$ env ;
@@ -253,7 +254,7 @@ public function readSection($name, $default= []) {
253254 */
254255 public function readString ($ section , $ key , $ default = '' ) {
255256 $ this ->_load ();
256- if (null === ($ value = $ this ->_data [$ section ][$ key ] ?? null )) return $ default ;
257+ if (null === ($ value = $ this ->_data [$ section ?? '' ][$ key ] ?? null )) return $ default ;
257258
258259 $ expansion = $ this ->expansion ?? self ::$ env ;
259260 return $ expansion ->in ($ value );
@@ -269,7 +270,7 @@ public function readString($section, $key, $default= '') {
269270 */
270271 public function readArray ($ section , $ key , $ default = []) {
271272 $ this ->_load ();
272- if (null === ($ value = $ this ->_data [$ section ][$ key ] ?? null )) return $ default ;
273+ if (null === ($ value = $ this ->_data [$ section ?? '' ][$ key ] ?? null )) return $ default ;
273274
274275 // New: key[]="a" or key[0]="a"
275276 // Old: key="" (an empty array) or key="a|b|c"
@@ -293,7 +294,7 @@ public function readArray($section, $key, $default= []) {
293294 */
294295 public function readMap ($ section , $ key , $ default = null ) {
295296 $ this ->_load ();
296- if (null === ($ value = $ this ->_data [$ section ][$ key ] ?? null )) return $ default ;
297+ if (null === ($ value = $ this ->_data [$ section ?? '' ][$ key ] ?? null )) return $ default ;
297298
298299 // New: key[color]="green" and key[make]="model"
299300 // Old: key="color:green|make:model"
@@ -326,7 +327,7 @@ public function readMap($section, $key, $default= null) {
326327 */
327328 public function readRange ($ section , $ key , $ default = []) {
328329 $ this ->_load ();
329- if (null === ($ value = $ this ->_data [$ section ][$ key ] ?? null )) return $ default ;
330+ if (null === ($ value = $ this ->_data [$ section ?? '' ][$ key ] ?? null )) return $ default ;
330331
331332 $ expansion = $ this ->expansion ?? self ::$ env ;
332333 if (2 === sscanf ($ expansion ->in ($ value ), '%d..%d ' , $ min , $ max )) {
@@ -346,7 +347,7 @@ public function readRange($section, $key, $default= []) {
346347 */
347348 public function readInteger ($ section , $ key , $ default = 0 ) {
348349 $ this ->_load ();
349- if (null === ($ value = $ this ->_data [$ section ][$ key ] ?? null )) return $ default ;
350+ if (null === ($ value = $ this ->_data [$ section ?? '' ][$ key ] ?? null )) return $ default ;
350351
351352 $ expansion = $ this ->expansion ?? self ::$ env ;
352353 return (int )$ expansion ->in ($ value );
@@ -362,7 +363,7 @@ public function readInteger($section, $key, $default= 0) {
362363 */
363364 public function readFloat ($ section , $ key , $ default = 0.0 ) {
364365 $ this ->_load ();
365- if (null === ($ value = $ this ->_data [$ section ][$ key ] ?? null )) return $ default ;
366+ if (null === ($ value = $ this ->_data [$ section ?? '' ][$ key ] ?? null )) return $ default ;
366367
367368 $ expansion = $ this ->expansion ?? self ::$ env ;
368369 return (float )$ expansion ->in ($ value );
@@ -380,7 +381,7 @@ public function readBool($section, $key, $default= false) {
380381 static $ true = ['1 ' => 1 , 'yes ' => 1 , 'true ' => 1 , 'on ' => 1 ];
381382
382383 $ this ->_load ();
383- if (null === ($ value = $ this ->_data [$ section ][$ key ] ?? null )) return $ default ;
384+ if (null === ($ value = $ this ->_data [$ section ?? '' ][$ key ] ?? null )) return $ default ;
384385
385386 $ expansion = $ this ->expansion ?? self ::$ env ;
386387 return isset ($ true [strtolower ($ expansion ->in ($ value ))]);
@@ -419,6 +420,7 @@ public function writeSection($name, $overwrite= false) {
419420 */
420421 public function writeString ($ section , $ key , $ value ) {
421422 $ this ->_load ();
423+ $ section ??= '' ;
422424 if (!$ this ->hasSection ($ section )) $ this ->_data [$ section ]= [];
423425 $ this ->_data [$ section ][$ key ]= (string )$ value ;
424426 }
@@ -432,6 +434,7 @@ public function writeString($section, $key, $value) {
432434 */
433435 public function writeInteger ($ section , $ key , $ value ) {
434436 $ this ->_load ();
437+ $ section ??= '' ;
435438 if (!$ this ->hasSection ($ section )) $ this ->_data [$ section ]= [];
436439 $ this ->_data [$ section ][$ key ]= (int )$ value ;
437440 }
@@ -445,6 +448,7 @@ public function writeInteger($section, $key, $value) {
445448 */
446449 public function writeFloat ($ section , $ key , $ value ) {
447450 $ this ->_load ();
451+ $ section ??= '' ;
448452 if (!$ this ->hasSection ($ section )) $ this ->_data [$ section ]= [];
449453 $ this ->_data [$ section ][$ key ]= (float )$ value ;
450454 }
@@ -458,6 +462,7 @@ public function writeFloat($section, $key, $value) {
458462 */
459463 public function writeBool ($ section , $ key , $ value ) {
460464 $ this ->_load ();
465+ $ section ??= '' ;
461466 if (!$ this ->hasSection ($ section )) $ this ->_data [$ section ]= [];
462467 $ this ->_data [$ section ][$ key ]= $ value ? 'yes ' : 'no ' ;
463468 }
@@ -471,6 +476,7 @@ public function writeBool($section, $key, $value) {
471476 */
472477 public function writeArray ($ section , $ key , $ value ) {
473478 $ this ->_load ();
479+ $ section ??= '' ;
474480 if (!$ this ->hasSection ($ section )) $ this ->_data [$ section ]= [];
475481 $ this ->_data [$ section ][$ key ]= $ value ;
476482 }
@@ -484,6 +490,7 @@ public function writeArray($section, $key, $value) {
484490 */
485491 public function writeMap ($ section , $ key , $ value ) {
486492 $ this ->_load ();
493+ $ section ??= '' ;
487494 if (!$ this ->hasSection ($ section )) $ this ->_data [$ section ]= [];
488495 $ this ->_data [$ section ][$ key ]= $ value ;
489496 }
@@ -497,6 +504,7 @@ public function writeMap($section, $key, $value) {
497504 */
498505 public function writeComment ($ section , $ comment ) {
499506 $ this ->_load ();
507+ $ section ??= '' ;
500508 if (!$ this ->hasSection ($ section )) $ this ->_data [$ section ]= [];
501509 $ this ->_data [$ section ]['; ' .sizeof ($ this ->_data [$ section ])]= $ comment ;
502510 }
@@ -509,6 +517,7 @@ public function writeComment($section, $comment) {
509517 */
510518 public function removeSection ($ section ) {
511519 $ this ->_load ();
520+ $ section ??= '' ;
512521 if (!isset ($ this ->_data [$ section ])) throw new IllegalStateException ('Cannot remove nonexistant section " ' .$ section .'" ' );
513522 unset($ this ->_data [$ section ]);
514523 }
@@ -522,6 +531,7 @@ public function removeSection($section) {
522531 */
523532 public function removeKey ($ section , $ key ) {
524533 $ this ->_load ();
534+ $ section ??= '' ;
525535 if (!isset ($ this ->_data [$ section ][$ key ])) throw new IllegalStateException ('Cannot remove nonexistant key " ' .$ key .'" in " ' .$ section .'" ' );
526536 unset($ this ->_data [$ section ][$ key ]);
527537 }
0 commit comments