#676·larastan

Update stubs for new `model-property` type

Author: canvuralCreated Oct 7, 2020Updated Jun 25, 2026
Labelsenhancementhelp wantedgood first issue

The Problem

With the new model-property type and ModelPropertyRule Larastan now has the ability to check the existence of model columns that are given to a method.

Adding the model-property type to the Laravel's core methods are done with the help of PHPstan stubs. Some examples can be seen here. Currently, not all the Laravel methods that are doing something with the model properties are updated in stubs. And we need help with updating this.

Contributing

You can contribute to this issue by updating the stubs and tests. Below you can read the detailed instructions for how to that. In case you have any questions you can ask them in this issue, or you can reach out to me at Twitter

Preparation

Helpful guides

Updating stubs

Here is the directory containing all of the stubs Larastan uses. All of the stubs that need updating are already there, so you don't need to create new files. Just updating method definitions inside.

Steps

  1. Find a Laravel method that does not exist in our stubs, or exists but the parameter type is not yet updated. For this example, we will use this method here: https://github.com/laravel/framework/blob/8.x/src/Illuminate/Database/Eloquent/Builder.php#L511

  2. Then we need to copy the method definition into our stub. So in this file, at the end we can add the following code:

php
/**
 * Get a single column's value from the first result of a query.
 *
 * @param  model-property<TModelClass>  $column
 * @return mixed
 */
public function value($column);

Here we used generic type model-property<TModelClass> because we only want to allow the model properties from the current model. You can see the TModelClass defined at the beginning of the class. If the stub you are updating does not have any @template definition for the class you can just use model-property

  1. As a last step we need to update the tests to verify our changes are working as we expect. The method we added to stubs was on Eloquent Builder class, so we need to add our test case to the model-property-builder file. If the stu you added was in one of the relation classes you can use model-property-relation file. If it was in the model class itself, you can use model-property-model file.

Our test case can be as simple as the following:

php
\App\User::query()->value('emaiil');

We intentionally made a typo and we expect Larastan to catch it.

Note: Here you can also add test cases that shouldn't produce any error. If you want to test that aspect of the method too.

Now that we added a test case, we also need to add an assertion that the expected error is produced. For that we need to add a new line to the testModelPropertyRuleOnBuilder method in ModelPropertyRuleTest class.

php
public function testModelPropertyRuleOnBuilder(): void
{
    $errors = $this->setConfigPath(__DIR__.DIRECTORY_SEPARATOR.'Data/modelPropertyConfig.neon')->findErrorsByLine(__DIR__.'/Data/model-property-builder.php');

    self::assertEquals([
        // Rest of the assertions
        35 => 'Property \'emaiil\' does not exist in App\\User model.', // Newly added line
    ], $errors);
}

Here the array key represents the line number of the error. And the value is the message we expect to be produced.

Now let's run the whole test suit to see we did not break anything else. You can do that by running the command composer test in your terminal. If everything goes well you should see all green:

image

Then you can commit your changes, push it to your fork of the repo then go onto Github and open a pull request!

That's it! You've successfully created a PR on an open-source project

(PS: The example used in this issue is not currently in the stubs. So you can just follow the guide, copy the code and make an easy first contribution )

List of PRs