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

v3.4.0 ProxyGenerator Warning: Undefined array key #982

Closed
forgie1 opened this issue Aug 26, 2022 · 7 comments
Closed

v3.4.0 ProxyGenerator Warning: Undefined array key #982

forgie1 opened this issue Aug 26, 2022 · 7 comments

Comments

@forgie1
Copy link

forgie1 commented Aug 26, 2022

doctrine/common version: 3.4.0
PHP version: 8.1.1

This code

$qb = $this->getEntityManager()->createQueryBuilder();
$qb->from(EntityName::class), 'n', null);
$qb->select('n, nd');
$qb->join('n.someJoinName', 'nd')
	->where('nd.language = :lang')->setParameter('lang', $lang)
	->andWhere('nd.nid = :nid')->setParameter('nid', $nid);
$qb->getQuery()->getOneOrNullResult();

Results to following warning in \vendor\doctrine\common\src\Proxy\ProxyGenerator.php:1131:

Warning
Undefined array key 1

This didn't happen in version 3.3.1

@malarzm
Copy link
Member

malarzm commented Aug 26, 2022

@forgie1 line 1131 sounds weird as that's just phpdoc

* @param ReflectionParameter[] $parameters

I guess the problem lies somehow in line 1125

$value = rtrim(substr(explode('$' . $parameter->getName() . ' = ', (string) $parameter, 2)[1], 0, -2));
as that's the new code version 3.4.0 introduced. Sadly the code you've pasted tells me nothing about the root cause, one of your entity's methods must have a default value for parameter we fail to support correctly. Could you please check which one is it? Also a PR with a failing test case would help us a lot. For reference how to write such tests you can take a look here: #966

@malarzm
Copy link
Member

malarzm commented Aug 27, 2022

Oops, I've checked current branch writing above, indeed the line is correct in tagged version: https://github.com/doctrine/common/blob/3.4.0/src/Proxy/ProxyGenerator.php#L1131. Still, we'd greatly appreciate a reproducer

@forgie1
Copy link
Author

forgie1 commented Aug 27, 2022

@malarzm it looks like it fails on this parameter:

	/**
	 * @var Url|null
	 * @ORM\ManyToOne(targetEntity="Url")
	 * @ORM\JoinColumn(nullable=true, onDelete="RESTRICT")
	 */
	private $alias;

Setter for our parameter named alias has null default value:

	/**
	 * @param null|Url $alias
	 * @return $this
	 */
	public function setAlias(Url $alias = null)
	{
            ...

$parameter passed to ProxyGenerator::getParameterDefaultValue(ReflectionParameter $parameter) is:

Nette\Reflection\Parameter
    function: 
        array
            0 => 'Plugins\Urls\Models\Url'
            1 => 'setAlias'
    name: 'alias'

Result of explode('$' . $parameter->getName() . ' = ', (string) $parameter, 2) is

array
    0 => '$alias in Plugins\Urls\Models\Url::setAlias()'

So there is really no array key 1.

To prepare failing test I would have to delve more into the Doctrine Common.

@malarzm
Copy link
Member

malarzm commented Aug 28, 2022

This seems weird, reflection's string representation is correct: https://3v4l.org/qGBdg#v8.1.9.

The thing that stands out to me, why do you have an instance of Nette\Reflection\Parameter passed instead of native reflection?

@malarzm
Copy link
Member

malarzm commented Aug 28, 2022

Having Nette's reflection is exactly the culprit as it has its own __toString method: https://github.com/nette/reflection/blob/master/src/Reflection/Parameter.php#L86-L89

@malarzm
Copy link
Member

malarzm commented Aug 28, 2022

To elaborate a bit: \ReflectionParameter::__toString is used so we can support https://wiki.php.net/rfc/new_in_initializers and there's really no other way to do that. Given https://github.com/nette/reflection is discontinued and last release was in 2017 I'm inclined to say we will not provide extra support for it.

@forgie1
Copy link
Author

forgie1 commented Aug 28, 2022

@malarzm you are right, this was the problem
Thank you for your support.

@forgie1 forgie1 closed this as not planned Won't fix, can't repro, duplicate, stale Aug 28, 2022
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