Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions tests/PHPStan/Analyser/nsrt/static-with-this-chained.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace StaticWithThisChained;

use function PHPStan\Testing\assertType;

class Foo
{
/** @var $this */
public $propThis;

/** @var static */
public $propStatic;

/** @var static */
public static $propStaticStatic;

/**
* @return $this
*/
public function getThis()
{
return $this;
}

/**
* @return static
*/
public function newStatic()
{
return new static();
}

/**
* @return static
*/
public static function newStaticStatic()
{
return new static();
}

public function testMethodChained(): void
{
assertType('$this(StaticWithThisChained\Foo)', $this->getThis());
assertType('static(StaticWithThisChained\Foo)', $this->newStatic());
assertType('static(StaticWithThisChained\Foo)', static::newStaticStatic());
assertType('StaticWithThisChained\Foo', self::newStaticStatic());
assertType('StaticWithThisChained\Foo', Foo::newStaticStatic());

assertType('$this(StaticWithThisChained\Foo)', $this->getThis()->getThis());
assertType('static(StaticWithThisChained\Foo)', $this->newStatic()->getThis());
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Help needed - how can I mutate the scope to be static when static return type is analysed?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ondrejmirtes please see https://phpstan.org/r/29fcfdb5-f19e-4df2-9177-61ad88490c64 repro - do you know where the issue comes from and how it can be fixed? (static must never be converted to $this )

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@staabm might I ask you for a help? I have looked into the fix several times, but I am not familiar with mutating the scope logic and I do not know how to fix this myself.

assertType('static(StaticWithThisChained\Foo)', static::newStaticStatic()->getThis());
assertType('StaticWithThisChained\Foo', self::newStaticStatic()->getThis());
assertType('StaticWithThisChained\Foo', Foo::newStaticStatic()->getThis());
}

public function testPropertyChained(): void
{
/*
assertType('$this(StaticWithThisChained\Foo)', $this->propThis);
assertType('static(StaticWithThisChained\Foo)', $this->propStatic);
assertType('static(StaticWithThisChained\Foo)', static::$propStaticStatic);
assertType('StaticWithThisChained\Foo', self::$propStaticStatic);
assertType('StaticWithThisChained\Foo', Foo::$propStaticStatic);

assertType('$this(StaticWithThisChained\Foo)', $this->propThis->propThis);
assertType('static(StaticWithThisChained\Foo)', $this->propStatic->propThis);
assertType('static(StaticWithThisChained\Foo)', static::$propStaticStatic->propThis);
assertType('StaticWithThisChained\Foo', self::$propStaticStatic->propThis);
assertType('StaticWithThisChained\Foo', Foo::$propStaticStatic->propThis);
*/
}
}
Loading