Skip to content
Closed
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 @@ -7,7 +7,6 @@ parameters:
stubFiles:
- stubs/Application/Routers/RouteList.stub
- stubs/Application/UI/Component.stub
- stubs/Application/UI/Multiplier.stub
- stubs/Application/UI/Presenter.stub
- stubs/Caching/Cache.stub
- stubs/ComponentModel/Component.stub
Expand Down Expand Up @@ -52,6 +51,11 @@ parameters:
- forward

services:
-
class: PHPStan\Stubs\Nette\Application\StubFilesExtensionLoader
tags:
- phpstan.stubFilesExtension

-
class: PHPStan\Reflection\Nette\HtmlClassReflectionExtension
tags:
Expand Down
38 changes: 38 additions & 0 deletions src/Stubs/Nette/Application/StubFilesExtensionLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php declare(strict_types = 1);

namespace PHPStan\Stubs\Nette\Application;

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
{
$path = dirname(dirname(dirname(dirname(__DIR__)))) . '/stubs';

try {
$applicationVersion = class_exists(InstalledVersions::class)
? InstalledVersions::getVersion('nette/application')
: null;
} catch (OutOfBoundsException $e) {
$applicationVersion = null;
}

$files = [];

if ($applicationVersion !== null && version_compare($applicationVersion, '3.2.5', '>=')) {
$files[] = $path . '/Application/UI/Multiplier1.stub';
} else {
$files[] = $path . '/Application/UI/Multiplier.stub';
}

return $files;
}

}
19 changes: 19 additions & 0 deletions stubs/Application/UI/Multiplier1.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Nette\Application\UI;

/**
* @template T of \Nette\ComponentModel\IComponent
*/
final class Multiplier extends Component
{
/**
* @param callable(string, self<T>) : (T|null) $factory
*/
public function __construct(callable $factory);

/**
* @return T|null
*/
protected function createComponent(string $name): ?\Nette\ComponentModel\IComponent;
}
18 changes: 17 additions & 1 deletion tests/Type/Nette/MultiplierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,30 @@

namespace PHPStan\Type\Nette;

use Composer\InstalledVersions;
use OutOfBoundsException;
use PHPStan\Testing\TypeInferenceTestCase;
use function class_exists;
use function version_compare;

class MultiplierTest extends TypeInferenceTestCase
{

public function dataFileAsserts(): iterable
{
yield from self::gatherAssertTypes(__DIR__ . '/data/multiplier.php');
try {
$applicationVersion = class_exists(InstalledVersions::class)
? InstalledVersions::getVersion('nette/application')
: null;
} catch (OutOfBoundsException $e) {
$applicationVersion = null;
}

if ($applicationVersion !== null && version_compare($applicationVersion, '3.2.5', '>=')) {
yield from self::gatherAssertTypes(__DIR__ . '/data/multiplierApplication325.php');
} else {
yield from self::gatherAssertTypes(__DIR__ . '/data/multiplier.php');
}
}

/**
Expand Down
25 changes: 25 additions & 0 deletions tests/Type/Nette/data/multiplierApplication325.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php declare(strict_types = 1);

use Nette\Application\UI\Form;
use Nette\Application\UI\Multiplier;

use function PHPStan\Testing\assertType;

/** @var Multiplier<Form> $multiplier */
$multiplier = new Multiplier(function (string $name): ?Form {
if($name === 'foo') {
return new Form();
} else {
return null;
}
});

assertType('Nette\Application\UI\Multiplier<Nette\Application\UI\Form>', $multiplier);

$form = $multiplier->createComponent('foo');

assertType(Form::class . '|null', $form);

$notSupportedForm = $multiplier->createComponent('notSupported');

assertType(Form::class . '|null', $notSupportedForm);