Skip to content

Make invalid method call and property get hard errors #313

Description

@DarkGhostHunter

Description

Currently, PHPantom does not treat calling methods that do not exist as errors:

Image

This means the editor is not aware of hard errors, "hard" as in "it will throw an syntax exception you like or not".

Use case

Basically this allows the developer to immediately pick typos on method and properties while writing instead of waiting for the error to happen.

Proposed solution

Throw an error if the method or property does not exist.

This should not be an error depending on the call:

  • If the class implements __call, method calls should not show an error.
  • If the class implements __callStatic, static method calls should not show an error.
  • If the class implements __get, dynamic property getters should not show an error.

For Laravel and other frameworks, properties set/get is pure magic. This should be detected using the following list, in order of importance:

  1. PHPDoc (primary source of truth): The developer says what the model has. Include traits/extended as fallback.
  2. Casts: It states the property AND type.
  3. Attributes: Manually states the property and type, even if these are virtual. Also include traits that use mergeCasts() on initialize{TraitName}.
  4. Fillable/Hidden + Migration: mixes the property and the type. Also include traits that use mergeFillable() and mergeHidden() on initialize{TraitName}.
  5. Relationship property: Map a Model to the property (Laravel magic). Don't use TModel|null unless the dev explicitly says so via PHPDoc. Include Traits
  6. Local Scopes: states the method, and attributes. Include Traits with scopes.
  7. Magic Local Scopes: Typical where + {tableColumn} + params..., like wherePublishedAt(null).

It's kind of a hassle, but the only way to offer code completion in a reliable way for models.

Alternatives considered

Going back to PHPStorm?

Code example

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions