Hacktoberfest 2026: los issues que los mantenedores marcaron para octubre, abiertos y aptos para principiantes. Explorar issues de Hacktoberfest

ClosureUsesThisRule produces false positive with Prophecy will()

Abierto
#142 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
3/5
Tiempo estimado
1-2 días
Aptitud para principiantes
35/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Estancado
Stack tecnológico
php
Área
devtools

Línea de trabajo

Start with the supplied PHPUnit and Prophecy reproducer and inspect the implementation of ClosureUsesThisRule. Confirm that the closure passed to Prophecy's will() is analyzed with the correct $this context, then use the example as a regression test whose expected result is no false positive.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

If you replace $that by $this in the closure of the following, the test will fail.

At runtime, the type of $this is \Prophecy\Prophecy\ObjectProphecy and the type of $that is MyTest.

<?php

declare(strict_types=1);

// Requires phpunit/phpunit, phpspec/prophecy and phpspec/prophecy-phpunit

namespace foo;

use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;

class MyTarget
{
    public function run(string $input): string
    {
        return $input;
    }
}

class MyTest extends TestCase
{
    use ProphecyTrait;

    private array $stuff = ['foo' => 'bar'];

    public function testMyMock(): void
    {
        $prophecy = $this->prophesize(MyTarget::class);
        $that = $this;
        $prophecy->run(Argument::type('string'))->will(
            function (array $args) use ($that): string {
                return $that->stuff[$args[0]] ?? $args[0];
            }
        );
        $target = $prophecy->reveal();
        $result = $target->run('foo');
        self::assertSame('bar', $result);
    }
}
Lenguaje dominante
PHP
Estrellas
709
Forks
62
Merge medio
5 d 19 h
PR fusionados (30 d)
3

Guía de contribución

No hay ninguna guía de contribución indexada para este repositorio

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de phpstan/phpstan-strict-rules

Todos los issues de phpstan/phpstan-strict-rules

Issues similares

Más issues de PHP

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.