nette / php-generator

🐘 Generates neat PHP code for you. Supports new PHP 8.3 features.
https://doc.nette.org/php-generator
Other
2.11k stars 138 forks source link

Breaking change within minor version bump RE: namespaces #145

Closed Westie closed 11 months ago

Westie commented 11 months ago

Hello

I'm working on a project that uses anonymous classes with namespaces.

According to the Nette documentation, here's how this software package handles anonymous classes:

Unfortunately it appears impossible to add an anonymous class to a file (and understandably so - where do you even start!)

However, in commit bcb74f3 a breaking change was introduced which meant that any attempts to generate a namespace definition, where that definition did not have any linked functions or classes, was to be omitted.

I've worked around this now, using the flag Printer::$omitEmptyNamespaces that was added as part of that commit, but this still leaves me in a position where I don't know if there'll be a future update that would change this behaviour in future.

I was wondering what the community's thoughts on this were?

dg commented 11 months ago

I don't understand what you're trying to accomplish. What are the anonymous functions with namespaces? How do them relate to anonymous classes? Can you give a specific example?

Westie commented 11 months ago

What are the anonymous functions with namespaces

This is a brainfart, apologies.

As following the rest of the tone of this issue, It should read:

anonymous classes with namespaces

Westie commented 11 months ago

A real world example would be this:

namespace X\Y;

use V\A;
use V\B;

return class extends A {
    function __construct(B $b) {
        ...
    }
}

Within Nette, there appears so far to be no way as to clean up generated anonymous classes. How I have to do it, is by creating an instance of PhpFile, create an instance of PhpNamespace and assign that to the file, associate any class or function references I want aliased, then print the file and the anonymous class manually and hope that they match up.

There is no link between the anonymous class and the namespace (or more specifically, the file) which is understandable.

dg commented 11 months ago

Your example, that's global code that this library never supported. It only allows you to define classes and functions in namespaces. So I don't understand what the breaking change is. Did something stop working for you?

Westie commented 11 months ago

that's global code that this library never supported

Personally I wouldn't say that it is global code, as in my eyes it is an anonymous class that has been defined within the context of a namespace.

But, having thought about it I believe I can see (and I think more importantly, follow) why it is that your opinion is such, so that would explain the current behaviour.

I don't understand what the breaking change is

The breaking change is the addition of $omitEmptyNamespaces to the Printer - as the anonymous class cannot be associated with the namespace within this library (as it is by definition, not a class with a name) - any attempt to print the namespace will result in an empty namespace definition being generated.

Did something stop working for you

Yes, that is correct. With $omitEmptyNamespaces being set to its default value of true, the Printer detected that there were no functions nor classes associated with that namespace, then decided to not print it.

dg commented 11 months ago

I'm sorry, but I don't understand what you want. If there is a bug, please post the code here that used to work and now doesn't. If the problem is that $omitEmptyNamespaces is true, change it to false, it's a public variable. Code outside of functions and classes is not (yet) supported by this library.

Westie commented 11 months ago

Code outside of functions and classes is not (yet) supported by this library

I'll therefore deem it as not a bug then, just a rather quirky feature gap.