phpstan / phpstan-nette

Nette Framework class reflection extension for PHPStan & framework-specific rules
MIT License
100 stars 35 forks source link

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

Open KacerCZ opened 3 years ago

KacerCZ commented 3 years ago

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.

lukasojd commented 2 years ago

+1

TomasVotruba commented 2 years ago

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 :+1:

KacerCZ commented 2 years ago

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 1 year ago

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