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

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

Open
RazinTeqB opened this issue Aug 13, 2024 · 2 comments

Comments

@RazinTeqB
Copy link

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
Copy link
Author

RazinTeqB commented Aug 14, 2024

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
);

@sethsandaru
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants