Skip to content

Issue with Type of static::class and self::class. #4233

@VincentLanglet

Description

@VincentLanglet

Bug report

While working on pepakriz/phpstan-exception-rules#138

I get the following results in

new ReflectionClass(static::class);

the static::class is a

object(PHPStan\Type\Constant\ConstantStringType)#24493 (2) {
    ["value":"PHPStan\Type\Constant\ConstantStringType":private]=>
    string(65) "Pepakriz\PHPStanExceptionRules\Rules\PhpInternalFunctions\Example"
    ["isClassString":"PHPStan\Type\Constant\ConstantStringType":private]=>
    bool(false)
  }

When in

new ReflectionClass(self::class);

the self::class is a

  object(PHPStan\Type\Constant\ConstantStringType)#24464 (2) {
    ["value":"PHPStan\Type\Constant\ConstantStringType":private]=>
    string(65) "Pepakriz\PHPStanExceptionRules\Rules\PhpInternalFunctions\Example"
    ["isClassString":"PHPStan\Type\Constant\ConstantStringType":private]=>
    bool(true)
  }

Does the fact that the bool value is different is correct ? @ondrejmirtes

But more important in

new ReflectionClass(rand(0, 1) === 0 ? self::class : Throwable::class);

the argument is

  object(PHPStan\Type\Constant\ConstantStringType)#25258 (2) {
    ["value":"PHPStan\Type\Constant\ConstantStringType":private]=>
    string(65) "Pepakriz\PHPStanExceptionRules\Rules\PhpInternalFunctions\Example"
    ["isClassString":"PHPStan\Type\Constant\ConstantStringType":private]=>
    bool(true)
  }
  [1]=>
  object(PHPStan\Type\Constant\ConstantStringType)#25269 (2) {
    ["value":"PHPStan\Type\Constant\ConstantStringType":private]=>
    string(9) "Throwable"
    ["isClassString":"PHPStan\Type\Constant\ConstantStringType":private]=>
    bool(true)
  }

But as soon as I changed self by static

new ReflectionClass(rand(0, 1) === 0 ? static::class : Throwable::class);

the ConstantStringType become a

PHPStan\Type\Generic\GenericClassStringType

This behaviour exists since the version 0.12.55.

In 0.12.54, it was correctly

array(2) {
  [0]=>
  object(PHPStan\Type\Constant\ConstantStringType)#24409 (2) {
    ["value":"PHPStan\Type\Constant\ConstantStringType":private]=>
    string(65) "Pepakriz\PHPStanExceptionRules\Rules\PhpInternalFunctions\Example"
    ["isClassString":"PHPStan\Type\Constant\ConstantStringType":private]=>
    bool(true)
  }
  [1]=>
  object(PHPStan\Type\Constant\ConstantStringType)#24420 (2) {
    ["value":"PHPStan\Type\Constant\ConstantStringType":private]=>
    string(9) "Throwable"
    ["isClassString":"PHPStan\Type\Constant\ConstantStringType":private]=>
    bool(true)
  }
}

(But it weird that isClassString is true, when it's false for new ReflectionClass(static::class);)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions