Repository: barryvdh/laravel-ide-helper (php)

Issue:
Removing extends from generated class breaks autocomplete
### Versions:
- ide-helper Version: 2.11.0
- Laravel Version: 8.79.0
- PHP Version: 8.0

### Description:

https://github.com/barryvdh/laravel-ide-helper/pull/1268 changed mixin mode to not add an extends to the generated class. This breaks the autocomplete for functions like `Model->find()` as my classes no longer extend `\Eloquent`

### Steps To Reproduce:

- Run `ide-helper:models -M`
- Try to use the `find()` function on any model and see that the return type is listed as mixed instead of the proper class
Autocompletion for Model methods is not avaliable
### Versions:
- ide-helper Version: 2.12.2
- Laravel Version: 9.4.1
- PHP Version: 8.1

### Description:
Autocompletion is not available for some methods.

![image](https://user-images.githubusercontent.com/6592868/163394266-eb017234-7329-46f5-8494-fd5081ce31a3.png)

Commands used to generate helpers:
```
  artisan ide-helper:generate
  artisan ide-helper:models -M
  artisan ide-helper:meta
```

Model methods are missing with `--write-mixin`
### Versions:
- ide-helper Version: 2.12.3
- Laravel Version: 9.9.0
- PHP Version: 8.1.3

### Description:

Model methods are missing when using `--write-mixin`

| With `--write-mixin` | Without `--write-mixin` |
|---|---|
| ![](https://user-images.githubusercontent.com/4766244/164059464-1dbb2965-cc54-4c8f-b960-fc309af8d824.png) | ![](https://user-images.githubusercontent.com/4766244/164059527-7029f385-2975-41d8-862f-55cd812d33c8.png) |

Basic model methods like `find` appear to be missing when using `--write-mixin`. They work correctly without `--write-mixin`

I've reproduced the following steps in this repo: https://github.com/ptrkcsk/laravel-ide-helper-models

There are two differences in `_ide_helper_models.php` between using `--write-mixin` and not, as seen here: https://github.com/ptrkcsk/laravel-ide-helper-models/compare/write-mixin...no-write-mixin

1. The generated class has `@mixin IdeHelperUser`. This looks like a mistake to me because `IdeHelperUser` appears to not exist
1. The generated class extends `\Eloquent`. I suspect this is the problem, because with `--write-mixin`, the generated class is missing the methods provided by `\Eloquent`

### Steps To Reproduce:

1. Generate facades helper and models helper with `--write-mixin`:

    ```sh
    vendor/bin/sail artisan ide-helper:generate
    vendor/bin/sail artisan ide-helper:models --write-mixin
    ```
   
1. Try to use a model's `find` method, e.g. `User::find()`. Notice "Method 'find' not found..." warning in IDE

1. Re-generate models helper without `--write-mixin`:

    ```sh
    vendor/bin/sail artisan ide-helper:models
    ```
   
1. Try to use a model's `find` method again. Notice `find` method is now found


Pull request:
Fix issue where \Eloquent is not included when using write_mixin
## Summary
If you write your models using the `--write-mixin` option the included docblock generated on the model will include a mixin. Previously the code replaced here would ignore the `@mixin \Eloquent` addition if any mixin existed on the model. This would break the code suggestions as on first write the Eloquent mixin would be added but after that it wouldn't be generated as its detected as 'already existing'. Info including the lines and commits where I found this are discussed in #1299 

This fix changes it to a loop which removes any mixins that matches the Eloquent class before appending it.

Also fixes #1342, fixes #1343, fixes #1299

## Type of change

<!-- Please delete options that are not relevant. -->

- [x] Bug fix (non-breaking change which fixes an issue)

### Checklist
- [x] Existing tests have been adapted and/or new tests have been added
- [x] Add a CHANGELOG.md entry
- [ ] Update the README.md
- [x] Code style has been fixed via `composer fix-style`


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index b65ebc9..f2f1ad9 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,7 @@ All notable changes to this project will be documented in this file.
 ### Fixes
 - Fix return type of methods provided by `SoftDeletes` [#1345 / KentarouTakeda](https://github.com/barryvdh/laravel-ide-helper/pull/1345)
 - Handle PHP 8.1 deprecation warnings when passing `null` to `new \ReflectionClass` [#1351 / mfn](https://github.com/barryvdh/laravel-ide-helper/pull/1351)
+- Fix issue where \Eloquent is not included when using write_mixin [#1352 / Jefemy](https://github.com/barryvdh/laravel-ide-helper/pull/1352)
 - Fix model factory method arguments for Laravel >= 9 [#1361 / wimski](https://github.com/barryvdh/laravel-ide-helper/pull/1361)
 
 2022-03-06, 2.12.3
diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php
index 3d38806..563b045 100644
--- a/src/Console/ModelsCommand.php
+++ b/src/Console/ModelsCommand.php
@@ -926,10 +926,19 @@ class ModelsCommand extends Command
             $phpdoc->appendTag($tag);
         }
 
-        if ($this->write && !$phpdoc->getTagsByName('mixin')) {
+        if ($this->write) {
             $eloquentClassNameInModel = $this->getClassNameInDestinationFile($reflection, 'Eloquent');
+
+            // remove the already existing tag to prevent duplicates
+            foreach ($phpdoc->getTagsByName('mixin') as $tag) {
+                if($tag->getContent() === $eloquentClassNameInModel) {
+                    $phpdoc->deleteTag($tag);
+                }
+            }
+
             $phpdoc->appendTag(Tag::createInstance('@mixin ' . $eloquentClassNameInModel, $phpdoc));
         }
+        
         if ($this->phpstorm_noinspections) {
             /**
              * Facades, Eloquent API


Test patch:
diff --git a/tests/Console/ModelsCommand/GeneratePhpdocWithMixin/Models/Post.php b/tests/Console/ModelsCommand/GeneratePhpdocWithMixin/Models/Post.php
index 24d6981..1317c4d 100644
--- a/tests/Console/ModelsCommand/GeneratePhpdocWithMixin/Models/Post.php
+++ b/tests/Console/ModelsCommand/GeneratePhpdocWithMixin/Models/Post.php
@@ -9,6 +9,7 @@ use Illuminate\Database\Eloquent\Model;
 /**
  * @property $someProp
  * @method someMethod(string $method)
+ * @mixin IdeHelperPost
  */
 class Post extends Model
 {