Larastan: Nova: Access to an undefined property of ActionFields

Created on 19 Dec 2019  路  9Comments  路  Source: nunomaduro/larastan

  • Larastan Version: 0.3.19
  • --level used: Default (5)

Description

Laravel Nova allows us to define fields that will appear in a prompt, when running an action on some models: Nova: Action Fields documentation

Note that the handle function has \Laravel\Nova\Fields\ActionFields $fields as the first parameter, and we dynamically retrieve the value of a field by calling $fields->fieldNameHere.

I have created a field named 'reprint'. I access it using:

$fields->reprint

However, Larastan identifies this as an issue in Level 2 and up checks:

------ ----------------------------------------------------------------------------- 
 Line   app/Nova/Actions/NeedsReprint.php                                            
------ ----------------------------------------------------------------------------- 
 27     Access to an undefined property Laravel\Nova\Fields\ActionFields::$reprint.  
------ -----------------------------------------------------------------------------

Laravel code where the issue was found

    /**
     * Perform the action on the given models.
     *
     * @param  \Laravel\Nova\Fields\ActionFields  $fields
     * @param  \Illuminate\Support\Collection  $models
     * @return mixed
     */
    public function handle(ActionFields $fields, Collection $models)
    {
        foreach ($models as $model) {
            $model->reprint = $fields->reprint;
            $model->save();
        }
    }

Possible solution

Issue #345 looks like it might be similar, but I'm not clear on what the intended fix is, if I wanted to simply fix this one case manually rather than installing and running laravel-ide-helper. If the fix is to add PHPDoc to the top of my NeedsReprint action class, can you please let me know what I should be adding in my PHPDoc block?

Part of the reason for creating this as an issue, is that no issue currently exists for the
Laravel Nova ActionFields problem, so I'm hoping to provide a landing point for others with the same issue. Others using Larastan alongside Nova will likely run into this issue if they use actions.

Nova enhancement

Most helpful comment

@canvural - that works for now, cheers!

For anyone else that runs into this issue, below is the current fix until this issue can be resolved.

Example of a phpstan.neon file with the 'universalObjectCratesClasses' parameter applied:

includes:
    - ./vendor/nunomaduro/larastan/extension.neon
parameters:
    level: 5
    ignoreErrors:
        - '#Access to an undefined property App\\Nova\\[a-zA-Z0-9\\_]+::\$[a-zA-Z0-9\\_]+\.#'
    universalObjectCratesClasses:
        - Laravel\Nova\Fields\ActionFields

Looks like I had a regex workaround for another Nova-related issue in there, as well.

All 9 comments

Thank you for reporting.
As Nova is closed source could you please share the definition of ActionFields?

Hi,

Thanks for reporting this. We do not have Nova support currently.

I think this is a really tricky one. It is not easy to fix.

I'm gonna keep the issue open, for the future. But in the meanwhile, I think PHPStan's universalObjectCratesClasses option can help you. You can check its documentation here.

@szepeviktor - Not sure what I'd technically be allowed to share, since it is closed source as you mentioned, but here's some of it...

ActionFields is almost empty:

<?php

namespace Laravel\Nova\Fields;

class ActionFields extends ResolvedFields
{
    //
}

ResolvedFields extends Fluent and has a collection named $callbacks.

Fluent implements ArrayAccess, Arrayable, Jsonable, JsonSerializable. It has an array named $attributes which seems to be a set of attribute names and their associated values. It implements a get($key, $default = null) function to actually get an attribute value.

Ah! It is a magic __get(). So PHPStan needs an extension to recognize it.
e.g. see https://github.com/szepeviktor/phpstan-wordpress/blob/master/src/WpThemeMagicPropertiesClassReflectionExtension.php

@canvural - that works for now, cheers!

For anyone else that runs into this issue, below is the current fix until this issue can be resolved.

Example of a phpstan.neon file with the 'universalObjectCratesClasses' parameter applied:

includes:
    - ./vendor/nunomaduro/larastan/extension.neon
parameters:
    level: 5
    ignoreErrors:
        - '#Access to an undefined property App\\Nova\\[a-zA-Z0-9\\_]+::\$[a-zA-Z0-9\\_]+\.#'
    universalObjectCratesClasses:
        - Laravel\Nova\Fields\ActionFields

Looks like I had a regex workaround for another Nova-related issue in there, as well.

Also, it might be a good option to add the Nova directory to excludes_analyse, if you are getting too many issues with Nova related stuff. But that comes with the cost of losing the analysis for those files.

This has been the first time in a long while that I've received a Nova-related issue with Larastan. It's been pretty solid. So I don't think I'll need the excludes_analyse option just yet, but will come back here in future and try it if I start seeing more issues crop up. Thanks!

I don't think we will add support to Nova inside of the Larastan. It's too specific. And the original issue was already solved. Closing the issue.

@shane-smith You are free to develop Nova Extension for Larastan.

Was this page helpful?
0 / 5 - 0 ratings