Skip to content

Add missing return type diagnostics and quick fixes#653

Open
eramitgupta wants to merge 3 commits into
laravel:mainfrom
eramitgupta:main
Open

Add missing return type diagnostics and quick fixes#653
eramitgupta wants to merge 3 commits into
laravel:mainfrom
eramitgupta:main

Conversation

@eramitgupta
Copy link
Copy Markdown

Summary

Adds a new return type feature for PHP methods/functions in the Laravel VS Code extension.

This change:

  • shows a diagnostic when a function/method is missing a return type
  • does not show a diagnostic when a return type is already declared
  • adds quick fixes to insert a suggested return type
  • includes test coverage for common PHP return types and model return types

What was added

  • New returnType diagnostic provider
  • Diagnostic registry wiring for returnType
  • Config support for Laravel.returnType.diagnostics
  • Quick fix suggestions for missing return types
  • Test fixture and test suite for return type behavior

Covered return type cases

The feature now avoids warnings when a return type already exists, including examples like:

  • array
  • int
  • string
  • mixed
  • ?string
  • ?int
  • string|array
  • Countable&Traversable
  • static
  • model return types like User
  • framework/class return types like RedirectResponse

Quick fix behavior

When no return type is declared, the extension now suggests adding one.
Examples of inferred suggestions:

  • return []; -> : array
  • return "text"; -> : string
  • return 1; -> : int
  • return true; -> : bool
  • return new User(); -> : User
  • return; or no return value -> : void

A fallback : mixed quick fix is also provided.

Tests

Verified with:

  • npm run check-types
  • npm test -- --grep "Return Type Test Suite"

Notes

This feature currently checks whether a return type is declared and offers suggestions when missing.
It does not yet validate whether the declared return type matches the actual returned value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant