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

Add ability to define trait import PHPDoc #90

Closed
InvisibleSmiley opened this issue Sep 21, 2021 · 5 comments
Closed

Add ability to define trait import PHPDoc #90

InvisibleSmiley opened this issue Sep 21, 2021 · 5 comments

Comments

@InvisibleSmiley
Copy link

In order to use generic traits with current versions of PHPStan, PHPDoc with @use must be added to trait imports (use).

AFAICS this generator is currently unable to define PHPDoc comments for trait imports.

FTR the feature was added to PHPStan here:
phpstan/phpstan-src@8766923

@dg
Copy link
Member

dg commented Sep 21, 2021

Can you describe it better?

@InvisibleSmiley
Copy link
Author

Example code:

<?php
$namespace = new \Nette\PhpGenerator\PhpNamespace('Foo');
$class = $namespace->addClass('Bar')->addTrait('SomeTrait');
$file = new \Nette\PhpGenerator\PhpFile();
$file->addNamespace($namespace);
echo (new \Nette\PhpGenerator\Printer())->printFile($file);

Result:

<?php

namespace Foo;

class Bar
{
        use \SomeTrait;
}

Desired result:

<?php

namespace Foo;

class Bar
{
        /** @use \SomeTrait<\Carbon\CarbonImmutable> */
        use \SomeTrait;
}

Trait for use case:

<?php
/** @template T of \DateTimeInterface */
trait SomeTrait
{
    /** @return T */
    public function createDate(): \DateTimeInterface
    {
        return \Carbon\CarbonImmutable::now();
    }
}

@dg
Copy link
Member

dg commented Sep 21, 2021

The ideal would be something like

 $namespace->addClass('Bar')
	->addTrait('SomeTrait')
	->addComment('@use \SomeTrait<\Carbon\CarbonImmutable>');

but addTrait() returns static and to change that would be a big BC break…

Maybe add a new method useTrait()...

@InvisibleSmiley
Copy link
Author

InvisibleSmiley commented Sep 21, 2021

I'd rather say adapt addTrait to accept either a string (behavior like now) or a class that wraps the name and PHPDoc, like so:

$trait = new PhpTrait('SomeTrait')->addComment('@use \SomeTrait<\Carbon\CarbonImmutable>');
$namespace->addClass('Bar')->addTrait($someTrait);

dg added a commit that referenced this issue Sep 21, 2021
dg added a commit that referenced this issue Sep 21, 2021
dg added a commit that referenced this issue Sep 21, 2021
dg added a commit that referenced this issue Sep 21, 2021
@dg
Copy link
Member

dg commented Sep 21, 2021

It's working now. I added the true parameter and then it returns the TraitUse object:

$namespace->addClass('Bar')
	->addTrait('SomeTrait', true)
		->addComment('@use \SomeTrait<\Carbon\CarbonImmutable>');

@dg dg closed this as completed Sep 21, 2021
dg added a commit that referenced this issue Sep 22, 2021
dg added a commit that referenced this issue Sep 22, 2021
dg added a commit that referenced this issue Sep 23, 2021
dg added a commit that referenced this issue Sep 23, 2021
dg added a commit that referenced this issue Sep 23, 2021
dg added a commit that referenced this issue Sep 23, 2021
dg added a commit that referenced this issue Sep 23, 2021
dg added a commit that referenced this issue Sep 23, 2021
dg added a commit that referenced this issue Sep 23, 2021
dg added a commit that referenced this issue Sep 23, 2021
dg added a commit that referenced this issue Sep 23, 2021
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