ReactPHP get_class() expects parameter 1 to be object, string given - php

I am getting this error code:
PHP Warning: get_class() expects parameter 1 to be object, string
given in /phalcon/vendor/clue/block-react/src/functions.php on line 90
PHP Fatal error: Uncaught UnexpectedValueException: Promise rejected
with unexpected value of type in
/phalcon/vendor/clue/block-react/src/functions.php:89 Stack trace:
0 /phalcon/vendor/clue/block-react/src/functions.php(198): Clue\React\Block\await(NULL, Object(React\EventLoop\StreamSelectLoop),
NULL)
1 /phalcon/app/tasks/RunTask.php(96): Clue\React\Block\awaitAll(NULL, Object(React\EventLoop\StreamSelectLoop))
I'm building my promises with the following code:
$promises[] = $this->getTrades($rule->id, $exchange->id)
->then(
function ($trades) use ($handleTrades) {
foreach ($trades as $trade) {
$handleTrades[] = $trade;
}
}
);
The function is like this:
private function getTrades($rule, $exchange) {
$deferred = new React\Promise\Deferred();
//...
if (empty($trades->count())) {
$deferred->reject("No trades found for rule $rule");
} else {
$deferred->resolve($trades);
}
return $deferred->promise();
}
How do I solve this?

The real reason is Fatal error: Uncaught UnexpectedValueException, not the warning. As you can see the clue/reactphp-block library expects an Exception in your reject function. Try:
$deferred->reject(new \Exception("No trades found for rule $rule"));

As Furgas mentioned above I needed to add the error handling function in the promise.

Related

How to reference a Class Method using an array variable in PHP7?

I've moved from PHP5 to PHP7.
The following code no longer works.
<?php
class sandbox {
function doit() {
$method = array('name' => 'testresponse');
return $this->$method['name']();
}
function testresponse(){
return "Hi!";
}
}
$h = new sandbox();
echo "Hello, " . $h->doit();
I'm wondering what is the new syntax for this?
Here are the PHP errors I'm getting
A PHP Error was encountered Severity: Notice
Severity: Notice
Message: Array to string conversion
Filename: front/sandbox.php
Line Number: 20
And
Fatal error: Uncaught Error: Function name must be a string in /var/www/application/controller/sandbox.php:20 Stack trace: #0
Change this
return $this->$method['name']();
to this
return $this->{$method['name']}();

Fatal error: Uncaught ArgumentCountError: Too few arguments to function Class::__construct(), 0 passed in and at least 1 expected in PHP7.1

I am learning PHP but I encountered a new problem. I would appreciate if you help.
My code:
$idTrack = (new Track_Obj)->getIdtCommon($a,$b);
Class Track_Obj{
function __construct( $idtakip, $env = false ) {...}
function getIdtCommon( $idref, $idstudent, $env = false) {...}
}
Error:
Uncaught ArgumentCountError: Too few arguments to function Track_Obj::__construct(), 0 passed in and at least 1 expected in PHP7.1
Solve:
New code:
$idTrack = (new Track_Obj($idtakip))->getIdtCommon($a,$b);
or
$class=new Track_Obj($idtakip);
$idTrack = $class->getIdtCommon($a,$b);
Thank you for help and explanation #Phil

Nullable type throws error "Must be an instance of Exception"

After adding ?string type to the function paramerers it throws me an error.
Error:
Type error: Argument 1 passed to
Whoops\Exception\Inspector::__construct() must be an instance of
Exception, instance of ParseError given, called in
/var/www/website/vendor/filp/whoops/src/Whoops/Exception/Inspector.php
on line 80
Code:
public function getPayments(?string $searchKey = '', $searchByColumn = "id")
{
return $this->model->where($searchByColumn, '=', $searchKey)->get();
}
I am using laravel4 with php7. What can I do about it?

How to Unit Test Type Hint with PHPUnit

I want to test this method:
Class:
public function bind(\Elastica\ResultSet $result = null) {
if (!$result instanceof \Elastica\ResultSet) {
throw new \InvalidArgumentException('I need an instance of \Elastica\ResultSet');
}
$this->bindedData = $result->getResults();
$this->isBinded = true;
}
Test
public function testGetTransformedDataNotSuccesful() {
$this->object->bind(new \stdClass()); //This throws a Catchable fatal error
}
My question is:
How can i test this?
An alternative is not to Type Hint the method var.
Or shouldn't i test this.
Wouldn't it make sense that PHP throws an exception instead of throwing a fatal error ?
Throwing a fatal error is correct, as your method signature explicitly asks for a \Elastica\ResultSet but you provide an \stdClass.
Removing the typehint would also remove the fatal error - but that doesn't make much sense imho :)
edit
This test should pass
public function testGetTransformedDataNotSuccesful() {
$this->setExpectedException(get_class(new PHPUnit_Framework_Error("",0,"",1)));
$this->object->bind(new \stdClass()); //This throws a Catchable fatal error
}

How to handle erroneous argument type in this situation?

I'd like to use stdClass to store options for some methods, instead of passing huge lists of variables (inspired by javascript-style coding)
However, I'd like to make sure I'm always getting an instance of stdClass as an argument. I know I can add a hint in the argument (gb::search below) but when I deliberately try to break it, I'm not sure how to handle the error.
Any tips?
class gb extends CI_Model {
protected $searchtypes = array('full','partial');
protected $endpoint = "https://local.endpoint";
function __construct() {
parent::__construct();
// sample search
$options = new stdClass();
$options->term = 'sample search';
$options->type = 'full';
$this->search($options);
}
function clean_term($term){
$term = trim($term);
return $term;
}
function search(stdClass $options){
$term = $options->term;
$type = $options->type;
// make sure we're doing a valid search
if (!$term || !in_array($type, $this->searchtypes)) {
return false;
}
$term = $this->clean_term($term); // etc
}
The error it throws is something like:
A PHP Error was encountered
Severity: 4096
Message: Argument 1 passed to gb::search() must be an instance of stdClass, null given, called in /application/models/gb.php on line 20 and defined
Filename: models/gb.php
Line Number: 29
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: models/gb.php
Line Number: 31
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: models/gb.php
Line Number: 32
Any ideas how to approach this from a CodeIgniter point of view?
if I remember - mistyped argument should raise E_RECOVERABLE_ERROR, so, it triggers error handler but execution continues. So, you have two options basically.
One is to throw exception in error handler when E_RECOVERABLE_ERROR is encountered. To halt execution.
Another - check type with instanceof stdClass and do what you suppose - raise exception or return something.
UPDATE In your case your framework (CI is for CodeIgniter?) sets error handler (somewhere using set_error_handler). So, after logging or printing error message execution continues. (If there was not handler you would get fatal error). Just test type of argument manually:
function search(stdClass $options){
// test type for sure, because of recoverable error
if (!($options instanceof stdClass)) {
return false; // or throw new InvalidArgumentException('Parameter should be instance of stdClass');
}
$term = $options->term;
$type = $options->type;
// make sure we're doing a valid search
if (!$term || !in_array($type, $this->searchtypes)) {
return false;
}
$term = $this->clean_term($term); // etc
}

Categories