• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

luttje / filament-user-attributes / 6906952621

17 Nov 2023 04:58PM UTC coverage: 51.136% (-4.0%) from 55.134%
6906952621

push

github

luttje
fix checks for trait and interfaces

0 of 21 new or added lines in 2 files covered. (0.0%)

144 existing lines in 4 files now uncovered.

698 of 1365 relevant lines covered (51.14%)

12.7 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Commands/WizardStepModels.php
1
<?php
2

3
namespace Luttje\FilamentUserAttributes\Commands;
4

5
use Illuminate\Console\Command;
6
use Luttje\FilamentUserAttributes\CodeGeneration\CodeModifier;
7
use Luttje\FilamentUserAttributes\Contracts\HasUserAttributesContract;
8
use Luttje\FilamentUserAttributes\Traits\HasUserAttributes;
9

10
class WizardStepModels extends Command
11
{
12
    public function __construct()
×
13
    {
14
        $this->signature = 'filament-user-attributes:wizard-models';
×
15
        $this->description = 'Wizard to help setup your models with Filament User Attributes';
×
16

17
        parent::__construct();
×
18
    }
19

20
    public function handle()
×
21
    {
22
        $models = self::scanForModels();
×
23
        $chosenModels = $this->getChosenModels($models);
×
24

25
        $this->displaySelectedModels($chosenModels);
×
26

27
        if (empty($chosenModels)) {
×
28
            return;
×
29
        }
30

31
        $this->setupModels($chosenModels);
×
32

33
        return;
×
34
    }
35

36
    protected function getChosenModels(array $models): array
×
37
    {
38
        $models['0'] = 'No models';
×
39
        $chosenModels = $this->choice(
×
40
            "Which of your models should be able to have user attributes? (comma separated)",
×
41
            $models,
×
42
            null,
×
43
            null,
×
44
            true
×
45
        );
×
46

47
        return array_map(
×
48
            fn ($choice) => $models[$choice],
×
49
            array_filter(
×
50
                $chosenModels,
×
51
                fn ($choice) => $choice !== '0'
×
52
            )
×
53
        );
×
54
    }
55

56
    protected function displaySelectedModels(array $chosenModels): void
×
57
    {
58
        if (empty($chosenModels)) {
×
59
            return;
×
60
        }
61

62
        $this->info('The following models will be setup to support user attributes:');
×
63
        foreach ($chosenModels as $model) {
×
64
            $setupStatus = self::isModelSetup($model) ? "<fg=green>(already setup)</>" : "";
×
65
            $this->line("- $model $setupStatus");
×
66
        }
67
    }
68

69
    public static function scanForModels(): array
×
70
    {
71
        $models = [];
×
72
        $modelFiles = glob(app_path('Models/*.php'));
×
73

74
        foreach ($modelFiles as $file) {
×
75
            $modelName = basename($file, '.php');
×
76
            $models[$modelName] = app()->getNamespace() . 'Models\\' . $modelName;
×
77
        }
78

79
        return $models;
×
80
    }
81

82
    public static function isModelSetup(string $model): bool
×
83
    {
84
        $filePath = self::getModelFilePath($model);
×
85
        if (!file_exists($filePath)) {
×
UNCOV
86
            return false;
×
87
        }
88

89
        $code = file_get_contents($filePath);
×
UNCOV
90
        return CodeModifier::usesTrait($code, HasUserAttributes::class) &&
×
UNCOV
91
               CodeModifier::implementsInterface($code, HasUserAttributesContract::class);
×
92
    }
93

94
    protected function setupModels(array $models): void
×
95
    {
96
        $this->warn("\nWe will now implement the HasUserAttributesContract interface with the HasUserAttributes trait for you...");
×
97

UNCOV
98
        foreach ($models as $model) {
×
UNCOV
99
            $this->setupModel($model);
×
100
        }
101
    }
102

103
    protected function setupModel(string $model): void
×
104
    {
UNCOV
105
        $file = self::getModelFilePath($model);
×
106
        $contents = file_get_contents($file);
×
107

UNCOV
108
        $contents = CodeModifier::addTrait($contents, HasUserAttributes::class);
×
109
        $contents = CodeModifier::addInterface($contents, HasUserAttributesContract::class);
×
110

UNCOV
111
        file_put_contents($file, $contents);
×
112
    }
113

114
    private static function getModelFilePath(string $model): string
×
115
    {
UNCOV
116
        return app_path(str_replace('\\', '/', substr($model, strlen(app()->getNamespace()))) . '.php');
×
117
    }
118
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc