Skip to content

Commit

Permalink
Use generics for BaseControl and Control
Browse files Browse the repository at this point in the history
  • Loading branch information
xificurk committed Dec 27, 2023
1 parent e5bc24b commit f90f5eb
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 15 deletions.
8 changes: 8 additions & 0 deletions src/FormRenderer/Filters/ValidationClassFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ public function __construct(?string $invalidClass, ?string $validClass)
$this->validClass = $validClass;
}

/**
* @template T
* @param Nette\Forms\Control<T> $control
*/
public function __invoke(Nette\Forms\Control $control): ?string
{
if (count($control->getErrors()) > 0) {
Expand All @@ -33,6 +37,10 @@ public function __invoke(Nette\Forms\Control $control): ?string
return null;
}

/**
* @template T
* @param Nette\Forms\Control<T> $control
*/
private function isFilled(Nette\Forms\Control $control): bool
{
if ($control instanceof Nette\Forms\Controls\TextInput && $control->getControlPrototype()->type === 'password') {
Expand Down
10 changes: 5 additions & 5 deletions tests/FormRenderer/Bootstrap3RendererTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class Bootstrap3RendererTest extends TestCase
$form = $this->createTestForm();
$form->addError('Form error 1.');
$form->addError('Form error 2.');
/** @var Nette\Forms\Controls\BaseControl $control */
/** @var Nette\Forms\Controls\BaseControl<mixed> $control */
foreach ($form->getControls() as $control) {
if ($control instanceof Nette\Forms\Controls\Button) {
continue;
Expand All @@ -106,7 +106,7 @@ class Bootstrap3RendererTest extends TestCase
public function testRequiredControl(string $mode): void
{
$form = $this->createTestForm();
/** @var Nette\Forms\Controls\BaseControl $control */
/** @var Nette\Forms\Controls\BaseControl<mixed> $control */
foreach ($form->getControls() as $control) {
$control->setRequired('REQUIRED');
}
Expand All @@ -123,7 +123,7 @@ class Bootstrap3RendererTest extends TestCase
public function testControlDescription(string $mode): void
{
$form = $this->createTestForm();
/** @var Nette\Forms\Controls\BaseControl $control */
/** @var Nette\Forms\Controls\BaseControl<mixed> $control */
foreach ($form->getControls() as $control) {
$control->setOption('description', "Control {$control->getName()} description.");
}
Expand All @@ -140,7 +140,7 @@ class Bootstrap3RendererTest extends TestCase
public function testCustomControlId(string $mode): void
{
$form = $this->createTestForm();
/** @var Nette\Forms\Controls\BaseControl $control */
/** @var Nette\Forms\Controls\BaseControl<mixed> $control */
foreach ($form->getControls() as $control) {
$control->setOption('id', sprintf('custom-%s', $control->lookupPath()));
}
Expand All @@ -157,7 +157,7 @@ class Bootstrap3RendererTest extends TestCase
public function testCustomControlClass(string $mode): void
{
$form = $this->createTestForm();
/** @var Nette\Forms\Controls\BaseControl $control */
/** @var Nette\Forms\Controls\BaseControl<mixed> $control */
foreach ($form->getControls() as $control) {
$control->setOption('class', sprintf('custom-%s', $control->lookupPath()));
}
Expand Down
10 changes: 5 additions & 5 deletions tests/FormRenderer/Bootstrap4RendererTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class Bootstrap4RendererTest extends TestCase
$form = $this->createTestForm();
$form->addError('Form error 1.');
$form->addError('Form error 2.');
/** @var BaseControl $control */
/** @var BaseControl<mixed> $control */
foreach ($form->getControls() as $control) {
if ($control instanceof Nette\Forms\Controls\Button) {
continue;
Expand All @@ -126,7 +126,7 @@ class Bootstrap4RendererTest extends TestCase
public function testRequiredControl(string $mode): void
{
$form = $this->createTestForm();
/** @var BaseControl $control */
/** @var BaseControl<mixed> $control */
foreach ($form->getControls() as $control) {
$control->setRequired('REQUIRED');
}
Expand All @@ -143,7 +143,7 @@ class Bootstrap4RendererTest extends TestCase
public function testControlDescription(string $mode): void
{
$form = $this->createTestForm();
/** @var BaseControl $control */
/** @var BaseControl<mixed> $control */
foreach ($form->getControls() as $control) {
$control->setOption('description', "Control {$control->getName()} description.");
}
Expand All @@ -160,7 +160,7 @@ class Bootstrap4RendererTest extends TestCase
public function testCustomControlId(string $mode): void
{
$form = $this->createTestForm();
/** @var BaseControl $control */
/** @var BaseControl<mixed> $control */
foreach ($form->getControls() as $control) {
$control->setOption('id', sprintf('custom-%s', $control->lookupPath()));
}
Expand All @@ -177,7 +177,7 @@ class Bootstrap4RendererTest extends TestCase
public function testCustomControlClass(string $mode): void
{
$form = $this->createTestForm();
/** @var BaseControl $control */
/** @var BaseControl<mixed> $control */
foreach ($form->getControls() as $control) {
$control->setOption('class', sprintf('custom-%s', $control->lookupPath()));
}
Expand Down
3 changes: 3 additions & 0 deletions tests/FormRenderer/Filters/Fixtures/CustomControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

use Nette;

/**
* @implements Nette\Forms\Control<mixed>
*/
class CustomControl implements Nette\Forms\Control
{

Expand Down
2 changes: 2 additions & 0 deletions tests/FormRenderer/Filters/ValidationClassFilterTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class ValidationClassFilterTest extends TestCase

/**
* @dataProvider getFilterData
* @template T
* @param Control<T> $control
*/
public function testFilter(string $description, ?string $invalidClass, ?string $validClass, Control $control, ?string $expectedClass): void
{
Expand Down
4 changes: 4 additions & 0 deletions tests/FormRenderer/Fixtures/FooControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
namespace NepadaTests\FormRenderer\Fixtures;

use Nette;
use Nette\Forms\Controls\BaseControl;
use Nette\Utils\Html;

/**
* @extends BaseControl<mixed>
*/
class FooControl extends Nette\Forms\Controls\BaseControl
{

Expand Down
10 changes: 5 additions & 5 deletions tests/FormRenderer/TemplateRendererTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class TemplateRendererTest extends TestCase
$form = $this->testFormFactory->create();
$form->addError('Form error 1.');
$form->addError('Form error 2.');
/** @var Nette\Forms\Controls\BaseControl $control */
/** @var Nette\Forms\Controls\BaseControl<mixed> $control */
foreach ($form->getControls() as $control) {
if ($control instanceof Nette\Forms\Controls\Button) {
continue;
Expand All @@ -80,7 +80,7 @@ class TemplateRendererTest extends TestCase
public function testRequiredControl(): void
{
$form = $this->testFormFactory->create();
/** @var Nette\Forms\Controls\BaseControl $control */
/** @var Nette\Forms\Controls\BaseControl<mixed> $control */
foreach ($form->getControls() as $control) {
$control->setRequired('REQUIRED');
}
Expand All @@ -94,7 +94,7 @@ class TemplateRendererTest extends TestCase
public function testControlDescription(): void
{
$form = $this->testFormFactory->create();
/** @var Nette\Forms\Controls\BaseControl $control */
/** @var Nette\Forms\Controls\BaseControl<mixed> $control */
foreach ($form->getControls() as $control) {
$control->setOption('description', "Control {$control->getName()} description.");
}
Expand All @@ -108,7 +108,7 @@ class TemplateRendererTest extends TestCase
public function testCustomControlId(): void
{
$form = $this->testFormFactory->create();
/** @var Nette\Forms\Controls\BaseControl $control */
/** @var Nette\Forms\Controls\BaseControl<mixed> $control */
foreach ($form->getControls() as $control) {
$control->setOption('id', sprintf('custom-%s', $control->lookupPath()));
}
Expand All @@ -122,7 +122,7 @@ class TemplateRendererTest extends TestCase
public function testCustomControlClass(): void
{
$form = $this->testFormFactory->create();
/** @var Nette\Forms\Controls\BaseControl $control */
/** @var Nette\Forms\Controls\BaseControl<mixed> $control */
foreach ($form->getControls() as $control) {
$control->setOption('class', sprintf('custom-%s', $control->lookupPath()));
}
Expand Down
8 changes: 8 additions & 0 deletions tests/PHPStan/conditional.config.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
declare(strict_types = 1);

use Composer\InstalledVersions;
use Composer\Semver\VersionParser;
use Nette\Localization\Translator;

$config = [];
Expand All @@ -15,4 +17,10 @@
];
}

if (InstalledVersions::satisfies(new VersionParser(), 'nette/forms', '<3.1.15')) {
$config['parameters']['ignoreErrors'][] = [
'message' => '~contains generic type Nette\\\\Forms\\\\Control<mixed> but interface Nette\\\\Forms\\\\Control is not generic~',
];
}

return $config;
9 changes: 9 additions & 0 deletions tests/PHPStan/conditional.config.tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,13 @@
];
}

if (InstalledVersions::satisfies(new VersionParser(), 'nette/forms', '<3.1.15')) {
$config['parameters']['ignoreErrors'][] = [
'message' => '~contains generic type Nette\\\\Forms\\\\Controls\\\\BaseControl<mixed> but class Nette\\\\Forms\\\\Controls\\\\BaseControl is not generic~',
];
$config['parameters']['ignoreErrors'][] = [
'message' => '~contains generic type Nette\\\\Forms\\\\Control<mixed> but interface Nette\\\\Forms\\\\Control is not generic~',
];
}

return $config;

0 comments on commit f90f5eb

Please sign in to comment.