psalm icon indicating copy to clipboard operation
psalm copied to clipboard

Cannot access value on variable $links using a int<1, max> offset, expecting int<0, 2>

Open zeleniy opened this issue 1 year ago • 8 comments

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)
                ]);
            }
        }
    }
}

zeleniy avatar Feb 08 '24 16:02 zeleniy

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.

psalm-github-bot[bot] avatar Feb 08 '24 16:02 psalm-github-bot[bot]

https://psalm.dev/r/f22de3b6bf

zeleniy avatar Feb 08 '24 16:02 zeleniy

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>

psalm-github-bot[bot] avatar Feb 08 '24 16:02 psalm-github-bot[bot]

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>

psalm-github-bot[bot] avatar Feb 08 '24 16:02 psalm-github-bot[bot]

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

weirdan avatar Feb 08 '24 17:02 weirdan

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!

psalm-github-bot[bot] avatar Feb 08 '24 17:02 psalm-github-bot[bot]

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.

zeleniy avatar Feb 08 '24 17:02 zeleniy

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!

psalm-github-bot[bot] avatar Feb 08 '24 17:02 psalm-github-bot[bot]