Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ services:
- phpstan.broker.propertiesClassReflectionExtension
- phpstan.broker.methodsClassReflectionExtension

-
class: PHPStan\Type\Nette\CachingFallbacksDynamicReturnTypeExtension
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension

-
class: PHPStan\Type\Nette\CachingSaveDynamicReturnTypeExtension
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension

-
class: PHPStan\Type\Nette\ComponentModelArrayAccessDynamicReturnTypeExtension
tags:
Expand Down
52 changes: 52 additions & 0 deletions src/Type/Nette/CachingFallbacksDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Nette;

use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\Type;

final class CachingFallbacksDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
{

/** @var array<string, int> */
private $fallbackMethods = [
'load' => 1,
'call' => 0,
'wrap' => 0,
];

public function getClass(): string
{
return 'Nette\Caching\Cache';
}

public function isMethodSupported(MethodReflection $methodReflection): bool
{
return array_key_exists($methodReflection->getName(), $this->fallbackMethods);
}

public function getTypeFromMethodCall(
MethodReflection $methodReflection,
MethodCall $methodCall,
Scope $scope
): Type
{
$fallbackParameterIndex = $this->fallbackMethods[$methodReflection->getName()];

if ($fallbackParameterIndex >= count($methodCall->args)) {
return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
}

$fallbackParameterType = $scope->getType($methodCall->args[$fallbackParameterIndex]->value);
if (!$fallbackParameterType->isCallable()->yes()) {
return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
}

return ParametersAcceptorSelector::selectFromArgs($scope, $methodCall->args, $fallbackParameterType->getCallableParametersAcceptors($scope))->getReturnType();
}

}
38 changes: 38 additions & 0 deletions src/Type/Nette/CachingSaveDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Nette;

use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\Type;

final class CachingSaveDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
{

public function getClass(): string
{
return 'Nette\Caching\Cache';
}

public function isMethodSupported(MethodReflection $methodReflection): bool
{
return $methodReflection->getName() === 'save';
}

public function getTypeFromMethodCall(
MethodReflection $methodReflection,
MethodCall $methodCall,
Scope $scope
): Type
{
if (count($methodCall->args) < 2) {
return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
}

return $scope->getType($methodCall->args[1]->value);
}

}