Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect error issued for return value type. #17

Closed
Techget opened this issue Jan 3, 2018 · 5 comments
Closed

Incorrect error issued for return value type. #17

Techget opened this issue Jan 3, 2018 · 5 comments

Comments

@Techget
Copy link

Techget commented Jan 3, 2018

Hello, I meet some weired error when I use opis closure to serialize&unserialize pimple container, after unserializing, it seems causing some ambiguity to the return value type.

  1. Currently, it can't determine the return type correctly after unserializing, for example:
function (Container $container) use ($class, $args, $stacktraces): FormatterInterface {
    $formatter = newv($class, $args); // newv is a function to create new class
    if ($stacktraces !== null) {
        $formatter->includeStacktraces($stacktraces);
    }
    return $formatter;
}

In above closure, the formatter is going to be a class extends from basicFormatter, and basicFormatter implements the FormatterInterface, so the return value will be an instance of FormatterInterface however I get error message as following:

Fatal error: Uncaught TypeError: Return value of Opis\Closure\SerializableClosure::{closure}() must be an instance of FormatterInterface, instance of JsonFormatter returned in closure://function (\Pimple\Container $container) use ($class, $args, $stacktraces): FormatterInterface {
            $formatter = newv($class, $args);
            if ($stacktraces !== null) {
                $formatter->includeStacktraces($stacktraces);
            }
            return $formatter;
        }:
  1. It does not compatible with alias in use statement, for example:
use Aws\S3\S3Client as AwsS3Client;
$container['AwsS3Client'] = function (Container $container) use ($config): AwsS3Client {
      return $container['AwsSdk']->createS3($config);
};

Will get error message as message":"Return value of Opis\\Closure\\SerializableClosure::{closure}() must be an instance of AwsS3Client, instance of Aws\\S3\\S3Client returned","code":500,

Please help when you get time. Thanks in advance.

@msarca
Copy link
Member

msarca commented Jan 3, 2018

Actually, Opis Closure is returning the correct type, instance of Aws\\S3\\S3Client returned, but you expect an instance of AwsS3Client, which is wrong. Try using something like below

use Aws\S3\S3Client as AwsS3Client;
$container[AwsS3Client::class] = function (Container $container) use ($config): AwsS3Client {
      return $container['AwsSdk']->createS3($config);
};

@Techget
Copy link
Author

Techget commented Jan 3, 2018

Thanks for your response, I tried your solution, doesn't work, I think there is some misunderstanding, Instance of Aws\\S3\\S3Client returned is correct but AwsS3Client returned should also be correct, since as you can see in above use case, AwsS3Client is an alias of Aws\\S3\\S3Client.

And how about the first problem, it can't determine the type correctly, let me give you an simplified example:

class A implements interfaceA {}
class B extends A {}
$closure = function(): interfaceA {
   return new B();   // after unserializing, it complains that this closure doesn't return an instance of interface A
}

Could you please also have a look on this problem, this really bites me.

@msarca
Copy link
Member

msarca commented Jan 4, 2018

Sorry, but I can't reproduce this.It will be much more easy if you can provide me a full example, not just pieces of code. The most simple and reproducible example you can provide. Also, please specify which version of PHP, Opis Closure and Pimple are you using. Thanks.

@msarca
Copy link
Member

msarca commented Jan 4, 2018

Finally, I was able to reproduce the error. I will provide a fix asap.

@msarca
Copy link
Member

msarca commented Jan 4, 2018

This should be fixed in v3.0.9

@msarca msarca closed this as completed Jan 4, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants