Allow casts with a return type of static or this to reference themselves (#1103)

* Allow casts with a return type of static or this to reference themselves

Supports

```php
/**
     * @param $model
     * @param string $key
     * @param $value
     * @param array $attributes
     * @return static
     */
    public function get($model, string $key, $value, array $attributes)
    {
        return new static($value);
    }
```

or

```php
/**
     * @param $model
     * @param string $key
     * @param $value
     * @param array $attributes
     * @return $this
     */
    public function get($model, string $key, $value, array $attributes)
    {
        return new static($value);
    }
```

* Update CHANGELOG.md
This commit is contained in:
Matthew Hailwood
2020-12-04 07:33:45 +01:00
committed by GitHub
parent b4138e5122
commit b3a79fe6c1
9 changed files with 118 additions and 4 deletions
+8 -4
View File
@@ -1089,13 +1089,17 @@ class ModelsCommand extends Command
$methodReflection = new \ReflectionMethod($type, 'get');
$type = $this->getReturnTypeFromReflection($methodReflection);
$reflectionType = $this->getReturnTypeFromReflection($methodReflection);
if ($type === null) {
$type = $this->getReturnTypeFromDocBlock($methodReflection);
if ($reflectionType === null) {
$reflectionType = $this->getReturnTypeFromDocBlock($methodReflection);
}
if($reflectionType === 'static' || $reflectionType === '$this') {
$reflectionType = $type;
}
return $type;
return $reflectionType;
}
protected function getTypeInModel(object $model, ?string $type): ?string