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

Aperta Adatta ai principianti
#2,080 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 2 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
62/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
php
Ambito
testing

Direzione di ricerca

Inizia da src/PendingCalls/TestCall.php, dove esiste il metodo __get e non sono dichiarati né __set né l'attributo #[\AllowDynamicProperties], e confronta con la classe di test generata in src/Factories/TestCaseFactory.php intorno alla riga 169. Controlla src/Functions.php intorno alla riga 63 per l'annotazione @param-closure-this TestCall. È completato quando una scrittura di proprietà su $this dentro beforeEach supera l'analisi statica e la suite di test esistente continua a passare.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
PHP
Stelle
11.8k
Fork
563
Merge medio
2g 21h
PR unite (30g)
39

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di pestphp/pest

Tutte le issue di pestphp/pest

Issue simili

Altre issue su PHP

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.