3
3
namespace PHPStan \Type \Nette \Data \ComponentModel ;
4
4
5
5
use Nette \Application \UI \Control ;
6
+ use Nette \Application \UI \Form ;
7
+ use Nette \Forms \Container ;
6
8
use function PHPStan \Testing \assertType ;
7
9
8
10
class SomeControl extends Control {
@@ -33,6 +35,26 @@ public function createComponent(string $name): AnotherControl {
33
35
34
36
}
35
37
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
+
36
58
$ bool = rand (0 , 1 ) ? true : false ;
37
59
38
60
$ someControl = new SomeControl ();
@@ -56,3 +78,18 @@ public function createComponent(string $name): AnotherControl {
56
78
assertType ('PHPStan\Type\Nette\Data\ComponentModel\AnotherControl ' , $ overrideCreateControl ->getComponent ('unknown ' , false ));
57
79
assertType ('PHPStan\Type\Nette\Data\ComponentModel\AnotherControl ' , $ overrideCreateControl ->getComponent ('unknown ' , true ));
58
80
assertType ('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