zendframework / zend-code

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

PropertyGenerator::setDefaultValue incorrectly handles ValueGenerator instances

adamlundrigan opened this issue · comments

Related to #96, PropertyGenerator behaves incorrectly when the default value passed into it is a ValueGenerator instead of a PropertyValueGenerator.

Example (lifted from #96):

<?php
<<<CONFIG
packages:
    - "zendframework/zend-code: ^3.0"
CONFIG;

$classGenerator    = new \Zend\Code\Generator\ClassGenerator();
$propertyGenerator = new \Zend\Code\Generator\PropertyGenerator();
$value             = new \Zend\Code\Generator\ValueGenerator();
$value->setValue('DefaultString');
$value->setType(\Zend\Code\Generator\ValueGenerator::TYPE_STRING);
$propertyGenerator->setName("foo");
$propertyGenerator->setDefaultValue($value->generate());

$classGenerator->addPropertyFromGenerator($propertyGenerator);

print $classGenerator->generate();

Will print this:

class 
{

    public $foo = '\'DefaultString\'';


}

PropertyGenerator::setDefaultValue wraps everything that isn't a PropertyValueGenerator instance in a new instance of that class. IMO setDefaultValue should not accept ValueGenerator instances or at least convert to a PropertyValueGenerator, vs composing one into the other as is presently the case. This change does break BC, however.

This repository has been closed and moved to laminas/laminas-code; a new issue has been opened at laminas/laminas-code#16.