[Bug]: Assigning `$this->foo` in `beforeEach` is flagged as an undefined property, because `TestCall` doesn't allow dynamic properties

Open Beginner friendly
#2,080 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
62/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
php
Domain
testing

Research direction

Start in src/PendingCalls/TestCall.php, where the __get method exists and no __set or #[\AllowDynamicProperties] attribute is declared, and compare with the generated test case class in src/Factories/TestCaseFactory.php around line 169. Check src/Functions.php around line 63 for the @param-closure-this TestCall annotation. Done when a property write on $this inside beforeEach passes static analysis, and the existing test suite still passes.

Written by the indexing model from the issue text.

Description

What Happened

Assigning state to $this in a beforeEach (or in any test/it/afterEach closure) is reported by editors as an undefined property. Intelephense reports P1014, Undefined property '$foo'. Reading the same property doesn't produce a warning.

beforeEach(function () {
    $this->foo = new Foo(); // Undefined property '$foo'.
});

it('uses foo', function () {
    expect($this->foo)->toBeInstanceOf(Foo::class); // no warning
});

The tests run fine. The warning comes from the declared type of $this:

  • Since #1628, test, it, beforeEach and afterEach declare @param-closure-this TestCall (Functions.php#L63).
  • TestCall has __get (TestCall.php#L718), which is why reads pass. It has no __set and no #[\AllowDynamicProperties], so writes are flagged, and PHP 8.2+ treats them as deprecated dynamic properties.
  • At runtime $this is the generated test case class, which is declared #[\AllowDynamicProperties] (TestCaseFactory.php#L169). So the declared type is stricter than the real one, and the documented $this->foo = … pattern in beforeEach gets flagged.

I expected $this in these closures to accept property writes, the same as it does at runtime.

How to Reproduce
  1. Open a project using Pest 4.x in VS Code with Intelephense 1.15 or later (I'm on 1.18.5), with undefined-property diagnostics enabled, which is the default.

  2. Add a test file:

    <?php
    
    beforeEach(function () {
        $this->value = 1;
    });
    
    it('reads the value', function () {
        expect($this->value)->toBe(1);
    });
    
  3. The assignment in beforeEach is flagged Undefined property '$value'. The test passes.

Sample Repository

None. The file above is enough to reproduce it in any Pest 4 project.

Pest Version

4.7.8. The same annotations and TestCall are on the current 4.x branch and in v5.3.7.

PHP Version

8.5.13

Operation System

macOS

Notes

Possible fix: add #[\AllowDynamicProperties] to Pest\PendingCalls\TestCall. That matches the runtime class, and nothing assigns properties to a TestCall instance today, so it shouldn't change any runtime behaviour. It would finish what #1628 started: #1628 fixed method calls and reads on $this, but not writes. #996 already made a similar fix for PHPStan by adding HigherOrderTapProxy and Expectation to universalObjectCratesClasses in extension.neon. TestCall could be added there as well, if you want PHPStan users covered too.

Related reports: Intelephense users have hit this repeatedly: bmewburn/vscode-intelephense#3446, bmewburn/vscode-intelephense#3458 and bmewburn/vscode-intelephense#3502. The Intelephense maintainer's position is that it correctly follows Pest's @param-closure-this type, and the workaround offered is a project-level stub that redeclares beforeEach/it with a custom TestCase. A fix in Pest would remove the need for that in every project. Discussion #1469 asks a related question about $this in beforeEach.

Dominant language
PHP
Stars
11.8k
Forks
563
Avg merge
2d 21h
Merged PRs (30d)
39

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from pestphp/pest

All issues in pestphp/pest

Similar issues

More PHP issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.