Skip to content

Commit

Permalink
Update ComponentModel\Container stub for component-model 3.1.0
Browse files Browse the repository at this point in the history
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
jtojnar committed May 18, 2024
1 parent 8af9474 commit 11e9969
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
6 changes: 5 additions & 1 deletion extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ parameters:
- stubs/Application/UI/Component.stub
- stubs/Application/UI/Multiplier.stub
- stubs/ComponentModel/Component.stub
- stubs/ComponentModel/Container.stub
- stubs/ComponentModel/IComponent.stub
- stubs/ComponentModel/IContainer.stub
- stubs/Database/ResultSet.stub
Expand Down Expand Up @@ -110,3 +109,8 @@ services:
class: PHPStan\Rule\Nette\PresenterInjectedPropertiesExtension
tags:
- phpstan.properties.readWriteExtension

-
class: PHPStan\Stubs\Nette\StubFilesExtensionLoader
tags:
- phpstan.stubFilesExtension
47 changes: 47 additions & 0 deletions src/Stubs/Nette/StubFilesExtensionLoader.php
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;
}

}
21 changes: 21 additions & 0 deletions stubs/ComponentModel/Container_3_1.stub
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 true
* ? ($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): \Iterator
{
// nothing
}
}

0 comments on commit 11e9969

Please sign in to comment.