Skip to content
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

TestCase::$backupGlobals is declared as boolean but its initial value is NULL #4307

Closed
morozov opened this issue Jun 20, 2020 · 3 comments
Closed
Labels
type/bug Something is broken

Comments

@morozov
Copy link
Contributor

morozov commented Jun 20, 2020

Q A
PHPUnit version 9.2.3
PHP version 7.4.7
Installation Method Composer

Summary

In a project that uses PHPUnit and Psalm, Psalm detects the issue in PHPUnit that cannot be fixed in the project and therefore have to be suppressed.

Current behavior

ERROR: PropertyNotSetInConstructor - tests/ExampleTest.php:7:7 - Property 
Test\ExampleTest::$backupGlobals is not defined in constructor of Test\ExampleTest and in any 
methods called in the constructor (see https://psalm.dev/074)
class ExampleTest extends TestCase

The above is caused by a boolean property being non-initialized:

  1. /**
    * @var bool
    */
    protected $backupGlobals;
  2. public function __construct(?string $name = null, array $data = [], $dataName = '')
    {
    if ($name !== null) {
    $this->setName($name);
    }
    $this->data = $data;
    $this->dataName = $dataName;
    }

How to reproduce

<?php declare(strict_types=1);

namespace Test;

use PHPUnit\Framework\TestCase;

class ExampleTest extends TestCase
{
    /**
     * @test
     */
    public function testExample() : void
    {
        $this->assertTrue(true);
    }
}

See the full demo project in the gist:

$ composer install
$ vendor/bin/psalm

Since the project's tests extend and use the PHPUnit test classes, suppressing errors in the vendor/ directory doesn't help.

Expected behavior

The value of $backupGlobals is always within its declared type constraints.

@morozov morozov added the type/bug Something is broken label Jun 20, 2020
@morozov morozov changed the title Psalms decets issues in PHPUnit Psalms detects issues in PHPUnit Jun 20, 2020
@sebastianbergmann
Copy link
Owner

Can you please open separate issues? Thanks!

@morozov morozov changed the title Psalms detects issues in PHPUnit TestCase::$backupGlobals is declared as boolean but its initial value is NULL Jun 20, 2020
@morozov
Copy link
Contributor Author

morozov commented Jun 20, 2020

Please see #4309.

@sebastianbergmann
Copy link
Owner

Please see #4309.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/bug Something is broken
Projects
None yet
Development

No branches or pull requests

2 participants