From: Timo Tijhof Date: Wed, 10 Oct 2018 19:17:37 +0000 (+0100) Subject: Minor OrderedStreamingForkController improvements X-Git-Tag: 1.34.0-rc.0~3829^2 X-Git-Url: https://git.cyclocoop.org/%7B%24www_url%7Dadmin/compta/banques/%7B%7B%20url_for%28%27admin_users%27%29%20%7D%7D?a=commitdiff_plain;h=5a7670e42693e312d14e702e26d07709cd291b3f;p=lhc%2Fweb%2Fwiklou.git Minor OrderedStreamingForkController improvements Follows-up c94dea7029cae. * Avoid strlen() where a strict check suffices. * Use substr() for both comparison and stripping, avoid potentially expensive, but also fragile, because strlen()-1 can produce $str[-1], which would > PHP Notice: Uninitialized string offset: -1 Luckily, the implied null would work as expected, given that `null !== "\n"` and produces the same result. Change-Id: If61e5e412aaa2dc7c00c4441b3b7bd5f04160ec8 --- diff --git a/includes/OrderedStreamingForkController.php b/includes/OrderedStreamingForkController.php index 11abc81c1f..5424b13285 100644 --- a/includes/OrderedStreamingForkController.php +++ b/includes/OrderedStreamingForkController.php @@ -135,13 +135,16 @@ class OrderedStreamingForkController extends ForkController { protected function consumeNoFork() { while ( !feof( $this->input ) ) { $data = fgets( $this->input ); - if ( $data[ strlen( $data ) - 1 ] == "\n" ) { + if ( substr( $data, -1 ) === "\n" ) { + // Strip any final new line used to delimit lines of input. + // The last line of input might not have it, though. $data = substr( $data, 0, -1 ); } - if ( strlen( $data ) !== 0 ) { - $result = call_user_func( $this->workCallback, $data ); - fwrite( $this->output, "$result\n" ); + if ( $data === '' ) { + continue; } + $result = call_user_func( $this->workCallback, $data ); + fwrite( $this->output, "$result\n" ); } } @@ -163,12 +166,12 @@ class OrderedStreamingForkController extends ForkController { $this->updateAvailableSockets( $sockets, $used, $sockets ? 0 : 5 ); } while ( !$sockets ); } - // Strip the trailing \n. The last line of a file might not have a trailing - // \n though - if ( $data[ strlen( $data ) - 1 ] == "\n" ) { + if ( substr( $data, - 1 ) === "\n" ) { + // Strip any final new line used to delimit lines of input. + // The last line of input might not have it, though. $data = substr( $data, 0, -1 ); } - if ( strlen( $data ) === 0 ) { + if ( $data === '' ) { continue; } $socket = array_pop( $sockets );