Skip to content

Commit 492fbf8

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

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed

extension.neon

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

0 commit comments

Comments
 (0)