eloquent-docs icon indicating copy to clipboard operation
eloquent-docs copied to clipboard

After upgrading to v2.0.1 wrong Carbon\Carbon namespace for date property generated

Open RazinTeqB opened this issue 1 year ago • 2 comments

I am upgrading laravel 10 project to version 11. I have upgraded sethphat/eloquent-docs package to v2.0.1

user001@DEV:~/projects/my-project$ composer show sethphat/eloquent-docs
name     : sethphat/eloquent-docs
descrip. : Generate PHPDoc scope for your Eloquent models (columns, accessors and more)
keywords : 
versions : * 2.0.1
type     : library
license  : MIT License (MIT) (OSI approved) https://spdx.org/licenses/MIT.html#licenseText
homepage : 
source   : [git] https://github.com/sethsandaru/eloquent-docs.git 51cee123d8732a5095d1e8e165988f8238917d8d
dist     : [zip] https://api.github.com/repos/sethsandaru/eloquent-docs/zipball/51cee123d8732a5095d1e8e165988f8238917d8d 51cee123d8732a5095d1e8e165988f8238917d8d
path     : /home/teq001/projects/project/vendor/sethphat/eloquent-docs
names    : sethphat/eloquent-docs

When running this command

php artisan eloquent:bulk-phpdoc "app/Models/*.php" --short-class

This is how doc block is added in model

<?php

namespace App\Models;

/**
 * Table: users
 *
 * === Columns ===
 * 
 * ... Other columns
 *
 * @property Carbon\Carbon|null $deleted_at
 * @property Carbon\Carbon $created_at
 * @property Carbon\Carbon|null $updated_at
 * 
 */
class User
{
    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'deleted_at' => 'date:m/d/Y',
        'created_at' => 'date:m/d/Y',
        'updated_at' => 'date:m/d/Y',
    ];
}

Previously it was generated like this

<?php

namespace App\Models;

/**
 * Table: users
 *
 * === Columns ===
 * 
 * ... Other columns
 *
 * @property \Carbon\Carbon|null $deleted_at
 * @property \Carbon\Carbon $created_at
 * @property \Carbon\Carbon|null $updated_at
 * 
 */
class User
{
    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'deleted_at' => 'date:m/d/Y',
        'created_at' => 'date:m/d/Y',
        'updated_at' => 'date:m/d/Y',
    ];
}

See difference in Carbon namespace

- * @property \Carbon\Carbon|null $deleted_at
+ * @property Carbon\Carbon|null $deleted_at

so after version upgrade, generated property doc block @property Carbon\Carbon|null $deleted_at field namespace resolved to App\Models\Carbon\Carbon which is in-correct.

What am i missing ??

RazinTeqB avatar Aug 13 '24 05:08 RazinTeqB

I am able fix the issue for my project with this.

  • app/Services/SethphatEloquentDocsColumnsGeneratorFix.php
<?php

namespace App\Services;

use SethPhat\EloquentDocs\Services\Generators\ColumnsGenerator;

class SethphatEloquentDocsColumnsGeneratorFix extends ColumnsGenerator
{
    #[\Override]
    protected function getDateCasting(string $column): string
    {
        $columnType = parent::getDateCasting($column);

        return $columnType === 'string'
            ? $columnType
            : $this->getRootNamespaceClassName($columnType);
    }

    public function getRootNamespaceClassName(string $className): string
    {
        return '\\' . ltrim($className, '\\');
    }
}

and in app/Providers/AppServiceProvider.php -> register method

$this->app->bind(
    SethPhat\EloquentDocs\Services\Generators\ColumnsGenerator::class,
    App\Services\SethphatEloquentDocsColumnsGeneratorFix::class
);

RazinTeqB avatar Aug 14 '24 06:08 RazinTeqB

Interesting, thanks for reporting! I'll play with it

sethsandaru avatar Aug 14 '24 08:08 sethsandaru