Add support for Assert::oneOf()
greg0ire opened this issue · 6 comments
Now that we can narrow types, it would be quite handy to be able to do this:
/**
* @var string
* @phpstan-var EntityType::TYPE_*
*/
private $inputType;
public function __construct(string $inputType)
{
Assert::oneOf($inputType, EntityType::ALL);
- assert(in_array($inputType, EntityType::ALL, true));
$this->inputType = $inputType;
}What about:
/**
* @param EntityType::TYPE_* $inputType
*/
public function __construct(string $inputType)
{
}?
It'd have the benefit to point out dangerous calls to the constructor...
But then the Assert would be useless wouldn't it? And there are dangerous calls, I want to catch them here, because I'm parsing a config file, and I want to catch mistakes (so my constructor can be called with invalid data, and phpstan will bring no benefit because it can't spot issues in a YAML file)
PHPStan currently doesn't understand Assert::oneOf. Once it does, it will mark it as "always true", which happens only with "strict-rules" installed. You should add the PHPDoc above the constructor anyway. So this is a non-issue for most people.
If you don't want to see the "always true" warning with this check even if you want to use strict-rules, you can change the treatPhpDocTypesAsCertain setting: https://phpstan.org/config-reference#treatphpdoctypesascertain
Indeed I think I am in an edge case here 🤔 thanks for your help!
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.