mustangostang / spyc

A simple YAML loader/dumper class for PHP
MIT License
701 stars 206 forks source link

PHP Warning: strpos(): Empty needle #69

Closed ktomk closed 2 years ago

ktomk commented 6 years ago

This warning is given with different PHP versions when a sort of invalid string is encountered. The invalid string is an array entry like so:

- : # comment

It is invalid as the saying goes the colon should be put into double-quotes.

The warning is given on line 1054 in Spyc.php, that is inside \Spyc::returnMappedSequence and it says that the $key is a zero-length string.

I'd say it should not give a warning at all, perhaps give a parse error instead. Not so firm with the YAML syntax requirements. Here an online YAML parser which hightlights the error in my example:

The example is a bit verbose, a short version would be:

Which does not trigger the warning w/ Spyc, but

does, this that is the minimal YAML example to trigger the warning.

I'm new to Spyc, so I'm not so firm about the error handling. The : example gives not warning at all and the file returns as parsed, so I assume not all errors lead to a hard stop. But no idea how that is deeper in. In comparison the online YAML parser gives an error in both cases.

This might be useful, as YAML 1.0 allows mappings and lists for keys in key-value pairs of mappings which is not supported for PHP arrays as used to collect the mapping sequence as far as I looked in your code.

So let me know how to error handle, empty string keys are possible in PHP, in my case not seeing the warning would be wanted, however the whole list should break. Otherwise the error in the file might get unnoticed. If you give me directions, I'm fine with creating a PR, just in case you wonder.