Skip to content

Add ability to define trait import PHPDoc #90

Closed

Description

@InvisibleSmiley

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

Activity

  1. dg commented on Sep 21, 2021

    @dg
    Member

    Can you describe it better?

  2. InvisibleSmiley commented on Sep 21, 2021

    @InvisibleSmiley
    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();
        }
    }
  3. dg commented on Sep 21, 2021

    @dg
    Member

    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()...

  4. InvisibleSmiley commented on Sep 21, 2021

    @InvisibleSmiley
    Author

    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);
  5. added 4 commits that reference this issue on Sep 21, 2021
    2e3e9d7
    f3fe6e5
    34d0c0e
    ae2b64c
  6. dg commented on Sep 21, 2021

    @dg
    Member

    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>');
  7. added 11 commits that reference this issue on Sep 22, 2021
    0e7e749
    5f82948
    2c15d73
    34b2505
    72f6256
    0650340
    e129449
    8dae28a
    57be48f
    34d94e1
    1170323
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions