Skip to content

Improve property access when type hinting declared with @property #4075

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

Conversation

kayw-geek
Copy link

@kayw-geek kayw-geek commented Jun 27, 2025

Problem

Previously, accessing properties declared with @property-read annotations would incorrectly trigger " Access to an undefined property" error.

<?php declare(strict_types = 1);

use function PHPStan\dumpType;
use function PHPStan\Testing\assertType;

/**
 * @property-read \stdClass $getCreator
 */
class Model
{
}

class Defaults
{
    public function defaults(Model $model): void
    {
        $creator = $model->getCreator;  // Access to an undefined property Model::$getCreator.

    }
}

Playground: phpstan.org/r/7c5fd6b0-da88-4298-8e72-59bebdd48d08

Solution

Enhanced property access checking to properly recognize properties declared via PHPDoc annotations.

@kayw-geek kayw-geek changed the title Improve property access checking with property tag support [2.x] Improve property access checking with property tag support Jun 27, 2025
@kayw-geek kayw-geek changed the title [2.x] Improve property access checking with property tag support Improve property access checking with property tag support Jun 27, 2025
@kayw-geek kayw-geek changed the title Improve property access checking with property tag support Improve property access property when type hinting with @property Jun 27, 2025
@kayw-geek kayw-geek changed the title Improve property access property when type hinting with @property Improve property access when type hinting with @property Jun 27, 2025
@kayw-geek kayw-geek changed the title Improve property access when type hinting with @property Improve property access when type hinting declared with @property Jun 27, 2025
@kayw-geek kayw-geek force-pushed the feature/improve-access-properties-check branch 2 times, most recently from d1b2ca8 to 172a886 Compare June 27, 2025 03:47
@kayw-geek kayw-geek marked this pull request as draft June 27, 2025 04:16
@kayw-geek kayw-geek force-pushed the feature/improve-access-properties-check branch from 172a886 to a2f80ff Compare June 27, 2025 05:51
@kayw-geek kayw-geek force-pushed the feature/improve-access-properties-check branch from a2f80ff to 848efbe Compare June 27, 2025 06:42
@kayw-geek kayw-geek marked this pull request as ready for review June 27, 2025 07:20
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

@canvural
Copy link
Contributor

Isn't this expected behavior from PHP 8.2+? You need AllowDynamicProperties attribute.

@kayw-geek
Copy link
Author

@canvural Thank you for the clarification. I didn't know about this. I'll close this PR.

@kayw-geek kayw-geek closed this Jun 27, 2025
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

Successfully merging this pull request may close these issues.

3 participants