laravel/pint

Generating a Gitlab Report isn't correct with the line beginnings and endings

stefan-willems-beech opened this issue · 2 comments

Pint Version

1.13.1

PHP Version

8.2.5

Description

Currently when generating a report based on the Gitlab format, the line beginnings and endings of each entry is not correct and is set to the beginning of 1.

I am running the tool in my pipeline to generate a cgl report and use it in Gitlab to show me where the issues are.

the command I am running is as follows: ./vendor/bin/pint -v --test --format=gitlab > gl-code-quality-report.json.

One of the lines it generates is:

{"check_name":"blank_line_after_opening_tag","description":"blank_line_after_opening_tag","categories":["Style"],"fingerprint":"a33b0f3ff225ff07b6d7c19fc46e8f46","severity":"minor","location":{"path":"\/builds\/application-projects\/skeleton\/skeleton-project\/app\/Models\/User.php","lines":{"begin":1,"end":5}}},{"check_name":"ordered_traits","description":"ordered_traits","categories":["Style"],"fingerprint":"5f35cbcf41f3ba85fea99a52507b9c5d","severity":"minor","location":{"path":"\/builds\/application-projects\/skeleton\/skeleton-project\/app\/Models\/User.php","lines":{"begin":1,"end":5}}},{"check_name":"single_line_comment_style","description":"single_line_comment_style","categories":["Style"],"fingerprint":"98d3124e5c0afc80c3c1f0395276639d","severity":"minor","location":{"path":"\/builds\/application-projects\/skeleton\/skeleton-project\/app\/Models\/User.php","lines":{"begin":1,"end":5}}}

And this is the output if I run the command in my IDE:

⨯ app/Models/User.php                                                                                                                                                                                                                                                                                                                                                           single_trait_insert_per_statement, blank_line_after_opening_tag, no_multiple_statements_per_line  
  @@ -1,5 +1,7 @@
  -<?php declare(strict_types=1);
  +<?php
   
  +declare(strict_types=1);
  +
   namespace App\Models;
   
   use App\Interfaces\Broadcastable;
  @@ -19,7 +21,12 @@
    */
   class User extends Authenticatable implements Broadcastable
   {
  -    use HasRoles, Notifiable, SoftDeletesTrait, Blameable, Revisionable, HasFactory;
  +    use HasRoles;
  +    use Notifiable;
  +    use SoftDeletesTrait;
  +    use Blameable;
  +    use Revisionable;
  +    use HasFactory;
   
       protected $guard_name = 'api';

Steps To Reproduce

Run the command given above and have a User class which is code as follows:

The preset being used is psr12.

<?php declare(strict_types=1);

namespace App\Models;

use App\Interfaces\Broadcastable;
use App\Traits\Blameable;
use App\Traits\Revisionable;
use App\Traits\SoftDeletesTrait;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Spatie\Permission\Traits\HasRoles;

/**
 * @mixin IdeHelperUser
 */
class User extends Authenticatable implements Broadcastable
{
    use HasRoles, Notifiable, SoftDeletesTrait, Blameable, Revisionable, HasFactory;

    protected $guard_name = 'api';

    #region JSON API
    public const JSON_API_TYPE = 'users';
    #endregion

    #region Eloquent
    /**
     * The attributes that are mass assignable.
     *
     * @var array<string>
     */
    protected $fillable = [
        'name',
        'email',
        'given_name',
        'family_name',
        'email_verified',
        'updated_by',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array<string>
     */
    protected $hidden = [
        'password',
        'created_by',
        'created_at',
        'deleted_at',
        'deleted_by',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array<class-string|string, class-string|string>
     */
    protected $casts = [
        'company_id' => 'integer',
        'created_by' => 'integer',
        'deleted_by' => 'integer',
        'email' => 'string',
        'email_verified' => 'boolean',
        'family_name' => 'string',
        'given_name' => 'string',
        'name' => 'string',
        'remember_token' => 'string',
        'updated_by' => 'integer',
    ];
    #endregion

    #region Relationships
    public function revisions(): MorphMany
    {
        return $this->morphMany(Revision::class, 'revisionable');
    }

    public function files(): HasMany
    {
        return $this->hasMany(File::class);
    }

    public function company(): BelongsTo
    {
        return $this->belongsTo(Company::class);
    }
    #endregion
}

Feel free to submit a pull request for this issue. On our end, we're not sure if there's a bug.

@stefan-willems-beech I figured it out.

According to PHP-CS-Fixer documentation,

If you want to integrate with GitLab’s Code Quality feature, in order for report to contain correct line numbers, you will need to use both --format=gitlab and --diff arguments.

To enable PHP-CS-Fixer's diff mode, you need to make Laravel Pint verbose.

'diff' => $output->isVerbose(),

So the command you need to generate a correct GitLab Code Quality report is the following:

./vendor/bin/pint --test --format=gitlab -v > gl-code-quality-report.json