Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update ComponentModel\Container stub for component-model 3.1.0 #142

Draft
wants to merge 1 commit into
base: 1.1.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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
}
}