-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update ComponentModel\Container stub for component-model 3.1.0
That version changes the return type to array when `$deep` argument is `false` (default): nette/component-model@7f613ee It also deprecates the arguments but we cannot add deprecated annotation to those. nette/component-model@4e0946a
- Loading branch information
Showing
7 changed files
with
175 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Stubs\Nette; | ||
|
||
use Composer\InstalledVersions; | ||
use OutOfBoundsException; | ||
use PHPStan\PhpDoc\StubFilesExtension; | ||
use function class_exists; | ||
use function dirname; | ||
use function version_compare; | ||
|
||
class StubFilesExtensionLoader implements StubFilesExtension | ||
{ | ||
|
||
public function getFiles(): array | ||
{ | ||
$stubsDir = dirname(dirname(dirname(__DIR__))) . '/stubs'; | ||
$path = $stubsDir; | ||
|
||
$files = []; | ||
|
||
$componentModelVersion = self::getInstalledVersion('nette/component-model'); | ||
if ($componentModelVersion !== null && version_compare($componentModelVersion, '3.1.0', '>=')) { | ||
$files[] = $path . '/ComponentModel/Container_3_1.stub'; | ||
} else { | ||
$files[] = $path . '/ComponentModel/Container.stub'; | ||
} | ||
|
||
return $files; | ||
} | ||
|
||
private static function getInstalledVersion(string $package): ?string | ||
{ | ||
if (!class_exists(InstalledVersions::class)) { | ||
return null; | ||
} | ||
|
||
try { | ||
$installedVersion = InstalledVersions::getVersion($package); | ||
} catch (OutOfBoundsException $e) { | ||
return null; | ||
} | ||
|
||
return $installedVersion; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace Nette\ComponentModel; | ||
|
||
class Container extends Component implements IContainer | ||
{ | ||
|
||
/** | ||
* @template T of \Nette\ComponentModel\IComponent | ||
* @phpstan-param null|class-string<T> $filterType | ||
* @phpstan-return ( | ||
* $deep is false | ||
* ? ($filterType is null ? array<int|string, \Nette\ComponentModel\IComponent> : array<int|string, T>) | ||
* : ($filterType is null ? \Iterator<int|string, \Nette\ComponentModel\IComponent> : \Iterator<int|string, T>) | ||
* ) | ||
*/ | ||
public function getComponents(bool $deep = false, ?string $filterType = null): iterable | ||
{ | ||
// nothing | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
tests/Type/Nette/ComponentModelContainerDynamicReturnTypeExtensionTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Nette; | ||
|
||
use Composer\InstalledVersions; | ||
use OutOfBoundsException; | ||
use PHPStan\Testing\TypeInferenceTestCase; | ||
use function class_exists; | ||
use function version_compare; | ||
|
||
class ComponentModelContainerDynamicReturnTypeExtensionTest extends TypeInferenceTestCase | ||
{ | ||
|
||
/** | ||
* @return iterable<string, mixed[]> | ||
*/ | ||
public function dataFileAsserts(): iterable | ||
{ | ||
$componentModelVersion = self::getInstalledVersion('nette/component-model'); | ||
|
||
$suffix = $componentModelVersion !== null && version_compare($componentModelVersion, '3.1.0', '>=') ? '31' : ''; | ||
|
||
yield from self::gatherAssertTypes(__DIR__ . "/data/componentModelContainer{$suffix}.php"); | ||
} | ||
|
||
/** | ||
* @dataProvider dataFileAsserts | ||
* @param mixed ...$args | ||
*/ | ||
public function testFileAsserts( | ||
string $assertType, | ||
string $file, | ||
...$args | ||
): void | ||
{ | ||
$this->assertFileAsserts($assertType, $file, ...$args); | ||
} | ||
|
||
public static function getAdditionalConfigFiles(): array | ||
{ | ||
return [ | ||
__DIR__ . '/phpstan.neon', | ||
]; | ||
} | ||
|
||
private static function getInstalledVersion(string $package): ?string | ||
{ | ||
if (!class_exists(InstalledVersions::class)) { | ||
return null; | ||
} | ||
|
||
try { | ||
$installedVersion = InstalledVersions::getVersion($package); | ||
} catch (OutOfBoundsException $e) { | ||
return null; | ||
} | ||
|
||
return $installedVersion; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace PHPStan\Type\Nette\Data\ComponentModel; | ||
|
||
use Nette\Application\UI\Form; | ||
use Nette\Forms\Container; | ||
use function PHPStan\Testing\assertType; | ||
|
||
class SomeForm extends Form { | ||
} | ||
|
||
$bool = rand(0, 1) ? true : false; | ||
|
||
$someForm = new SomeForm(); | ||
|
||
assertType('Iterator<int|string, Nette\ComponentModel\IComponent>', $someForm->getComponents(false)); | ||
assertType('Iterator<int|string, Nette\Forms\Container>', $someForm->getComponents(false, Container::class)); | ||
assertType('Iterator<int|string, Nette\ComponentModel\IComponent>', $someForm->getComponents(true)); | ||
assertType('Iterator<int|string, Nette\Forms\Container>', $someForm->getComponents(true, Container::class)); | ||
assertType('Iterator<int|string, Nette\ComponentModel\IComponent>', $someControl->getComponents($bool)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace PHPStan\Type\Nette\Data\ComponentModel; | ||
|
||
use Nette\Application\UI\Form; | ||
use Nette\Forms\Container; | ||
use function PHPStan\Testing\assertType; | ||
|
||
class SomeForm extends Form { | ||
} | ||
|
||
$bool = rand(0, 1) ? true : false; | ||
|
||
$someForm = new SomeForm(); | ||
|
||
assertType('array<int|string, Nette\ComponentModel\IComponent>', $someForm->getComponents(false)); | ||
assertType('array<int|string, Nette\Forms\Container>', $someForm->getComponents(false, Container::class)); | ||
assertType('Iterator<int|string, Nette\ComponentModel\IComponent>', $someForm->getComponents(true)); | ||
assertType('Iterator<int|string, Nette\Forms\Container>', $someForm->getComponents(true, Container::class)); | ||
assertType('array<int|string, Nette\ComponentModel\IComponent>|Iterator<int|string, Nette\ComponentModel\IComponent>', $someControl->getComponents($bool)); |