Skip to content

Commit 4defe78

Browse files
committed
Added Nette\Caching\Cache dynamic return type extensions
1 parent fe39d82 commit 4defe78

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

extension.neon

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ services:
4343
- phpstan.broker.propertiesClassReflectionExtension
4444
- phpstan.broker.methodsClassReflectionExtension
4545

46+
-
47+
class: PHPStan\Type\Nette\CachingFallbacksDynamicReturnTypeExtension
48+
tags:
49+
- phpstan.broker.dynamicMethodReturnTypeExtension
50+
51+
-
52+
class: PHPStan\Type\Nette\CachingSaveDynamicReturnTypeExtension
53+
tags:
54+
- phpstan.broker.dynamicMethodReturnTypeExtension
55+
4656
-
4757
class: PHPStan\Type\Nette\ComponentModelArrayAccessDynamicReturnTypeExtension
4858
tags:
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+
namespace PHPStan\Type\Nette;
4+
5+
use PhpParser\Node\Expr\MethodCall;
6+
use PHPStan\Analyser\Scope;
7+
use PHPStan\Reflection\MethodReflection;
8+
use PHPStan\Reflection\ParametersAcceptorSelector;
9+
use PHPStan\Type\DynamicMethodReturnTypeExtension;
10+
use PHPStan\Type\Type;
11+
12+
final class CachingFallbacksDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
13+
{
14+
15+
/** @var array<string, int> */
16+
private $fallbackMethods = [
17+
'load' => 1,
18+
'call' => 0,
19+
'wrap' => 0,
20+
];
21+
22+
public function getClass(): string
23+
{
24+
return 'Nette\Caching\Cache';
25+
}
26+
27+
public function isMethodSupported(MethodReflection $methodReflection): bool
28+
{
29+
return array_key_exists($methodReflection->getName(), $this->fallbackMethods);
30+
}
31+
32+
public function getTypeFromMethodCall(
33+
MethodReflection $methodReflection,
34+
MethodCall $methodCall,
35+
Scope $scope
36+
): Type
37+
{
38+
$fallbackParameterIndex = $this->fallbackMethods[$methodReflection->getName()];
39+
40+
if ($fallbackParameterIndex >= count($methodCall->args)) {
41+
return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
42+
}
43+
44+
$fallbackParameterType = $scope->getType($methodCall->args[$fallbackParameterIndex]->value);
45+
if (!$fallbackParameterType->isCallable()->yes()) {
46+
return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
47+
}
48+
49+
return ParametersAcceptorSelector::selectFromArgs($scope, $methodCall->args, $fallbackParameterType->getCallableParametersAcceptors($scope))->getReturnType();
50+
}
51+
52+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Type\Nette;
4+
5+
use PhpParser\Node\Expr\MethodCall;
6+
use PHPStan\Analyser\Scope;
7+
use PHPStan\Reflection\MethodReflection;
8+
use PHPStan\Reflection\ParametersAcceptorSelector;
9+
use PHPStan\Type\DynamicMethodReturnTypeExtension;
10+
use PHPStan\Type\Type;
11+
12+
final class CachingSaveDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
13+
{
14+
15+
public function getClass(): string
16+
{
17+
return 'Nette\Caching\Cache';
18+
}
19+
20+
public function isMethodSupported(MethodReflection $methodReflection): bool
21+
{
22+
return $methodReflection->getName() === 'save';
23+
}
24+
25+
public function getTypeFromMethodCall(
26+
MethodReflection $methodReflection,
27+
MethodCall $methodCall,
28+
Scope $scope
29+
): Type
30+
{
31+
if (count($methodCall->args) < 2) {
32+
return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
33+
}
34+
35+
return $scope->getType($methodCall->args[1]->value);
36+
}
37+
38+
}

0 commit comments

Comments
 (0)