'PHPStan Extension: Dynamic Return Types with Value Objects
Some libraries (like Doctrine) use simple stringable value-objects, like Func:
<?php
class Func {
protected $name;
protected $arguments;
public function __construct($name, $arguments) {
$this->name = $name;
$this->arguments = $arguments;
}
public function __toString() {
return $this->name . '(' . implode(', ', $this->arguments) . ')';
}
}
$min = new Func('MIN', ['my_field']);
echo $min; // MIN(my_field)
Would it be possible for PHPStan to use a Conditional Return Type for the __toString()
method, something like:
/**
* @return ($this->value is literal-string ? literal-string : string)
*/
If not, maybe a Dynamic Return Type could work? I have used DynamicMethodReturnTypeExtension::getTypeFromMethodCall()
before, but I'm not sure if it can remember the values that have gone into the object, either during __construct()
or via other methods.
This relates to the PHPStan Doctrine Extension, where my intention is for __toString()
to return the literal-string
type when all the inputs to the value-object are literal-string
, otherwise return a string
.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|