Skip to content
This repository

dataAvailable method backported to 2.4 #1098

Closed
wants to merge 7 commits into from

4 participants

Rik van der Heijden Mark Mark Story ceeram
Rik van der Heijden

I already did this for 3.0 earlier, now for 2.4

lib/Cake/Console/ConsoleInput.php
@@ -48,4 +48,16 @@ public function read() {
48 48 return fgets($this->_input);
49 49 }
50 50
  51 +/**
  52 + * Check if data is available on stdin
  53 + *
  54 + * @param integer $timeout An optional time to wait for data
  55 + * @return bool True for data available, false otherwise
  56 + */
  57 + public function dataAvailable($timeout = 0) {
  58 + $readFds = array($this->_input);
  59 + $readyFds = stream_select($readFds, $w = null, $e = null, $timeout);
1
Mark Story Owner

Inline assignments in function calls always look like a mistake. You should assign these variables above the function call and pass them in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Rik van der Heijden

@markstory See pull request #1020 ;-)

Mark
Collaborator

I don't think the initialization is even necessary.
In PHP you can just do stream_select($readFds, $writeFds, $errorFds, $timeout);.
$writeFds, $errorFds will automatically be initialized here as they are merely return variables.

A (small) flaw in PHP itself one could say.

Mark Story
Owner

Well I made a mistake last time, these things happen. I'll fix the code in 3.0.

Rik van der Heijden

I think it the initialization is necessary because these parameters are also a input to this function, by passing uninitialized variables you don't actually know what happens and PHP throws a warning.

From: http://php.net/manual/en/function.stream-select.php

e.g. write: 'The streams listed in the write array will be watched to see if a write will not block.'

which means that (even though its a reference) it still is input.

Mark
Collaborator

Interesting, I only know if from exec() etc where you don't need to do that. But maybe this method behaves differently.

Mark Story
Owner

Based on my local testing, this method like most of the gross C API's in PHP don't need the variables initialized.

<?php
error_reporting(-1);
$fds = array(fopen('php://stdin', 'r'));
$ready = stream_select($fds, $write, $error, 0);

Works fine on my machine without any notice errors.

Rik van der Heijden

I still don't like it, when the PHP interpreter changes this may give problems, just relying on the default of the interpreter is not the best way to go in my opinion.

Mark
Collaborator

This behavior is quite unlikely to ever change :)

Rik van der Heijden

@dereuromark, Yeah I think your right. Still I think its better to init imo. BUT now we saved 2 lines of code!

Mark
Collaborator

You still need to squash, though.

Rik van der Heijden

@dereuromark what do you mean?

Mark
Collaborator

those 5 commits should be merged into a single one by rebasing it upstream and squashing them into a single commit.

ceeram
Collaborator

@markstory except that phpcs will complain about Unused variable.

Mark Story
Owner

@ceeram That's a separate problem, I can take care of that after the merge.

djbobke added some commits
Rik van der Heijden djbobke Backport dataAvailable to 2.4 51fdfc6
Rik van der Heijden djbobke Merge branch '2.4' of github.com:djbobke/cakephp into 2.4
* '2.4' of github.com:djbobke/cakephp:
  Remove variable initialization
537a144
Rik van der Heijden

See #1112 :)

Mark Story
Owner

See #1112

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 12 additions and 0 deletions. Show diff stats Hide diff stats

  1. +12 0 lib/Cake/Console/ConsoleInput.php
12 lib/Cake/Console/ConsoleInput.php
@@ -48,4 +48,16 @@ public function read() {
48 48 return fgets($this->_input);
49 49 }
50 50
  51 +/**
  52 + * Checks if data is available on the stream
  53 + *
  54 + * @param integer $timeout An optional time to wait for data
  55 + * @return bool True for data available, false otherwise
  56 + */
  57 + public function dataAvailable($timeout = 0) {
  58 + $readFds = array($this->_input);
  59 + $readyFds = stream_select($readFds, $writeFds, $errorFds, $timeout);
  60 + return ($readyFds > 0);
  61 + }
  62 +
51 63 }

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.