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

Invalid read from CallMap when methods are in inheritance #3406

Closed
simPod opened this issue May 19, 2020 · 2 comments · Fixed by #10351
Closed

Invalid read from CallMap when methods are in inheritance #3406

simPod opened this issue May 19, 2020 · 2 comments · Fixed by #10351
Labels

Comments

@simPod
Copy link
Contributor

simPod commented May 19, 2020

<?php

use RdKafka\Producer;
use const RD_KAFKA_PARTITION_UA;

final class A
{
    public function fcn() : void
    {
        $producer = new Producer();
        $topic = $producer->newTopic('topic');
        $topic->produce(RD_KAFKA_PARTITION_UA, 0, 'payload', 'key');
    }
}

Psalm reports

UndefinedMethod - Method RdKafka\Topic::produce does not exist (see https://psalm.dev/022)
$topic->produce(RD_KAFKA_PARTITION_UA, 0, 'payload', 'key');

RdKafka\Producer extends RdKafka.

<?php

class Producer extends \RdKafka
{
    /**
     * @param string    $topic_name
     * @param TopicConf $topic_conf
     *
     * @return ProducerTopic
     */
    public function newTopic($topic_name, TopicConf $topic_conf = null)
    {
    }
<?php

abstract class RdKafka
{
    /**
     * @param string    $topic_name
     * @param TopicConf $topic_conf
     *
     * @return Topic
     */
    public function newTopic($topic_name, TopicConf $topic_conf = null)
    {
    }

This is correctly defined in CallMap:

'RdKafka::newTopic' => ['RdKafka\Topic', 'topic_name'=>'string', 'topic_conf='=>'?RdKafka\TopicConf'],
'RdKafka\Producer::newTopic' => ['RdKafka\ProducerTopic', 'topic_name'=>'string', 'topic_conf='=>'?RdKafka\TopicConf'],

However, psalm assigns RdKafka::newTopic definition to RdKafka\Producer::newTopic and thinks it returns RdKafka\Topic while it returns RdKafka\ProducerTopic.

https://github.com/simPod/psalm-kafka-broken/commit/58962588fc16d046cdc69ddbd16e77b9d34f4914#diff-3e3f7392d7da765a65d0ece9729444aaR14

@psalm-github-bot
Copy link

Hey @simPod, can you reproduce the issue on https://psalm.dev ?

@weirdan
Copy link
Collaborator

weirdan commented May 19, 2020

I don't think Psalm is mistaken here. Both documentation and extension source code says there's no method newTopic on RdKafka\Producer class. Psalm sees that (via reflection) and uses correct callmap entry.

Basically RdKafka::newTopic has a conditional return type based on LSB, but I'm not sure if it's possible to express that condition given the features we have right now. @muglug, can you advise if it's possible to use type of $this/static as a condition in a conditional return type (I tried static is Type and $this is Type but none of that worked for me)?

robchett added a commit to robchett/psalm that referenced this issue Nov 7, 2023
robchett added a commit to robchett/psalm that referenced this issue Nov 8, 2023
robchett added a commit to robchett/psalm that referenced this issue Nov 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants