Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AnonymousMigrations - Wrong location for closing ; when } appears after the class #10

Closed
flap152 opened this issue Sep 30, 2023 · 2 comments
Labels
bug Something isn't working

Comments

@flap152
Copy link
Contributor

flap152 commented Sep 30, 2023

  • Laravel Version: 10.16
  • PHP Version: 8.1.13
  • Shift CLI Version: 0.2.9
  • Platform: Windows 10

Issue:

In the AnonymousMigrations task the closing ; will appear in the wrong location if } appears after the class (in a comment section for example).

We sometimes document migrations with a data sample that could be imported into the table. We put it in a comment after the migration class because it will contain multiple records, sometimes expressed as json.

AnonymousMigrations currently replaces the last } in the file with }; to close the anonymous migration class return statement, but ends up changing the comment and the migration statement remains in error.

This modified fixture makes the test fail

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class SimpleMigration extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('videos', function (Blueprint $table) {
            $table->integer('runtime')->nullable();
        });
    }
}
/* a closing bracket in a comment here can confuse the parsing
{ id: 1, name: "Doug" }
*/

It can be argued that comments belong before the code, not after, and that shift is not expected to cover such a case.

However, I made a fixture and failing test, then a change that parses the class and puts the ; right after it in the file. If wanted, I will gladly submit a PR (be warned it would be my first PR )

@flap152 flap152 added the bug Something isn't working label Sep 30, 2023
@jasonmccreary
Copy link
Collaborator

Yes, feel free to issue PR. I think this is pretty light on a parsing. It just uses string manipulation. However, the better thing to do would probably be to parse the class and look at the closing offset. Or use the PhpFile class to find the closing brace.

@jasonmccreary
Copy link
Collaborator

Fixed in #11.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants