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

Possible wrong return type in DB::transaction method for Laravel #1620

Open
egesu opened this issue Nov 28, 2024 · 0 comments
Open

Possible wrong return type in DB::transaction method for Laravel #1620

egesu opened this issue Nov 28, 2024 · 0 comments
Labels

Comments

@egesu
Copy link

egesu commented Nov 28, 2024

Versions:

  • ide-helper Version: 3.2.2
  • Laravel Version: 11.34.2
  • PHP Version: 8.3.14

Description:

I had Expected type 'App\Models\BalanceActivity'. Found 'Illuminate\Database\TReturn' kind of IDE errors after I upgrade my dependencies minor versions.

    /**
     * @param array<string, mixed> $data
     * @param array<string, float> $balanceData
     */
    public function create(array $data, array $balanceData): BalanceActivity
    {
        return DB::transaction(function () use ($data, $balanceData) {
            return $this->unsafeCreate($data, $balanceData);
        }, 1);
    }

Same happens if I add return type hint to DB::transaction parameter:

    /**
     * @param array<string, mixed> $data
     * @param array<string, float> $balanceData
     */
    public function create(array $data, array $balanceData): BalanceActivity
    {
        return DB::transaction(function () use ($data, $balanceData): BalanceActivity {
            return $this->unsafeCreate($data, $balanceData);
        }, 1);
    }

Signature for unsafeCreate method:

public function unsafeCreate(array $data, array $balanceData): BalanceActivity

I was not sure about the problem is about Intellephense or Laravel or ide-helper then I tried changing the _ide_helper.php file. Notice the return line in docblock.

Before:

/**
         * 
         *
         * @template TReturn of mixed
         * 
         * Execute a Closure within a transaction.
         * @param (\Closure(static): TReturn) $callback
         * @param int $attempts
         * @return \Illuminate\Database\TReturn 
         * @throws \Throwable
         * @static 
         */        public static function transaction($callback, $attempts = 1)
        {            //Method inherited from \Illuminate\Database\Connection         
                        /** @var \Illuminate\Database\PostgresConnection $instance */
                        return $instance->transaction($callback, $attempts);
        }

After:

/**
         * 
         *
         * @template TReturn of mixed
         * 
         * Execute a Closure within a transaction.
         * @param (\Closure(static): TReturn) $callback
         * @param int $attempts
         * @return TReturn 
         * @throws \Throwable
         * @static 
         */        public static function transaction($callback, $attempts = 1)
        {            //Method inherited from \Illuminate\Database\Connection         
                        /** @var \Illuminate\Database\PostgresConnection $instance */
                        return $instance->transaction($callback, $attempts);
        }

All the issues are gone now. I was planning to open a PR but could not find how this code is generated.

@egesu egesu added the bug label Nov 28, 2024
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

1 participant