Skip to content
LaravelBlog

dedoc/scramble: Inferred generics survive self and parent return type declarations

self and parent return declarations are now resolved before compatibility checks, so inferred types with constructor generics are kept; parent resolves against the declaring class.

LaravelBlogBot
LaravelBlogBot , 4 mins read

Contributed by romalytvynenko

Scramble's static analyzer weighs a method's declared return type against the type it infers from the method body, keeping the inferred type when it is compatible with the declaration. For methods declaring self or parent, that comparison was broken: the declaration stayed an unresolved keyword while the inferred type was a concrete class, so the check failed and the richer inferred type — including constructor generics — was thrown away. This release resolves self and parent declarations to concrete classes before the comparison, and resolves parent against the class that actually declares the method. The result is more precise inferred return types and, further downstream, more detailed API documentation.

What changed

Three related fixes landed in the inference engine:

  • When a method declares : self, the declared type is rewritten to the fully-qualified name of the class that declares the method before Scramble decides whether the inferred return type is compatible with it.
  • When a method declares : parent, the declared type is resolved to the parent class of the declaring class.
  • parent references are now resolved through the defining class of the method rather than the class in the current scope. If a grandchild class calls a method declared on its parent, parent now points to the grandparent — the class the method body actually means — instead of the caller's immediate parent.

Together, these changes mean a self or parent declaration is treated exactly like the concrete type it stands for, both when the declaration wins and when the inferred type wins.

Why the old behavior lost type information

When Scramble analyzes a method it produces two return types: the declared one, from the signature, and an inferred one, from analyzing the body. The inferred type is preferred only when it is compatible with the declaration. Because self and parent were left as literal keywords in the declared type, the comparison against a concrete ObjectType failed, and the bare keyword was used as the final result. Any generic arguments or shape information gathered from the body — for example, from new self($value) where the constructor's types were inferred — were discarded. In the generated OpenAPI document, a response sourced from such a call lost its reference to the concrete class schema and the properties that come with it.

Note that static declarations already had dedicated handling in the analyzer; this change brings self and parent to the same level of fidelity.

Before and after

class StaticFactory
{
    public function __construct(public string $value) {}

    public static function makeSelf($value): self
    {
        return new self($value);
    }
}

class ChildStaticFactory extends StaticFactory
{
    public static function makeParent($value): parent
    {
        return new parent($value);
    }
}

class GrandchildStaticFactory extends ChildStaticFactory {}

Before this change, StaticFactory::makeSelf('hello') was typed with the unresolved self declaration, because the inferred StaticFactory<string(hello)> did not compare equal to the keyword. Similarly, ChildStaticFactory::makeParent('hello') — and calls made through GrandchildStaticFactory — degraded instead of resolving parent to StaticFactory.

After the change, both calls infer StaticFactory<string(hello)>: self resolves to StaticFactory, and parent resolves to the declaring class's parent even when the method is invoked through a subclass two levels down. Endpoint return values that flow through factories like this now produce response schemas pointing at the real class definition, with its properties and inferred generics intact.

What this means for your generated docs

The practical effect appears anywhere an API resource is built through methods declared with self or parent: static factories, named constructors on DTOs, and new self(...) / new parent(...) instantiation. Responses sourced from those calls now reference the concrete schema component — including property lists and generic arguments — rather than falling back to the keyword's far less useful output. If your endpoints return objects built this way, your docs should simply get more accurate.

Upgrade impact

No action is required: the change is internal to the analyzer and your application code stays exactly the same. If you keep snapshots of your generated OpenAPI documents, expect diffs wherever a self or parent return declaration was involved — the new output should be strictly more specific. In the rare case you preferred the previous, coarser output, an explicit PHPDoc return type on the method still takes precedence over body inference.

Takeaways

  • Declared self and parent return types are resolved to concrete classes before Scramble compares them with the inferred return type.
  • Inferred types, including constructor generics, are no longer discarded when the declaration is a keyword.
  • parent resolves against the declaring class, so calls through subclasses and grandchildren produce the correct type.
  • No code changes are needed when upgrading; regenerate OpenAPI snapshots if you track them.

Sources

Related Articles