fix #5428 only read the stream contents once
#5428 an attempt at a better fix
#5428 PSR compliant
#5428 use stream_filter_remove as there was a hint about it in the code. Also added unit test
Merge pull request #5570 from jeremyquinton/issue-5428-fix

fix #5428 only read the stream contents once
[#5570] CS and test fixes

- Braces, whitespace.
- Incorrect variable references in tests caused them to fail; fixing them makes
  them pass.
Merge branch 'hotfix/5570'

Close #5570