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

Getting InvalidDocblock when using the non-negative-int scalar type on a class with namespace #9070

Closed
eopnetto opened this issue Jan 6, 2023 · 2 comments
Labels
bug easy problems Issues that can be fixed without background knowledge of Psalm good first issue Help wanted

Comments

@eopnetto
Copy link
Contributor

eopnetto commented Jan 6, 2023

I'm trying to validate that the index of an array is a non-negative integer, which works fine in the example below:
https://psalm.dev/r/9113171040

However, I'm getting an InvalidDocblock error when using non-negative-int on a class with a namespace:
https://psalm.dev/r/21478194fc

namespace App;

class Entity {
    /**
    * @psalm-var array<non-negative-int, string>
    */
    private $actions = [];
    
    public function __construct() {
        $this->actions[-1]= 'test'; // I would expect an `InvalidPropertyAssignmentValue` error here, but I get an `InvalidDocblock` instead
    }
}

Note: it works if add a backslash before: \non-negative-int
https://psalm.dev/r/33769b88c8

I'm assuming this is a bug, right?! Or is it the expected behavior?

@psalm-github-bot
Copy link

I found these snippets:

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

class Entity {

    /**
    * @psalm-var array<non-negative-int, string>
	*/
    private $actions = [];
    
    public function __construct() {
        $this->actions[-1]= 'test';
    }
}
Psalm output (using commit 8329309):

ERROR: InvalidPropertyAssignmentValue - 11:9 - $this->actions with declared type 'array<int<0, max>, string>' cannot be assigned type 'array{-1: 'test', ...<int<0, max>, string>}'
https://psalm.dev/r/21478194fc
<?php

namespace App;

class Entity {

    /**
    * @psalm-var array<non-negative-int, string>
	*/
    private $actions = [];
    
    public function __construct() {
        $this->actions[-1]= 'test';
    }
}
Psalm output (using commit 8329309):

ERROR: InvalidDocblock - 10:5 - array<non-negative-int, string> is not a valid type (from /var/www/vhosts/psalm.dev/httpdocs/src/somefile.php:7)

INFO: MixedArrayAssignment - 13:9 - Cannot access array value on mixed variable $this->actions[int]

INFO: MissingPropertyType - 10:13 - Property App\Entity::$actions does not have a declared type - consider array|mixed
https://psalm.dev/r/33769b88c8
<?php

namespace App;

class Entity {

    /**
    * @psalm-var array<\non-negative-int, string>
	*/
    private $actions = [];
    
    public function __construct() {
        $this->actions[-1]= 'test';
    }
}
Psalm output (using commit 8329309):

ERROR: InvalidPropertyAssignmentValue - 13:9 - $this->actions with declared type 'array<int<0, max>, string>' cannot be assigned type 'array{-1: 'test', ...<int<0, max>, string>}'

@eopnetto eopnetto changed the title Getting InvalidDocblock when using an int-range scalar type on class with namespace Getting InvalidDocblock when using an int-range scalar type on a class with namespace Jan 6, 2023
@eopnetto eopnetto changed the title Getting InvalidDocblock when using an int-range scalar type on a class with namespace Getting InvalidDocblock when using the non-negative-int scalar type on a class with namespace Jan 6, 2023
@orklah
Copy link
Collaborator

orklah commented Jan 7, 2023

I answered here:
#9069 (comment)

There's a few keywords missing, are you up for a PR? :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug easy problems Issues that can be fixed without background knowledge of Psalm good first issue Help wanted
Projects
None yet
Development

No branches or pull requests

3 participants