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

[Bug]: Non existent attribute types #97

Open
ImSketch opened this issue Dec 20, 2024 · 5 comments
Open

[Bug]: Non existent attribute types #97

ImSketch opened this issue Dec 20, 2024 · 5 comments
Assignees
Labels

Comments

@ImSketch
Copy link

Extension Version

0.1.14

PHP Binary

Sail

Operating System

Windows

What happened?

When the extension is running, some of the model attributes are typed with non-existent types. In my particular case, with an "App\Models\date" and "App\Models\smallint" types.

I use bmewburn.vscode-intelephense-client in addition to Laravel extension.

image
image
image

Mimimal Code Sample

Model's migration:

Schema::create('user_streaks', function (Blueprint $table) {
     $table->foreignId('user_id')->primary()->constrained()->cascadeOnDelete();
     $table->unsignedSmallInteger('current_streak');
     $table->unsignedSmallInteger('longest_streak');
     $table->date('last_activity_date')->nullable();
     $table->timestamps();
});

Model:

<?php

declare(strict_types=1);

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

final class UserStreak extends Model
{
    use HasFactory;

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'user_id';

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'user_id',
        'current_streak',
        'longest_streak',
        'last_activity_date',
    ];

    /**
     * Get the user associated with the streak.
     */
    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }

    /**
     * Get the attributes that should be cast.
     *
     * @return array<string, string>
     */
    protected function casts(): array
    {
        return [
            'last_activity_date' => 'date',
        ];
    }
}
@ImSketch ImSketch added the bug label Dec 20, 2024
@ImSketch ImSketch changed the title [Bug]: [Bug]: Non existent attribute types Dec 20, 2024
@freerkminnema
Copy link

Same problem here. My initial guess is that this map needs to be expanded:

datetime: "\\Illuminate\\Support\\Carbon",

The invalid type that I encountered is varchar, which should map to string

@freerkminnema
Copy link

freerkminnema commented Jan 1, 2025

Okay, so I think there are multiple issues at play here:

  1. There are still a few common data types that are not properly mapped yet. Like the smallint that @ImSketch is encountering. But definitely a few more.

  2. There is a provision in the mapping code for varchar(x), with a specified length, but not for varchar as-is.

  3. SQLite's Flexible Type system allows for literally any data type name, and even no type name. This means I can even end up with a type hint of @property |null $typeless when I create a column without specifying a data type.

Besides adding more of the common data types, perhaps it would be beneficial that getActualType() defaults to returning mixed when no mapping is found?

@joetannenbaum
Copy link
Collaborator

How is this looking in v0.1.19?

@lcdss
Copy link

lcdss commented Feb 12, 2025

I'm having the same problem with _model_helpers defining my model attributes of type string as varchar and inteliphense complaining about the type.

final class RoleData extends Data
{
    public function __construct(
        #[Max(50)]
        public string $name,
        /** @var Lazy|Collection<int, PermissionData> */
        public Lazy|Collection $permissions
    ) {}

    public static function fromModel(Role $role): self
    {
        return new self(
            $role->name, // Expected type 'string'. Found 'App\Models\varchar'.intelephense(P1006)
            Lazy::create(fn () => PermissionData::collect($role->permissions))
        );
    }
}
// _model_helpers.php
/**
 * App\Models\Role
 *
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property varchar $guard_name
 * @property varchar $name
 * @property int $id
 * ...
 */
class Role extends \Illuminate\Database\Eloquent\Model
{
        //
}

@freerkminnema
Copy link

How is this looking in v0.1.19?

The refactor in #233 has offered some improvement, but there are still some incorrect or invalid docblocks being generated.

I've taken a shot at this issue with #258

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

No branches or pull requests

4 participants