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

NonInvariantDocblockPropertyType false positive when child property uses psalm-type #8515

Open
MauricioFauth opened this issue Sep 26, 2022 · 3 comments
Labels

Comments

@MauricioFauth
Copy link
Contributor

MauricioFauth commented Sep 26, 2022

It works if I declare the same type for both properties:
https://psalm.dev/r/f3c5eed362

But it does not work when using @psalm-type:
https://psalm.dev/r/d5ddb3e4fc

Also: https://psalm.dev/r/6facf65dd6

See #6575 (comment):

This may be a different bug where aliases are wrongly checked when looking at invariance.

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/f3c5eed362
<?php

abstract class Father {
    /** @var array<string, string> */
    public array $a = [];
}

class Child extends Father {
    /** @var array<string, string> */
    public array $a = [
    	'key' => 'value',
	];
}
Psalm output (using commit 028ac7f):

No issues!
https://psalm.dev/r/d5ddb3e4fc
<?php

/**
 * @psalm-type FatherType = array<string, string>
 */
abstract class Father {
    /** @var FatherType */
    public array $a = [];
}

/**
 * @psalm-import-type FatherType from Father
 */
class Child extends Father {
    /** @var FatherType */
    public array $a = [
    	'key' => 'value',
	];
}
Psalm output (using commit 028ac7f):

ERROR: NonInvariantDocblockPropertyType - 16:18 - Property Child::$a has type type-alias(Father::FatherType), not invariant with Father::$a of type array<string, string>
https://psalm.dev/r/6facf65dd6
<?php

declare(strict_types=1);

namespace Common;

/**
 * @psalm-type MessageTemplatesType = array<string, string>
 */
abstract class AbstractClass
{
    /**
     * @psalm-var MessageTemplatesType Validation error message templates to be used when validators detect
     *         errors that needs showing.
     */
    protected array $messageTemplates = [];
}

namespace App;

use Common\AbstractClass;

/**
 * @psalm-import-type MessageTemplatesType from AbstractClass
 */
class ConcreteClass extends AbstractClass
{
    /** @psalm-var MessageTemplatesType */
    protected array $messageTemplates = [
        'not_same'  => 'Security validation failed. Please try again.',
        'not_found' => 'We could not find a Use a lasting power of attorney account ' .
            'with that email address and password. Check your details and try again.',
    ];
}
Psalm output (using commit 028ac7f):

ERROR: NonInvariantDocblockPropertyType - 29:21 - Property App\ConcreteClass::$messageTemplates has type type-alias(Common\AbstractClass::MessageTemplatesType), not invariant with Common\AbstractClass::$messageTemplates of type array<string, string>

@orklah orklah added the bug label Sep 27, 2022
@Hikariii
Copy link

Just came accross the same issue: https://psalm.dev/r/6e05abde32

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/6e05abde32
<?php

/**
 * @psalm-type dataType = array<array-key, mixed>
 */
abstract class A {
    /** @psalm-var dataType */
    protected array $data = [];
}

/**
 * @psalm-import-type dataType from A
 */
class B extends A {
    /** @psalm-var dataType */
    protected array $data = [ 'test' ];
}
Psalm output (using commit d66981a):

ERROR: NonInvariantDocblockPropertyType - 16:21 - Property B::$data has type type-alias(A::dataType), not invariant with A::$data of type array<array-key, mixed>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants