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

Template: (foreach Collection<UserModel> as $user) being typed as T instead of UserModel #4556

Closed
gabrieltosetti opened this issue Sep 28, 2021 · 0 comments · Fixed by #4557
Assignees
Labels
enhancement This improves the quality of Phan's analysis of a codebase

Comments

@gabrieltosetti
Copy link

Phan 5.2.0
php-ast version 1.0.14
PHP version used to run Phan: 7.2.34

Hello!
I'm using Lumen and the last few days I've been documenting the return type of base model and collection for better analysis but I can't get this Template to work.

The goal is the teach phan what a Collection has inside of it. In resume, here is the example i wrote:

<?php

class UserModel {
    public $id;
    public function __construct(int $id)
    {
        $this->id = $id;
    }
}

/**
 * @template T
 */
class Collection implements \IteratorAggregate {
    /**
     * @var T[]
     */
    public $modelArray;
    
    /**
     * @param T[] $modelArray
     */
    public function __construct($modelArray = [])
    {
        $this->modelArray = $modelArray;
    }

    /**
     * @return \Iterator<int, T>
     */
    function getIterator()
    {
        return new \ArrayIterator($this->modelArray);
    }
}

class UserRepository {
    /**
     * @return Collection<UserModel>
     */
    public static function findByString(string $value): Collection
    {
        return new Collection([0 => new UserModel(0), 1 => new UserModel(1)]);
    }
}


$users = UserRepository::findByString('something');

'@phan-debug-var $users';
foreach ($users as $key => $user) {
    '@phan-debug-var $key, $user';
    echo $user->id . "\n";
}

Link to Phan Demo

The analisis output:

input:48: PhanDebugAnnotation @phan-debug-var requested for variable $users - it has union type \Collection<\UserModel>(real=\Collection)

input:51: PhanDebugAnnotation @phan-debug-var requested for variable $key - it has union type int(real=int)

input:51: PhanDebugAnnotation @phan-debug-var requested for variable $user - it has union type T(real=T)

Why $user is beeing typed as T?

@TysonAndre TysonAndre self-assigned this Sep 29, 2021
@TysonAndre TysonAndre added the enhancement This improves the quality of Phan's analysis of a codebase label Sep 29, 2021
TysonAndre added a commit to TysonAndre/phan that referenced this issue Sep 29, 2021
(the template type was not getting converted to a real type for a
IteratorAggregate that includes a template.

Closes phan#4556
TysonAndre added a commit to TysonAndre/phan that referenced this issue Sep 29, 2021
(the template type was not getting converted to a real type when
the return value of getIterator included a template

Closes phan#4556
TysonAndre added a commit to TysonAndre/phan that referenced this issue Sep 29, 2021
The template type was not getting converted to a real type when
the return value of getIterator included a template.

Closes phan#4556
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This improves the quality of Phan's analysis of a codebase
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants