Skip to content

Commit 303f5b8

Browse files
committed
uses nette/phpstan-rules
1 parent 7cbe717 commit 303f5b8

5 files changed

Lines changed: 69 additions & 8 deletions

File tree

composer.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
"nette/tester": "^2.6",
2727
"latte/latte": "^2.10.2 || ^3.0.12",
2828
"tracy/tracy": "^2.9",
29-
"phpstan/phpstan-nette": "^2.0@stable"
29+
"phpstan/phpstan": "^2.1@stable",
30+
"phpstan/extension-installer": "^1.4@stable",
31+
"nette/phpstan-rules": "^1.0"
3032
},
3133
"conflict": {
3234
"latte/latte": ">=3.0.0 <3.0.12 || >=3.2"
@@ -49,5 +51,10 @@
4951
"branch-alias": {
5052
"dev-master": "3.2-dev"
5153
}
54+
},
55+
"config": {
56+
"allow-plugins": {
57+
"phpstan/extension-installer": true
58+
}
5259
}
5360
}

phpstan.neon

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,3 @@ parameters:
33

44
paths:
55
- src
6-
7-
treatPhpDocTypesAsCertain: false
8-
9-
10-
includes:
11-
- vendor/phpstan/phpstan-nette/extension.neon

src/Bridges/FormsDI/FormsExtension.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
namespace Nette\Bridges\FormsDI;
99

1010
use Nette;
11-
use function defined;
11+
use function defined, is_object;
1212

1313

1414
/**
@@ -31,6 +31,7 @@ public function afterCompile(Nette\PhpGenerator\ClassType $class): void
3131
{
3232
$initialize = $this->initialization ?? $class->getMethod('initialize');
3333

34+
assert(is_object($this->config));
3435
foreach ($this->config->messages as $name => $text) {
3536
if (defined('Nette\Forms\Form::' . $name)) {
3637
$initialize->addBody('Nette\Forms\Validator::$messages[Nette\Forms\Form::?] = ?;', [$name, $text]);

tests/types/TypesTest.phpt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php declare(strict_types=1);
2+
3+
require __DIR__ . '/../bootstrap.php';
4+
5+
use Nette\PHPStan\Tester\TypeAssert;
6+
7+
TypeAssert::assertTypes(__DIR__ . '/forms-types.php');

tests/types/forms-types.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php declare(strict_types=1);
2+
3+
/**
4+
* PHPStan type tests for Forms.
5+
* Run: vendor/bin/phpstan analyse tests/types
6+
*/
7+
8+
use Nette\Forms\Container;
9+
use Nette\Forms\Form;
10+
use Nette\Utils\ArrayHash;
11+
use function PHPStan\Testing\assertType;
12+
13+
14+
class FormDto
15+
{
16+
public string $name;
17+
}
18+
19+
20+
function testFormContainerGetValues(Container $container): void
21+
{
22+
assertType('Nette\Utils\ArrayHash<mixed>', $container->getValues());
23+
assertType('array|array<mixed>', $container->getValues('array'));
24+
assertType(FormDto::class, $container->getValues(FormDto::class));
25+
}
26+
27+
28+
function testFormContainerGetUntrustedValues(Container $container): void
29+
{
30+
assertType('Nette\Utils\ArrayHash<mixed>', $container->getUntrustedValues(null));
31+
assertType('array|array<mixed>', $container->getUntrustedValues('array'));
32+
assertType(FormDto::class, $container->getUntrustedValues(FormDto::class));
33+
}
34+
35+
36+
function testFormContainerArrayAccess(Container $container): void
37+
{
38+
assertType('Nette\Forms\Controls\BaseControl', $container['name']);
39+
}
40+
41+
42+
function testFormEvents(Form $form): void
43+
{
44+
$form->onSuccess[] = function (Form $form, ArrayHash $values): void {
45+
assertType(Form::class, $form);
46+
assertType(ArrayHash::class, $values);
47+
};
48+
49+
$form->onError[] = function (Form $form): void {
50+
assertType(Form::class, $form);
51+
};
52+
}

0 commit comments

Comments
 (0)