[Bug]: Assigning `$this->foo` in `beforeEach` is flagged as an undefined property, because `TestCall` doesn't allow dynamic properties
Maintainers usually reply within 2 days
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 62/100
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,beforeEachandafterEachdeclare@param-closure-this TestCall(Functions.php#L63). TestCallhas__get(TestCall.php#L718), which is why reads pass. It has no__setand no#[\AllowDynamicProperties], so writes are flagged, and PHP 8.2+ treats them as deprecated dynamic properties.- At runtime
$thisis 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 inbeforeEachgets flagged.
I expected $this in these closures to accept property writes, the same as it does at runtime.
How to Reproduce
-
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.
-
Add a test file:
<?php beforeEach(function () { $this->value = 1; }); it('reads the value', function () { expect($this->value)->toBe(1); }); -
The assignment in
beforeEachis flaggedUndefined 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
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from pestphp/pest
-
[Bug]: [mutate] A --mutate run given --filter scores every mutant as testedPossibly taken @jfatr claimed this 2 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 2 days
-
[Bug]: [mutate] Windows: StreamWrapper::stream_lock() always takes LOCK_SH, so LOCK_EX writes fail in a mutant child and the mutant counts as killedPossibly taken @joshdaugherty claimed this 2 days ago. Open
Difficulty 1/5 1-3 hours Newbie friendliness 88/100
Maintainers usually reply within 2 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 2 days
-
[Bug]: --mutate hides unreadable files, so mutants behind a failed read can never be killedMay be free again A pull request for this issue was closed without being merged. Openbug
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
Maintainers usually reply within 2 days
-
[Bug]: [mutate] Windows: @pest-mutate-ignore comments land on line 1 in files with LF line endingsPossibly taken @webdevsamran claimed this 64 days ago. Openbug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 2 days
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 63/100
phpstan/phpstan-phpunit#361 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
HE-Arc/le-panier-du-coin#49 ·
Maintainers usually reply within 1 day
-
15.0
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
woocommerce/woocommerce-subscriptions-importer-exporter#298 ·
-
bug: juros do parcelamento travados em 1x no gateway Rede Débito e Crédito 3DS (woo-rede 5.6.0)Openbug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
LinkNacional/woo-rede#367 ·