Skip to content

Unable to validate function arguments because of "strict" phpdoc #1991

@grongor

Description

@grongor

https://phpstan.org/r/3be3170f-9b1b-401a-b57f-cf90907850f4

I think that I get why phpstan reports the issue but I don't think it's correct.

I want to document that the only accepted values of the function are an instance of Bar or a string. At the same time I obviously want to validate that the passed arguments are of the required types (I don't want to rely on a "good user behavior"), but according to phpstan I'm not able to do that.

Am I missing something or is this a bug? Any suggestions on how to go around it? Or do you suggest that such validation is not neccessary and that I should rely on the phpstan to check the arguments somewhere where the function is called?

/**
 * @param Bar|string $a
 */
function foo ($a) : bool {
	if ($a instanceof Bar) {
		return true;
	} elseif (!is_string($a)) {
		throw new \Exception('Not Bar or string');
	}
	
	return false;
}

foo(123); // I expect an exception here...

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions