Skip to content

Commit

Permalink
Fix build
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jan 31, 2023
1 parent b1e6ae1 commit 4db4f3c
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 41 deletions.
7 changes: 2 additions & 5 deletions src/Rule/Nette/RegularExpressionPatternRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\Constant\ConstantStringType;
use function in_array;
use function sprintf;
use function strtolower;
Expand Down Expand Up @@ -84,11 +83,9 @@ private function extractPatterns(StaticCall $staticCall, Scope $scope): array
}

foreach ($constantArrayType->getKeyTypes() as $arrayKeyType) {
if (!$arrayKeyType instanceof ConstantStringType) {
continue;
foreach ($arrayKeyType->getConstantStrings() as $constantString) {
$patternStrings[] = $constantString->getValue();
}

$patternStrings[] = $arrayKeyType->getValue();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use function count;
use function sprintf;
use function ucfirst;
Expand Down Expand Up @@ -43,20 +43,25 @@ public function getTypeFromMethodCall(
}

$argType = $scope->getType($args[0]->value);
if (!$argType instanceof ConstantStringType) {
if (count($argType->getConstantStrings()) === 0) {
return $mixedType;
}

$componentName = $argType->getValue();
$types = [];
foreach ($argType->getConstantStrings() as $constantString) {
$componentName = $constantString->getValue();

$methodName = sprintf('createComponent%s', ucfirst($componentName));
if (!$calledOnType->hasMethod($methodName)->yes()) {
return $mixedType;
}
$methodName = sprintf('createComponent%s', ucfirst($componentName));
if (!$calledOnType->hasMethod($methodName)->yes()) {
return $mixedType;
}

$method = $calledOnType->getMethod($methodName, $scope);
$method = $calledOnType->getMethod($methodName, $scope);

$types[] = ParametersAcceptorSelector::selectSingle($method->getVariants())->getReturnType();
}

return ParametersAcceptorSelector::selectSingle($method->getVariants())->getReturnType();
return TypeCombinator::union(...$types);
}

}
23 changes: 14 additions & 9 deletions src/Type/Nette/ComponentModelDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use function count;
use function sprintf;
use function ucfirst;
Expand Down Expand Up @@ -43,20 +43,25 @@ public function getTypeFromMethodCall(
}

$argType = $scope->getType($args[0]->value);
if (!$argType instanceof ConstantStringType) {
if (count($argType->getConstantStrings()) === 0) {
return $mixedType;
}

$componentName = $argType->getValue();
$types = [];
foreach ($argType->getConstantStrings() as $constantString) {
$componentName = $constantString->getValue();

$methodName = sprintf('createComponent%s', ucfirst($componentName));
if (!$calledOnType->hasMethod($methodName)->yes()) {
return $mixedType;
}
$methodName = sprintf('createComponent%s', ucfirst($componentName));
if (!$calledOnType->hasMethod($methodName)->yes()) {
return $mixedType;
}

$method = $calledOnType->getMethod($methodName, $scope);
$method = $calledOnType->getMethod($methodName, $scope);

$types[] = ParametersAcceptorSelector::selectSingle($method->getVariants())->getReturnType();
}

return ParametersAcceptorSelector::selectSingle($method->getVariants())->getReturnType();
return TypeCombinator::union(...$types);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\ArrayType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\MixedType;
use PHPStan\Type\NullType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
Expand All @@ -37,11 +35,11 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method

$arg = $methodCall->getArgs()[0]->value;
$scopedType = $scope->getType($arg);
if ($scopedType instanceof NullType) {
if ($scopedType->isNull()->yes()) {
return new ObjectType('Nette\Utils\ArrayHash');
}

if ($scopedType instanceof ConstantStringType && $scopedType->getValue() === 'array') {
if (count($scopedType->getConstantStrings()) === 1 && $scopedType->getConstantStrings()[0]->getValue() === 'array') {
return new ArrayType(new StringType(), new MixedType());
}

Expand Down
3 changes: 1 addition & 2 deletions src/Type/Nette/PresenterGetSessionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\NullType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use function count;
Expand All @@ -26,7 +25,7 @@ public function isMethodSupported(MethodReflection $methodReflection): bool

public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
{
if (count($methodCall->getArgs()) === 0 || $scope->getType($methodCall->getArgs()[0]->value) instanceof NullType) {
if (count($methodCall->getArgs()) === 0 || $scope->getType($methodCall->getArgs()[0]->value)->isNull()->yes()) {
return new ObjectType('Nette\Http\Session');
}

Expand Down
28 changes: 16 additions & 12 deletions src/Type/Nette/ServiceLocatorDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
Expand Down Expand Up @@ -46,25 +45,30 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
return $mixedType;
}
$argType = $scope->getType($methodCall->getArgs()[0]->value);
if (!$argType instanceof ConstantStringType) {
if (count($argType->getConstantStrings()) === 0) {
return $mixedType;
}

$type = new ObjectType($argType->getValue());
if (
$methodReflection->getName() === 'getByType'
&& count($methodCall->getArgs()) >= 2
) {
$throwType = $scope->getType($methodCall->getArgs()[1]->value);
$types = [];
foreach ($argType->getConstantStrings() as $constantString) {
$type = new ObjectType($constantString->getValue());
if (
!$throwType instanceof ConstantBooleanType
|| !$throwType->getValue()
$methodReflection->getName() === 'getByType'
&& count($methodCall->getArgs()) >= 2
) {
$type = TypeCombinator::addNull($type);
$throwType = $scope->getType($methodCall->getArgs()[1]->value);
if (
!$throwType instanceof ConstantBooleanType
|| !$throwType->getValue()
) {
$type = TypeCombinator::addNull($type);
}
}

$types[] = $type;
}

return $type;
return TypeCombinator::union(...$types);
}

}

0 comments on commit 4db4f3c

Please sign in to comment.