Skip to content

Commit

Permalink
Prevent PHP Notice errors on fwrite failure.
Browse files Browse the repository at this point in the history
  • Loading branch information
kicken committed Jun 25, 2017
1 parent c4afd01 commit fb5b480
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/Protocol/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,9 @@ private function write($data, $timeout){
$start = (int)(microtime(true) * 1000);
$remaining = strlen($data);
do {
$this->pushErrorHandler();
$written = fwrite($this->stream, $data);
$this->popErrorHandler();
$end = (int)(microtime(true) * 1000);
if ($written === 0){
if ($timeout !== false && $end - $start >= $timeout){
Expand Down Expand Up @@ -207,4 +209,19 @@ private function tryServer($uri, $timeout){

return $stream;
}

private function pushErrorHandler(){
/** @noinspection PhpUnusedLocalVariableInspection */
$previousErrorHandler = set_error_handler(function($errno, $errstr) use (&$previousErrorHandler){
if (preg_match('/errno=\d+/', $errstr, $matches)){
return true;
} else {
return call_user_func_array($previousErrorHandler, func_get_args());
}
}, E_NOTICE);
}

private function popErrorHandler(){
restore_error_handler();
}
}

0 comments on commit fb5b480

Please sign in to comment.