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

tempestphp / tempest-framework / 14393323144

10 Apr 2025 04:27PM UTC coverage: 81.196% (-0.2%) from 81.363%
14393323144

push

github

web-flow
refactor(core): rename `DoNotDiscover` to `SkipDiscovery` (#1142)

7 of 9 new or added lines in 9 files covered. (77.78%)

52 existing lines in 11 files now uncovered.

11529 of 14199 relevant lines covered (81.2%)

106.5 hits per line

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

92.73
/src/Tempest/Database/src/Commands/MakeMigrationCommand.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Tempest\Database\Commands;
6

7
use InvalidArgumentException;
8
use Tempest\Console\ConsoleArgument;
9
use Tempest\Console\ConsoleCommand;
10
use Tempest\Core\PublishesFiles;
11
use Tempest\Database\Enums\MigrationType;
12
use Tempest\Database\Stubs\MigrationStub;
13
use Tempest\Discovery\SkipDiscovery;
14
use Tempest\Generation\ClassManipulator;
15
use Tempest\Generation\DataObjects\StubFile;
16
use Tempest\Generation\Exceptions\FileGenerationAbortedException;
17
use Tempest\Generation\Exceptions\FileGenerationFailedException;
18
use Tempest\Validation\Rules\EndsWith;
19
use Tempest\Validation\Rules\NotEmpty;
20

21
use function Tempest\Support\str;
22

23
final class MakeMigrationCommand
24
{
25
    use PublishesFiles;
26

27
    #[ConsoleCommand(
4✔
28
        name: 'make:migration',
29
        description: 'Creates a new migration file',
30
        aliases: ['migration:make', 'migration:create', 'create:migration'],
31
    )]
32
    public function __invoke(
33
        #[ConsoleArgument(
34
            description: 'The file name of the migration',
35
        )]
36
        string $fileName,
37
        #[ConsoleArgument(
38
            name: 'type',
39
            description: 'The type of the migration to create',
40
        )]
41
        MigrationType $migrationType = MigrationType::OBJECT,
42
    ): void {
43
        try {
44
            $stubFile = $this->getStubFileFromMigrationType($migrationType);
4✔
45
            $targetPath = match ($migrationType) {
4✔
46
                MigrationType::RAW => $this->generateRawFile($fileName, $stubFile),
4✔
47
                default => $this->generateClassFile($fileName, $stubFile),
3✔
48
            };
4✔
49

50
            $this->success(sprintf('Migration file successfully created at "%s".', $targetPath));
4✔
UNCOV
51
        } catch (FileGenerationAbortedException|FileGenerationFailedException|InvalidArgumentException $e) {
×
UNCOV
52
            $this->error($e->getMessage());
×
53
        }
54
    }
55

56
    /**
57
     * Generates a raw migration file.
58
     * @param string $fileName The name of the file.
59
     * @param StubFile $stubFile The stub file to use.
60
     *
61
     * @return string The path to the generated file.
62
     */
63
    private function generateRawFile(
1✔
64
        string $fileName,
65
        StubFile $stubFile,
66
    ): string {
67
        $now = date('Y-m-d');
1✔
68
        $tableName = str($fileName)->snake()->toString();
1✔
69
        $suggestedPath = str($this->getSuggestedPath('Dummy'))
1✔
70
            ->replace(
1✔
71
                ['Dummy', '.php'],
1✔
72
                [$now . '_' . $tableName, '.sql'],
1✔
73
            )
1✔
74
            ->toString();
1✔
75

76
        $targetPath = $this->promptTargetPath($suggestedPath, rules: [
1✔
77
            new NotEmpty(),
1✔
78
            new EndsWith('.sql'),
1✔
79
        ]);
1✔
80
        $shouldOverride = $this->askForOverride($targetPath);
1✔
81

82
        $this->stubFileGenerator->generateRawFile(
1✔
83
            stubFile: $stubFile,
1✔
84
            targetPath: $targetPath,
1✔
85
            shouldOverride: $shouldOverride,
1✔
86
            replacements: [
1✔
87
                'DummyTableName' => $tableName,
1✔
88
            ],
1✔
89
        );
1✔
90

91
        return $targetPath;
1✔
92
    }
93

94
    /**
95
     * Generates a class migration file.
96
     *
97
     * @param string $fileName The name of the file.
98
     * @param StubFile $stubFile The stub file to use.
99
     *
100
     * @return string The path to the generated file.
101
     */
102
    private function generateClassFile(
3✔
103
        string $fileName,
104
        StubFile $stubFile,
105
    ): string {
106
        $suggestedPath = $this->getSuggestedPath($fileName);
3✔
107
        $targetPath = $this->promptTargetPath($suggestedPath);
3✔
108
        $shouldOverride = $this->askForOverride($targetPath);
3✔
109

110
        $this->stubFileGenerator->generateClassFile(
3✔
111
            stubFile: $stubFile,
3✔
112
            targetPath: $targetPath,
3✔
113
            shouldOverride: $shouldOverride,
3✔
114
            replacements: [
3✔
115
                'dummy-date' => date('Y-m-d'),
3✔
116
                'dummy-table-name' => str($fileName)->snake()->toString(),
3✔
117
            ],
3✔
118
            manipulations: [
3✔
119
                fn (ClassManipulator $class) => $class->removeClassAttribute(SkipDiscovery::class),
3✔
120
            ],
3✔
121
        );
3✔
122

123
        return $targetPath;
3✔
124
    }
125

126
    private function getStubFileFromMigrationType(MigrationType $migrationType): StubFile
4✔
127
    {
128
        try {
129
            return match ($migrationType) {
130
                MigrationType::RAW => StubFile::from(dirname(__DIR__) . '/Stubs/migration.stub.sql'),
4✔
131
                MigrationType::OBJECT => StubFile::from(MigrationStub::class), // @phpstan-ignore match.alwaysTrue (Because this is a guardrail for the future implementations)
3✔
132
                default => throw new InvalidArgumentException(sprintf('The "%s" migration type has no supported stub file.', $migrationType->value)),
4✔
133
            };
UNCOV
134
        } catch (InvalidArgumentException $invalidArgumentException) {
×
UNCOV
135
            throw new FileGenerationFailedException(sprintf('Cannot retrieve stub file: %s', $invalidArgumentException->getMessage()));
×
136
        }
137
    }
138
}
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