Skip to content

How to approach: annotation states correct type, trying to check its type inside method body gives error #2854

@desmax

Description

@desmax

Support question

I try to check if correct types where passed to the method, since there is no way to enforce it on language level, only specify via annotations.
Phpstan tells me its not necessary, since I have correct type in annotation.

class Test 
{
	/**
	* @param iterable<SomeInterface> $items
	*/
	public function test(iterable $items): void
	{
		foreach ($items as $item) {
			if (!$item instanceof SomeInterface) {
				throw new \Exception('Unsupported');
			}
		}
	}
}

https://phpstan.org/r/0fdb99ce-cd7e-49fe-8d59-5e57c526c9dc
What would be correct way to approach this?
if I loosen up annotations it might be misleading
if I don't check a type inside method it will fail in not a nice way.

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