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

Issue:
v2.9.1: `artisan ide-helper:models` stopped working after update
### Versions:
- ide-helper Version: 2.9.1
- Laravel Version: 8.33.1
- PHP Version: 7.4.15

### Description:
```
> @php artisan ide-helper:models -N
Exception: Class 'Database\Factories\PaypalNotificationFactory' not found
Could not analyze class App\Models\PaypalNotification.

Trace:
#0 /var/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Factories/HasFactory.php(15): Illuminate\Database\Eloquent\Factories\Factory::factoryForModel('App\\Models\\Payp...')
#1 /var/www/vendor/barryvdh/laravel-ide-helper/src/Console/ModelsCommand.php(1096): App\Models\PaypalNotification::factory()
#2 /var/www/vendor/barryvdh/laravel-ide-helper/src/Console/ModelsCommand.php(281): Barryvdh\LaravelIdeHelper\Console\ModelsCommand->getFactoryMethods(Object(App\Models\PaypalNotification))
#3 /var/www/vendor/barryvdh/laravel-ide-helper/src/Console/ModelsCommand.php(162): Barryvdh\LaravelIdeHelper\Console\ModelsCommand->generateDocs(Array, Array)
#4 /var/www/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(36): Barryvdh\LaravelIdeHelper\Console\ModelsCommand->handle()
#5 /var/www/vendor/laravel/framework/src/Illuminate/Container/Util.php(40): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
#6 /var/www/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(93): Illuminate\Container\Util::unwrapIfClosure(Object(Closure))
#7 /var/www/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(37): Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Array, Object(Closure))
#8 /var/www/vendor/laravel/framework/src/Illuminate/Container/Container.php(611): Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), Array, Array, NULL)
#9 /var/www/vendor/laravel/framework/src/Illuminate/Console/Command.php(136): Illuminate\Container\Container->call(Array)
#10 /var/www/vendor/symfony/console/Command/Command.php(256): Illuminate\Console\Command->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle))
#11 /var/www/vendor/laravel/framework/src/Illuminate/Console/Command.php(121): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle))
#12 /var/www/vendor/symfony/console/Application.php(971): Illuminate\Console\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#13 /var/www/vendor/symfony/console/Application.php(290): Symfony\Component\Console\Application->doRunCommand(Object(Barryvdh\LaravelIdeHelper\Console\ModelsCommand), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#14 /var/www/vendor/symfony/console/Application.php(166): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#15 /var/www/vendor/laravel/framework/src/Illuminate/Console/Application.php(92): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#16 /var/www/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(129): Illuminate\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#17 /var/www/artisan(37): Illuminate\Foundation\Console\Kernel->handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#18 {main}
```


---
Upd.
**Solution:**
Be sure to remove any `use HasFactory;` on models not having factories to avoid crashes in ide-helper

Pull request description:
Fix ide-helper:models exception if model doesn't have factory
## Summary
fixes #1188 
- [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
- [x] Code style has been fixed via `composer fix-style`


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index d85e760..78d54fa 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,7 @@ All notable changes to this project will be documented in this file.
 - Model hooks for adding custom information from external sources to model classes through the ModelsCommand [\#945 / wimski](https://github.com/barryvdh/laravel-ide-helper/pull/945)
 
 ### Fixed
+- Fix ide-helper:models exception if model doesn't have factory [\#1196 / ahmed-aliraqi](https://github.com/barryvdh/laravel-ide-helper/pull/1196)
 - Running tests triggering post_migrate hooks [\#1193 / netpok](https://github.com/barryvdh/laravel-ide-helper/pull/1193)
 - Array_merge error when config is cached prior to package install [\#1184 / netpok](https://github.com/barryvdh/laravel-ide-helper/pull/1184)
 
diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php
index 4ee4b08..8dcbb23 100644
--- a/src/Console/ModelsCommand.php
+++ b/src/Console/ModelsCommand.php
@@ -1097,7 +1097,14 @@ class ModelsCommand extends Command
         }
 
         $modelName = get_class($model);
-        $factory = get_class($modelName::factory());
+        $modelBaseName = class_basename($modelName);
+
+        $factory = "\Database\Factories\\{$modelBaseName}Factory";
+
+        if ($modelName::newFactory()) {
+            $factory = get_class($modelName::newFactory());
+        }
+
         $factory = '\\' . trim($factory, '\\');
 
         if (!class_exists($factory)) {


Test patch:
diff --git a/tests/Console/ModelsCommand/Factories/Models/ModelWithoutFactory.php b/tests/Console/ModelsCommand/Factories/Models/ModelWithoutFactory.php
new file mode 100644
index 0000000..d88ca6b
--- /dev/null
+++ b/tests/Console/ModelsCommand/Factories/Models/ModelWithoutFactory.php
@@ -0,0 +1,13 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Factories\Models;
+
+use Illuminate\Database\Eloquent\Factories\HasFactory;
+use Illuminate\Database\Eloquent\Model;
+
+class ModelWithoutFactory extends Model
+{
+    use HasFactory;
+}
diff --git a/tests/Console/ModelsCommand/Factories/Test.php b/tests/Console/ModelsCommand/Factories/Test.php
index c13d1a5..43d9c73 100644
--- a/tests/Console/ModelsCommand/Factories/Test.php
+++ b/tests/Console/ModelsCommand/Factories/Test.php
@@ -26,6 +26,7 @@ class Test extends AbstractModelsCommand
 
         $this->assertSame(0, $tester->getStatusCode());
         $this->assertStringContainsString('Written new phpDocBlock to', $tester->getDisplay());
+        $this->assertStringNotContainsString('not found', $tester->getDisplay());
         $this->assertMatchesMockedSnapshot();
     }
 }
diff --git a/tests/Console/ModelsCommand/Factories/__snapshots__/Test__test__1.php b/tests/Console/ModelsCommand/Factories/__snapshots__/Test__test__1.php
index df8bef1..4d8b0f8 100644
--- a/tests/Console/ModelsCommand/Factories/__snapshots__/Test__test__1.php
+++ b/tests/Console/ModelsCommand/Factories/__snapshots__/Test__test__1.php
@@ -4,6 +4,27 @@ declare(strict_types=1);
 
 namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Factories\Models;
 
+use Illuminate\Database\Eloquent\Factories\HasFactory;
+use Illuminate\Database\Eloquent\Model;
+
+/**
+ * Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Factories\Models\ModelWithoutFactory
+ *
+ * @method static \Illuminate\Database\Eloquent\Builder|ModelWithoutFactory newModelQuery()
+ * @method static \Illuminate\Database\Eloquent\Builder|ModelWithoutFactory newQuery()
+ * @method static \Illuminate\Database\Eloquent\Builder|ModelWithoutFactory query()
+ * @mixin \Eloquent
+ */
+class ModelWithoutFactory extends Model
+{
+    use HasFactory;
+}
+<?php
+
+declare(strict_types=1);
+
+namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Factories\Models;
+
 use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Factories\Factories\SimpleFactory;
 use Illuminate\Database\Eloquent\Factories\HasFactory;
 use Illuminate\Database\Eloquent\Model;