psalm
psalm copied to clipboard
Cannot access value on variable $links using a int<1, max> offset, expecting int<0, 2>
Got a error with completely valid code:
ERROR: [InvalidArrayOffset](https://psalm.dev/115) - 25:36 - Cannot access value on variable $links using a int<1, max> offset, expecting int<0, 2>
<?php
namespace Database\Seeders;
class Seeder {
}
class ProductsCross {
public static function create(array $array): void {
}
}
class CrossSeeder extends Seeder
{
/**
* Run the database seeds.
* @psalm-suppress PossiblyUnusedMethod
*/
public function run(): void
{
foreach ([[1, 21, 22], [3, 33]] as $links) {
for ($i = 1; $i < \count($links); $i++) {
ProductsCross::create([
'productId' => $links[0],
'crossId' => $links[$i],
'isOem' => (bool) \rand(0, 1)
]);
}
}
}
}
Hey @zeleniy, can you reproduce the issue on https://psalm.dev? These will be used as phpunit tests when implementing the feature or fixing this bug.
https://psalm.dev/r/f22de3b6bf
I found these snippets:
https://psalm.dev/r/f22de3b6bf
<?php
namespace Database\Seeders;
class Seeder {
}
class ProductsCross {
public static function create(array $array): void {
}
}
class CrossSeeder extends Seeder
{
/**
* Run the database seeds.
* @psalm-suppress PossiblyUnusedMethod
*/
public function run(): void
{
foreach ([[1, 21, 22], [3, 33]] as $links) {
for ($i = 1; $i < \count($links); $i++) {
ProductsCross::create([
'productId' => $links[0],
'crossId' => $links[$i],
'isOem' => (bool) \rand(0, 1)
]);
}
}
}
}
Psalm output (using commit 4b2c698):
ERROR: InvalidArrayOffset - 25:36 - Cannot access value on variable $links using a int<1, max> offset, expecting int<0, 2>
I found these snippets:
https://psalm.dev/r/a2e5b01ae7
<?php
namespace Database\Seeders;
class Seeder {
}
class ProductsCross {
public static function create(array $array): void {
}
}
class CrossSeeder extends Seeder
{
/**
* Run the database seeds.
* @psalm-suppress PossiblyUnusedMethod
*/
public function run(): void
{
foreach ([[1, 21, 22], [3, 33]] as $links) {
for ($i = 1; $i < \count($links); $i++) {
ProductsCross::create([
'productId' => $links[0],
/**
* @psalm-suppress InvalidArrayOffset
*/
'crossId' => $links[$i],
'isOem' => (bool) \rand(0, 1)
]);
}
}
}
}
Psalm output (using commit 4b2c698):
ERROR: InvalidArrayOffset - 28:36 - Cannot access value on variable $links using a int<1, max> offset, expecting int<0, 2>
And suppression does work (I don't see your comment here but I have it in my email notifications), but you need to do it on the statement level: https://psalm.dev/r/de79564cac
I found these snippets:
https://psalm.dev/r/de79564cac
<?php
namespace Database\Seeders;
class Seeder {
}
class ProductsCross {
public static function create(array $array): void {
}
}
class CrossSeeder extends Seeder
{
/**
* Run the database seeds.
* @psalm-suppress PossiblyUnusedMethod
*/
public function run(): void
{
foreach ([[1, 21, 22], [3, 33]] as $links) {
for ($i = 1; $i < \count($links); $i++) {
/** @psalm-suppress InvalidArrayOffset */
ProductsCross::create([
'productId' => $links[0],
'crossId' => $links[$i],
'isOem' => (bool) \rand(0, 1)
]);
}
}
}
}
Psalm output (using commit 4b2c698):
No issues!
I found these snippets:
https://psalm.dev/r/de79564cac
Yeah, thank you, i am already found how to suppress it. The only problem now is InvalidArrayOffset
itself.
I found these snippets:
https://psalm.dev/r/de79564cac
<?php
namespace Database\Seeders;
class Seeder {
}
class ProductsCross {
public static function create(array $array): void {
}
}
class CrossSeeder extends Seeder
{
/**
* Run the database seeds.
* @psalm-suppress PossiblyUnusedMethod
*/
public function run(): void
{
foreach ([[1, 21, 22], [3, 33]] as $links) {
for ($i = 1; $i < \count($links); $i++) {
/** @psalm-suppress InvalidArrayOffset */
ProductsCross::create([
'productId' => $links[0],
'crossId' => $links[$i],
'isOem' => (bool) \rand(0, 1)
]);
}
}
}
}
Psalm output (using commit 4b2c698):
No issues!