Skip to content

Commit e7d0fda

Browse files
committed
uses nette/phpstan-rules
1 parent 8ab2202 commit e7d0fda

3 files changed

Lines changed: 135 additions & 1 deletion

File tree

composer.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
"nette/tester": "^2.6",
2323
"nikic/php-parser": "^5.0",
2424
"tracy/tracy": "^2.8",
25-
"phpstan/phpstan-nette": "^2.0@stable",
25+
"phpstan/phpstan": "^2.1.40@stable",
26+
"phpstan/extension-installer": "^1.4@stable",
27+
"nette/phpstan-rules": "^1.0",
2628
"jetbrains/phpstorm-attributes": "^1.2"
2729
},
2830
"suggest": {
@@ -43,5 +45,10 @@
4345
"branch-alias": {
4446
"dev-master": "4.2-dev"
4547
}
48+
},
49+
"config": {
50+
"allow-plugins": {
51+
"phpstan/extension-installer": true
52+
}
4653
}
4754
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<?php declare(strict_types=1);
2+
3+
/**
4+
* PHPStan type tests.
5+
*/
6+
7+
use Nette\PhpGenerator\ClassType;
8+
use Nette\PhpGenerator\Closure;
9+
use Nette\PhpGenerator\Constant;
10+
use Nette\PhpGenerator\EnumCase;
11+
use Nette\PhpGenerator\EnumType;
12+
use Nette\PhpGenerator\GlobalFunction;
13+
use Nette\PhpGenerator\InterfaceType;
14+
use Nette\PhpGenerator\Method;
15+
use Nette\PhpGenerator\Parameter;
16+
use Nette\PhpGenerator\PhpFile;
17+
use Nette\PhpGenerator\PhpNamespace;
18+
use Nette\PhpGenerator\Property;
19+
use Nette\PhpGenerator\PropertyHook;
20+
use Nette\PhpGenerator\TraitType;
21+
use Nette\PhpGenerator\TraitUse;
22+
use Nette\Utils\Type;
23+
use function PHPStan\Testing\assertType;
24+
25+
26+
function testParameterGetType(Parameter $param): void
27+
{
28+
assertType('string|null', $param->getType());
29+
assertType('string|null', $param->getType(false));
30+
assertType(Type::class . '|null', $param->getType(true));
31+
}
32+
33+
34+
function testPropertyGetType(Property $prop): void
35+
{
36+
assertType('string|null', $prop->getType());
37+
assertType('string|null', $prop->getType(false));
38+
assertType(Type::class . '|null', $prop->getType(true));
39+
}
40+
41+
42+
function testMethodGetReturnType(Method $method): void
43+
{
44+
assertType('string|null', $method->getReturnType());
45+
assertType('string|null', $method->getReturnType(false));
46+
assertType(Type::class . '|null', $method->getReturnType(true));
47+
}
48+
49+
50+
function testEnumGetCases(EnumType $enum): void
51+
{
52+
assertType('array<string, ' . EnumCase::class . '>', $enum->getCases());
53+
}
54+
55+
56+
function testClosureGetUses(Closure $closure): void
57+
{
58+
assertType('list<' . Parameter::class . '>', $closure->getUses());
59+
}
60+
61+
62+
function testPropertyHookGetParameters(PropertyHook $hook): void
63+
{
64+
assertType('array<string, ' . Parameter::class . '>', $hook->getParameters());
65+
}
66+
67+
68+
function testPropertyGetHooks(Property $prop): void
69+
{
70+
assertType('array<string, ' . PropertyHook::class . '>', $prop->getHooks());
71+
}
72+
73+
74+
function testMethodGetVisibility(Method $method): void
75+
{
76+
assertType("'private'|'protected'|'public'|null", $method->getVisibility());
77+
}
78+
79+
80+
function testPropertyGetVisibility(Property $prop): void
81+
{
82+
assertType("'private'|'protected'|'public'|null", $prop->getVisibility());
83+
}
84+
85+
86+
function testClassTypeCollections(ClassType $class): void
87+
{
88+
assertType('array<string, ' . Method::class . '>', $class->getMethods());
89+
assertType('array<string, ' . Property::class . '>', $class->getProperties());
90+
assertType('array<string, ' . Constant::class . '>', $class->getConstants());
91+
assertType('array<string, ' . TraitUse::class . '>', $class->getTraits());
92+
}
93+
94+
95+
function testPhpFileCollections(PhpFile $file): void
96+
{
97+
assertType('array<string, ' . PhpNamespace::class . '>', $file->getNamespaces());
98+
assertType('array<string, ' . ClassType::class . '|' . EnumType::class . '|' . InterfaceType::class . '|' . TraitType::class . '>', $file->getClasses());
99+
assertType('array<string, ' . GlobalFunction::class . '>', $file->getFunctions());
100+
}
101+
102+
103+
function testPhpNamespaceCollections(PhpNamespace $ns): void
104+
{
105+
assertType('array<string, ' . ClassType::class . '|' . EnumType::class . '|' . InterfaceType::class . '|' . TraitType::class . '>', $ns->getClasses());
106+
assertType('array<string, ' . GlobalFunction::class . '>', $ns->getFunctions());
107+
assertType('array<string, string>', $ns->getUses());
108+
}
109+
110+
111+
function testTraitUseGetResolutions(TraitUse $trait): void
112+
{
113+
assertType('list<string>', $trait->getResolutions());
114+
}
115+
116+
117+
function testGlobalFunctionGetParameters(GlobalFunction $fn): void
118+
{
119+
assertType('array<string, ' . Parameter::class . '>', $fn->getParameters());
120+
}
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__ . '/php-generator-types.php');

0 commit comments

Comments
 (0)