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

ClassType->getNamespace() should not be deprecated #134

Open
riki137 opened this issue Jul 11, 2023 · 5 comments
Open

ClassType->getNamespace() should not be deprecated #134

riki137 opened this issue Jul 11, 2023 · 5 comments

Comments

@riki137
Copy link

riki137 commented Jul 11, 2023

I can't find a substitution for this piece of code without using the getNamespace() method:

$class = ClassType::from($myClass, true);
$namespace = $class->getNamespace();
$namespace->addUse(DateTimeInterface::class);

Alternative method should at least be mentioned in documention.

@GromNaN
Copy link
Contributor

GromNaN commented Sep 27, 2023

To create a new class, I create the PhpNamespace first.

$namespace = new PhpNamespace('Testing');
$namespace->addUse(\DateTimeInterface::class);
$class = $namespace->addClass('Test');

To create from existing, you can import the PhpFile.

$file = PhpFile::fromCode(file_get_contents('src/'.str_replace('\\', '/', $myClass).'.php));
$namespace = $file->getNamespaces()[0];
$class = $namespace->getClasses()[0];
$namespace->addUse(DateTimeInterface::class);

@riki137
Copy link
Author

riki137 commented Sep 27, 2023

@GromNaN No, i want to use ClassType::from, not PhpFile::fromCode, and also I'd like to avoid [0] when I can be sure that one class will always have one or no namespace.

@GromNaN
Copy link
Contributor

GromNaN commented Oct 8, 2023

What do you do with the $class or $namespace variables after adding the use? Do you save the file again?

@riki137
Copy link
Author

riki137 commented Oct 13, 2023

Ok, let's forget about getting rid of ClassType::from. (but it makes me cry)

If i use $namespace = $file->getNamespaces()[0]; i don't really have a guarantee that the namespace is the namespace of the class, and that the class is the class i want (in case there would be multiple classes and namespaces in the file). I really wouldn't like to manually check this every time i use it.

Also, using [0] indexes makes for a code that is way harder to statically analyze.
I'd have to add some type checks and that would make my code harder to read.

I really don't see the benefit of removing this method. It makes things so much harder for my use case.

@spawnia
Copy link

spawnia commented Nov 15, 2023

Seconded, I can't see how I would achieve what I do in the following code easily. It would require way more complexity, passing around the namespace with the ClassType object perhaps?

https://github.com/spawnia/sailor/blob/50b23706b64ac1134c9b1b90fc7e24b271d63f07/src/Codegen/Generator.php#L82

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

3 participants