Skip to content

Commit fe60978

Browse files
committed
Add NetteDIModuleType for PHPStan
1 parent 3ab38a5 commit fe60978

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

phpstan.neon

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,9 @@ parameters:
99
ignoreErrors:
1010
- '#^Calling method getContent\(\) on possibly null value of type Symfony\\Component\\BrowserKit\\Response\|null#'
1111
- '#^Method Arachne\\Codeception\\Module\\NetteDIModule::getContainer\(\) should return Nette\\DI\\Container but returns Nette\\DI\\Container\|null#'
12+
13+
services:
14+
-
15+
class: Arachne\Codeception\PHPStan\NetteDIModuleType
16+
tags:
17+
- phpstan.broker.dynamicMethodReturnTypeExtension

src/PHPStan/NetteDIModuleType.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Arachne\Codeception\PHPStan;
6+
7+
use Arachne\Codeception\Module\NetteDIModule;
8+
use PhpParser\Node\Expr\ClassConstFetch;
9+
use PhpParser\Node\Expr\MethodCall;
10+
use PhpParser\Node\Name;
11+
use PHPStan\Analyser\Scope;
12+
use PHPStan\Reflection\MethodReflection;
13+
use PHPStan\Type\DynamicMethodReturnTypeExtension;
14+
use PHPStan\Type\ObjectType;
15+
use PHPStan\Type\Type;
16+
17+
class NetteDIModuleType implements DynamicMethodReturnTypeExtension
18+
{
19+
public function getClass(): string
20+
{
21+
return NetteDIModule::class;
22+
}
23+
24+
public function isMethodSupported(MethodReflection $methodReflection): bool
25+
{
26+
return $methodReflection->getName() === 'grabService';
27+
}
28+
29+
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
30+
{
31+
if (!$methodCall->args) {
32+
return $methodReflection->getReturnType();
33+
}
34+
35+
$arg = $methodCall->args[0]->value;
36+
37+
if (!$arg instanceof ClassConstFetch) {
38+
return $methodReflection->getReturnType();
39+
}
40+
41+
$class = $arg->class;
42+
43+
if (!$class instanceof Name) {
44+
return $methodReflection->getReturnType();
45+
}
46+
47+
$class = (string) $class;
48+
49+
if ($class === 'static') {
50+
return $methodReflection->getReturnType();
51+
}
52+
53+
if ($class === 'self') {
54+
$class = $scope->getClassReflection()->getName();
55+
}
56+
57+
return new ObjectType($class);
58+
}
59+
}

0 commit comments

Comments
 (0)