33namespace PHPStan \Type \Nette \Data \ComponentModel ;
44
55use Nette \Application \UI \Control ;
6+ use Nette \Application \UI \Form ;
7+ use Nette \Forms \Container ;
68use function PHPStan \Testing \assertType ;
79
810class SomeControl extends Control {
@@ -33,6 +35,26 @@ public function createComponent(string $name): AnotherControl {
3335
3436}
3537
38+ class SomeForm extends Form {
39+ }
40+
41+ function getInstalledVersion (string $ package ): ?string
42+ {
43+ if (!class_exists (InstalledVersions::class)) {
44+ return null ;
45+ }
46+
47+ try {
48+ $ installedVersion = InstalledVersions::getVersion ($ package );
49+ } catch (OutOfBoundsException $ e ) {
50+ return null ;
51+ }
52+
53+ return $ installedVersion ;
54+ }
55+
56+ $ componentModelVersion = self ::getInstalledVersion ('nette/component-model ' );
57+
3658$ bool = rand (0 , 1 ) ? true : false ;
3759
3860$ someControl = new SomeControl ();
@@ -56,3 +78,18 @@ public function createComponent(string $name): AnotherControl {
5678assertType ('PHPStan\Type\Nette\Data\ComponentModel\AnotherControl ' , $ overrideCreateControl ->getComponent ('unknown ' , false ));
5779assertType ('PHPStan\Type\Nette\Data\ComponentModel\AnotherControl ' , $ overrideCreateControl ->getComponent ('unknown ' , true ));
5880assertType ('PHPStan\Type\Nette\Data\ComponentModel\AnotherControl ' , $ overrideCreateControl ->getComponent ('unknown ' , $ bool ));
81+
82+ $ someForm = new SomeForm ();
83+ if ($ componentModelVersion !== null && version_compare ($ componentModelVersion , '3.1.0 ' , '>= ' )) {
84+ assertType ('array<int|string, Nette\ComponentModel\IComponent> ' , $ someForm ->getComponents (false ));
85+ assertType ('array<int|string, Nette\Forms\Container> ' , $ someForm ->getComponents (false , Container::class));
86+ assertType ('Iterator<int|string, Nette\ComponentModel\IComponent> ' , $ someForm ->getComponents (true ));
87+ assertType ('Iterator<int|string, Nette\Forms\Container> ' , $ someForm ->getComponents (true , Container::class));
88+ assertType ('array<int|string, Nette\ComponentModel\IComponent>|Iterator<int|string, Nette\ComponentModel\IComponent> ' , $ someControl ->getComponents ($ bool ));
89+ } else {
90+ assertType ('Iterator<int|string, Nette\ComponentModel\IComponent> ' , $ someForm ->getComponents (false ));
91+ assertType ('Iterator<int|string, Nette\Forms\Container> ' , $ someForm ->getComponents (false , Container::class));
92+ assertType ('Iterator<int|string, Nette\ComponentModel\IComponent> ' , $ someForm ->getComponents (true ));
93+ assertType ('Iterator<int|string, Nette\Forms\Container> ' , $ someForm ->getComponents (true , Container::class));
94+ assertType ('Iterator<int|string, Nette\ComponentModel\IComponent> ' , $ someControl ->getComponents ($ bool ));
95+ }
0 commit comments