sethsandaru / eloquent-docs

Generate phpDoc for your Eloquent Models with ease
MIT License
41 stars 4 forks source link

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

Open RazinTeqB opened 1 month ago

RazinTeqB commented 1 month ago

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/isi-abrod-api/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 commented 1 month ago

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

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 
```php
$this->app->bind(
    SethPhat\EloquentDocs\Services\Generators\ColumnsGenerator::class,
    App\Services\SethphatEloquentDocsColumnsGeneratorFix::class
);
sethsandaru commented 1 month ago

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