Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Method Zend\Code\Generator\ValueGenerator::__toString() must not throw an exception (with PHP 7.3) #178

Closed
k00ni opened this issue Dec 9, 2019 · 6 comments · Fixed by #179
Labels

Comments

@k00ni
Copy link

k00ni commented Dec 9, 2019

The class ValueGenerator may throw an exception inside __toString which is forbidden (look for Exceptions can not be thrown from __toString() methods.) and leads to a fatal error in my case. I am running a Symfony application, which indirectly uses zend-code.

Are you aware of this? I would propose moving the functionality in a separate function or not throw an exception in __toString (or referenced functions) at all.

Thank you for the feedback.

@michalbundyra
Copy link
Member

@k00ni Since PHP 7.4 we can throw exceptions in __toString() method - see:
https://www.php.net/manual/en/migration74.new-features.php#migration74.new-features.core.tostring-exceptions

Can you provide more details about your use case when you are getting the exception? Thanks!

@k00ni
Copy link
Author

k00ni commented Dec 9, 2019

Hi @michalbundyra, thank you for the quick reply.

Since PHP 7.4 we can throw exceptions in __toString() method

Ok, good to know :)

Can you provide more details about your use case when you are getting the exception?

I am running PHP 7.3, no xdebug.

My use case involves:

  • Symfony 4.4.1
  • Doctrine ORM 2.6.6
  • PHPUnit 8.x

When running a simple test which requires the Kernel and Doctrine, i get the following error:

Fatal error: Method Zend\Code\Generator\ValueGenerator::__toString() must not throw an 
exception, caught ErrorException: Warning: get_class() expects parameter 1 to be object, 
resource given in 
/var/www/html/vendor/zendframework/zend-code/src/Generator/ParameterGenerator.php on line 0
!!  PHP Fatal error:  Method Zend\Code\Generator\ValueGenerator::__toString() must not 
throw an exception, caught ErrorException: Warning: get_class() expects parameter 1 to 
be object, resource given in 
/var/www/html/vendor/zendframework/zend-code/src/Generator/ParameterGenerator.php on line 0
!!  Symfony\Component\ErrorHandler\Error\FatalError {#1977
!!    -error: array:4 [
!!      "type" => 1
!!      "message" => "Method Zend\Code\Generator\ValueGenerator::__toString() must not
throw an exception, caught ErrorException: Warning: get_class() expects parameter 1 to be 
object, resource given"
!!      "file" => "/var/www/html/vendor/zendframework/zend-code/src/Generator/ParameterGenerator.php"
!!      "line" => 0
!!    ]
!!    #message: "Error: Method Zend\Code\Generator\ValueGenerator::__toString() must 
not throw an exception, caught ErrorException: Warning: get_class() expects parameter 
1 to be object, resource given"
!!    #code: 0
!!    #file: "./vendor/zendframework/zend-code/src/Generator/ParameterGenerator.php"
!!    #line: 0
!!  }

I am not familiar with Symfony, Doctrine or Zend internals, therefore can't located the source of the error. I am not even sure, if that is "your" problem, but i thought its a good start to ask the first one in the line.


EDIT: It must be related to symfony/proxy-manager-bridge, without it installed, i am not getting the error.

@k00ni k00ni changed the title Method Zend\Code\Generator\ValueGenerator::__toString() must not throw an exception Method Zend\Code\Generator\ValueGenerator::__toString() must not throw an exception (with PHP 7.3) Dec 9, 2019
@k00ni
Copy link
Author

k00ni commented Dec 10, 2019

I moved on to another solution for my problem, which means, i don't get this error anymore. But it maybe still of interest to you. Please close, if not.

@michalbundyra
Copy link
Member

@k00ni I see the issue - in case value is resource in the exception message we try to get_class of resource, which is not working:

case self::TYPE_OTHER:
default:
throw new Exception\RuntimeException(
sprintf('Type "%s" is unknown or cannot be used as property default value.', get_class($value))
);

Still, seems to be that your code in some place is (was?) invalid.

Would you be able to share your solution which solve your problem, please?

@k00ni
Copy link
Author

k00ni commented Dec 10, 2019

I had problems getting PHPUnit 8.x + Doctrine 2.7 running with Symfony 4.4.1. There was no PHP code involved, only YAML configuration.

After hours of searching it resulted in the following solution: symfony/symfony-docs#12788 (comment) .

I got the error above when trying to setup Doctrine to use entity manager in PHPUnit integration tests. Unfortunately, i can't find my configuration at that time anymore. I am still new to this and its hard to track all these changes and new features/components.

@k00ni
Copy link
Author

k00ni commented Dec 11, 2019

Thank you for the refinements.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants