Skip to content

Commit 1d686e4

Browse files
committed
fixup! Update ComponentModel\Container stub for component-model 3.1.0
1 parent 9407aa9 commit 1d686e4

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

stubs/ComponentModel/Container.stub

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Container extends Component implements IContainer
1010
* @phpstan-param null|class-string<T> $filterType
1111
* @phpstan-return ($filterType is null ? \Iterator<int|string, \Nette\ComponentModel\IComponent> : \Iterator<int|string, T>)
1212
*/
13-
public function getComponents(bool $deep = false, string $filterType = null): \Iterator
13+
public function getComponents(bool $deep = false, ?string $filterType = null): \Iterator
1414
{
1515
// nothing
1616
}

stubs/ComponentModel/Container_3_1.stub

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Container extends Component implements IContainer
1414
* : ($filterType is null ? \Iterator<int|string, \Nette\ComponentModel\IComponent> : \Iterator<int|string, T>)
1515
* )
1616
*/
17-
public function getComponents(bool $deep = false, string $filterType = null): iterable
17+
public function getComponents(bool $deep = false, ?string $filterType = null): iterable
1818
{
1919
// nothing
2020
}

tests/Type/Nette/data/componentModel.php

+37
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace PHPStan\Type\Nette\Data\ComponentModel;
44

55
use Nette\Application\UI\Control;
6+
use Nette\Application\UI\Form;
7+
use Nette\Forms\Container;
68
use function PHPStan\Testing\assertType;
79

810
class 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 {
5678
assertType('PHPStan\Type\Nette\Data\ComponentModel\AnotherControl', $overrideCreateControl->getComponent('unknown', false));
5779
assertType('PHPStan\Type\Nette\Data\ComponentModel\AnotherControl', $overrideCreateControl->getComponent('unknown', true));
5880
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

Comments
 (0)