matchish / laravel-scout-elasticsearch

Search among multiple models with ElasticSearch and Laravel Scout
MIT License
702 stars 113 forks source link

[BUG] Call to a member function toCodeString() on null #244

Closed mikysetiawan closed 5 months ago

mikysetiawan commented 1 year ago

Describe the bug Error when I want to running scout:import: Call to a member function toCodeString() on null

at ....\vendor\matchish\laravel-scout-elasticsearch\src\Searchable\SearchableListFactory.php:110

To Reproduce Steps to reproduce the behavior:

  1. I deleted the index using curl -u elastic:password -XDELETE http://localhost:9200/INDEX_NAME
  2. then I want to generate the index again, so I run the php artisan scout:import
  3. the error occured

Expected behavior No error

Version Laravel 9.19 Laravel Scout Elasticsearch 7.2.1

matchish commented 1 year ago

Hey. Feel free to send a PR

matchish commented 1 year ago

The error occurs because for some class nodes, the namespacedName property is null, indicating that these class nodes do not have a fully qualified name. This could happen if the class does not have a namespace defined, or if there is an issue with the way the AST is generated or traversed.

Could you provide more info about file on which the error occured?

hanifqas commented 1 year ago

Issue: Error: Call to a member function toCodeString() on null

Description

I am encountering an issue with the matchish/laravel-scout-elasticsearch package when running the php artisan scout:import command. The error message I receive is:

Error: Call to a member function toCodeString() on null

This error occurs at the following line of code in the SearchableListFactory.php file:

return $node->namespacedName->toCodeString();

From my understanding, it seems that namespacedName is null for some $node objects, and therefore, calling toCodeString() on it results in an error.

Steps to reproduce the issue

  1. Run the php artisan scout:import command.

Expected Result

The command should execute successfully, without any errors.

Actual Result

I get the error Error: Call to a member function toCodeString() on null.

Additional Information

I have tried re-installing the package, moving non-class PHP files to a different location, and updating the composer.json file's autoload files array. Additionally, I have checked all models in the App namespace, and all of them have the class defined and no files are missing the class definition. However, none of these steps have resolved the issue.

Screenshots

PS ...\logisticapi> php artisan scout:import

   Error 

  Call to a member function toCodeString() on null

  at ...\vendor\matchish\laravel-scout-elasticsearch\src\Searchable\SearchableListFactory.php:110
    106▕             return $node instanceof Class_;
    107▕         });
    108▕
    109▕         return Collection::make($nodes)->map(function ($node) {
  ➜ 110▕             return $node->namespacedName->toCodeString();
    111▕         });
    112▕     }
    113▕
    114▕     /**

  1   [internal]:0
      Matchish\ScoutElasticSearch\Searchable\SearchableListFactory::Matchish\ScoutElasticSearch\Searchable\{closure}(Object(PhpParser\Node\Stmt\Class_))

  2   ...\vendor\laravel\framework\src\Illuminate\Collections\Arr.php:560
      array_map(Object(Closure))
Ecsodikas commented 5 months ago

We had the same error today. The problem was that we used anonymous classes in our code base. By replacing them with generic new stdClass() we fixed the problem. This is quite an ugly solution, but it works.

I hope this helps anyone having the same error.

matchish commented 5 months ago

@Ecsodikas thanks for sharing yours findings. don't remember details but yes anonymous classes not supported to be able to search by multiple models as I remember.