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

Undefined property on models when using interface #1692

Closed
kyrare opened this issue Jul 18, 2023 · 2 comments
Closed

Undefined property on models when using interface #1692

kyrare opened this issue Jul 18, 2023 · 2 comments

Comments

@kyrare
Copy link

kyrare commented Jul 18, 2023

  • Larastan Version: 2.6.3
  • --level used: 2
  • Pull request with failing test:

Description

Larastan does not see properties that have been checked through the interface

Laravel code where the issue was found

use Illuminate\Database\Eloquent\Model;

/**
 * @property-read string $url
 */
interface Page
{
    public function getUrlAttribute(): string;
}

class News extends Model implements Page
{
    public function getUrlAttribute(): string
    {
        return '/news/foo/';
    }
}

function test(Model $model): array
{
    $data = [];
    
    if ($model instanceof Page) {
        $data[] = $model->url; // Access to an undefined property Illuminate\Database\Eloquent\Model&Page::$url
    }

    return $data;
}
@szepeviktor
Copy link
Collaborator

Try using an Accessor
https://laravel.com/docs/10.x/eloquent-mutators#accessors-and-mutators

Then add PHP generics

/**
 * @return \Illuminate\Database\Eloquent\Casts\Attribute<string, never>
 */

@sanfair
Copy link
Contributor

sanfair commented Jul 19, 2023

Properties cannot be defined on interfaces
https://phpstan.org/r/f14d865c-30c1-4075-9050-8b11086c1811

Update: link to PHPStan issue phpstan/phpstan#8550

@canvural canvural closed this as completed Jan 3, 2024
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

4 participants