Skip to content

Type of child object is not infered if get_class is used outside of switch argument #523

Open
phpstan/phpstan-src
#6383

Description

@Jean85

It's hard to explain this issue that I found, so I'll provide a code example:

<?php declare(strict_types = 1);

class Base {}
class Child1 extends Base {}

function typeHintedFunction(Child1 $object)
{
}

function testPasses(Base $object)
{
	switch (get_class($object)) {
		case Child1::class:
			typeHintedFunction($object);
	}

	$type = get_class($object);
	switch ($type) {
		case Child1::class:
			typeHintedFunction($object);
	}
}

PHPStan raise an error if I use get_class and assign it to a variable, but not if I do it directly in the switch statement: https://phpstan.org/r/2cc127fa-2762-48a5-8a25-56f326f38451

Activity

  1. ondrejmirtes commented on Sep 28, 2017

    @ondrejmirtes
    Member

    Yep, PHPStan is not that smart yet 😊 Try to make the code more understandable for the static analyzer.

    Alternative (also understood) approach is this:

    switch (true) {
        case $object instanceof Child1:
            typeHintedFunction($object);
    }
  2. added this to the Constant values milestone on Sep 28, 2017
  3. Jean85 commented on Sep 28, 2017

    @Jean85
    ContributorAuthor

    For now I've just moved the get_class inside the switch statement since it wasn't an issue, but now that I'm thinking about it, your approach is better performance-wise, since it's not comparting two strings but just checking the type of an instance, which is more correct and faster; thanks!

  4. ondrejmirtes commented on Dec 2, 2020

    @ondrejmirtes
    Member

    Closing this in favour of: #1239

  5. ondrejmirtes commented on Dec 2, 2020

    @ondrejmirtes
    Member

    Oh, this is actually for dependent types.

  6. gbirke commented on Apr 8, 2022

    @gbirke

    I have made another example where this fails for get_class inside a match statement:

    https://phpstan.org/r/d6117a8f-0255-479d-b681-e45396ce79df

  7. phpstan-bot commented on Jun 19, 2023

    @phpstan-bot
    Contributor

    @gbirke After the latest push in 1.11.x, PHPStan now reports different result with your code snippet:

    @@ @@
    -PHP 8.0 – 8.1 (1 error)
    +PHP 8.0 – 8.1
     ==========
     
    -53: Parameter #1 $dingus of class SomeApp\BarAttachment constructor expects SomeApp\BarDingus, SomeApp\Dingus given.
    +No errors
     
     PHP 7.1 – 7.4 (3 errors)
     ==========
    Full report

    PHP 8.0 – 8.1

    No errors

    PHP 7.1 – 7.4 (3 errors)

    Line Error
    53 Syntax error, unexpected ',' on line 53
    53 Syntax error, unexpected T_DOUBLE_ARROW on line 53
    55 Syntax error, unexpected '}' on line 55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions