phpstan/phpstan-nette

Missing dynamic return type for Nette\Http\Session::getSection()

KacerCZ opened this issue · 4 comments

Method Nette\Http\Session::getSection() has optional second parameter which can change class of returned object.
This extension does not support it and always returns Nette\Http\SessionSection class.

+1

Hi, could you provide PHP code examples of these 2 states? (I don't use Nette on this level)
I might try to add the extension then 👍

Here is the definition of method: https://github.com/nette/http/blob/04224e7fd25c70390419b463f4e2b26042ea5e2a/src/Http/Session.php#L314

In following examples $sessionHandler is instance of \Nette\Http\Session.

Example using default class for session section:

$section = $sessionHandler->getSection('mySection');

Variable $section contains instance of \Nette\Http\SessionSection.

Example using diferent class for session section:

class MySection extends \Nette\Http\SessionSection {}

$section = $sessionHandler->getSection('mySection', MySection::class);

Variable $section contains instance of MySection.

lulco commented

see https://github.com/phpstan/phpstan-nette/blob/1.1.x/stubs/ComponentModel/Container.stub

I think this is similar case and you can handle it with stub. I think something like this:

/**
  * @template T of \Nette\Http\SessionSection
  * @phpstan-param class-string<T> $class
  * @phpstan-return T
  */
public function getSection(string $section, string $class = SessionSection::class): \Nette\Http\SessionSection