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

Proposal: Adding previously created objects to existing ones #35

Closed
MCStreetguy opened this issue Jul 31, 2018 · 0 comments
Closed

Proposal: Adding previously created objects to existing ones #35

MCStreetguy opened this issue Jul 31, 2018 · 0 comments

Comments

@MCStreetguy
Copy link

Hey there,
I currently use this awesome library to regenerate existing classes with some modifications.
For this reason I'm often forced to to something like this:

$target = ClassType::from($targetClass);

$methods = $target->getMethods();
$newMethods = [];

foreach ($methods as $method) {
    # do some work
    $newMethods[] = $someNewMethod;
}

$target->setMethods(array_merge($methods, $newMethods));

It would be really nice if it would be possible to directly add existing Method or Property instances to an ClassType instance, instead of using this workaround. Currently it's only possible to create new methods or properties through their respective add...() methods as far as I know.
I imagine something like this:

$target = ClassType::from($targetClass);

foreach ($target->getMethods() as $method) {
    # do some work
    $target->addExistingMethod($someNewMethod);
}

Even worse is the process of adding ClassType objects to a PhpFile. For that purpose I had to write a helper method looking like this:

public static function addClassFromTemplate(PhpNamespace &$target, ClassType $tpl): ClassType
{
    return $target->addClass($tpl->getName())
                ->setType($tpl->getType())
                ->setFinal($tpl->isFinal())
                ->setAbstract($tpl->isAbstract())
                ->setExtends($tpl->getExtends())
                ->setImplements($tpl->getImplements())
                ->setTraits($tpl->getTraits())
                ->setConstants($tpl->getConstants())
                ->setProperties($tpl->getProperties())
                ->setMethods($tpl->getMethods())
                ->setComment($tpl->getComment());
}

In this case, it would be even more helpful to add the class with a one-liner.
Let me know what you think about my proposal.

Greetings, MCStreetguy

@dg dg closed this as completed in 1787a10 Aug 6, 2018
dg added a commit that referenced this issue Aug 6, 2018
dg added a commit that referenced this issue Aug 6, 2018
dg added a commit that referenced this issue Aug 6, 2018
dg added a commit that referenced this issue Aug 6, 2018
dg added a commit that referenced this issue Aug 9, 2018
dg added a commit that referenced this issue Aug 9, 2018
dg added a commit that referenced this issue Aug 9, 2018
dg added a commit that referenced this issue Aug 29, 2018
dg added a commit that referenced this issue Aug 29, 2018
dg added a commit that referenced this issue Aug 29, 2018
dg added a commit that referenced this issue Aug 29, 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

1 participant