Skip to content

Commit a45d709

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

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-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 PhpParser\Node\Expr\MethodCall;
6+
use PHPStan\Analyser\Scope;
7+
use PHPStan\Reflection\MethodReflection;
8+
use PHPStan\Reflection\ParametersAcceptor;
9+
use PHPStan\Reflection\ParametersAcceptorSelector;
10+
use PHPStan\Type\DynamicMethodReturnTypeExtension;
11+
use PHPStan\Type\Type;
12+
13+
final class CachingFallbacksDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
14+
{
15+
16+
/** @var array<string, int> */
17+
private $fallbackMethods = [
18+
'load' => 1,
19+
'call' => 0,
20+
'wrap' => 0,
21+
];
22+
23+
public function getClass(): string
24+
{
25+
return 'Nette\Caching\Cache';
26+
}
27+
28+
public function isMethodSupported(MethodReflection $methodReflection): bool
29+
{
30+
$methodName = $methodReflection->getName();
31+
32+
return array_key_exists($methodName, $this->fallbackMethods);
33+
}
34+
35+
public function getTypeFromMethodCall(
36+
MethodReflection $methodReflection,
37+
MethodCall $methodCall,
38+
Scope $scope
39+
): Type
40+
{
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->isCallable()->yes()) {
49+
return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
50+
}
51+
52+
return ParametersAcceptorSelector::selectFromArgs($scope, $methodCall->args, $fallbackParameterType->getCallableParametersAcceptors($scope))->getReturnType();
53+
}
54+
55+
}
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)