Skip to content
Closed
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
58 changes: 58 additions & 0 deletions src/Type/Php/GeneratorSendReturnTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Php;

use Generator;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Reflection\ExtendedParametersAcceptor;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\BenevolentUnionType;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\NullType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\TypeUtils;

#[AutowiredService]
final class GeneratorSendReturnTypeExtension implements DynamicMethodReturnTypeExtension
{

public function getClass(): string
{
return Generator::class;
}

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

public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
{
$parametersAcceptor = ParametersAcceptorSelector::selectFromArgs(
$scope,
$methodCall->getArgs(),
$methodReflection->getVariants(),
);

$returnType = $parametersAcceptor->getReturnType();

if ($parametersAcceptor instanceof ExtendedParametersAcceptor) {
$nativeReturnType = $parametersAcceptor->getNativeReturnType();
if ($nativeReturnType->isSuperTypeOf(new NullType())->no()) {
return $returnType;
}
}

$result = TypeCombinator::addNull($returnType);
if ($returnType instanceof BenevolentUnionType && !($result instanceof BenevolentUnionType)) {
$result = TypeUtils::toBenevolentUnion($result);
}

return $result;
}

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

namespace PHPStan\Type\Php;

use Iterator;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Reflection\ExtendedParametersAcceptor;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\BenevolentUnionType;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\NullType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\TypeUtils;
use function in_array;

#[AutowiredService]
final class IteratorCurrentReturnTypeExtension implements DynamicMethodReturnTypeExtension
{

public function getClass(): string
{
return Iterator::class;
}

public function isMethodSupported(MethodReflection $methodReflection): bool
{
return in_array($methodReflection->getName(), ['current', 'key'], true);
}

public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
{
$parametersAcceptor = ParametersAcceptorSelector::selectFromArgs(
$scope,
$methodCall->getArgs(),
$methodReflection->getVariants(),
);

$returnType = $parametersAcceptor->getReturnType();

if ($parametersAcceptor instanceof ExtendedParametersAcceptor) {
$nativeReturnType = $parametersAcceptor->getNativeReturnType();
if ($nativeReturnType->isSuperTypeOf(new NullType())->no()) {

Check warning on line 46 in src/Type/Php/IteratorCurrentReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ if ($parametersAcceptor instanceof ExtendedParametersAcceptor) { $nativeReturnType = $parametersAcceptor->getNativeReturnType(); - if ($nativeReturnType->isSuperTypeOf(new NullType())->no()) { + if (!$nativeReturnType->isSuperTypeOf(new NullType())->yes()) { return $returnType; } }

Check warning on line 46 in src/Type/Php/IteratorCurrentReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\IsSuperTypeOfCalleeAndArgumentMutator": @@ @@ if ($parametersAcceptor instanceof ExtendedParametersAcceptor) { $nativeReturnType = $parametersAcceptor->getNativeReturnType(); - if ($nativeReturnType->isSuperTypeOf(new NullType())->no()) { + if ((new NullType())->isSuperTypeOf($nativeReturnType)->no()) { return $returnType; } }
return $returnType;
}
}

$result = TypeCombinator::addNull($returnType);
if ($returnType instanceof BenevolentUnionType && !($result instanceof BenevolentUnionType)) {
$result = TypeUtils::toBenevolentUnion($result);
}

return $result;
}

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

namespace PHPStan\Type\Php;

use Iterator;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Identifier;
use PHPStan\Analyser\Scope;
use PHPStan\Analyser\SpecifiedTypes;
use PHPStan\Analyser\TypeSpecifier;
use PHPStan\Analyser\TypeSpecifierAwareExtension;
use PHPStan\Analyser\TypeSpecifierContext;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\MethodTypeSpecifyingExtension;

#[AutowiredService]
final class IteratorValidMethodTypeSpecifyingExtension implements MethodTypeSpecifyingExtension, TypeSpecifierAwareExtension
{

private TypeSpecifier $typeSpecifier;

public function setTypeSpecifier(TypeSpecifier $typeSpecifier): void
{
$this->typeSpecifier = $typeSpecifier;
}

public function getClass(): string
{
return Iterator::class;
}

public function isMethodSupported(MethodReflection $methodReflection, MethodCall $node, TypeSpecifierContext $context): bool
{
return $methodReflection->getName() === 'valid'
&& $context->truthy();

Check warning on line 37 in src/Type/Php/IteratorValidMethodTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrueTruthyFalseFalseyTypeSpecifierContextMutator": @@ @@ public function isMethodSupported(MethodReflection $methodReflection, MethodCall $node, TypeSpecifierContext $context): bool { return $methodReflection->getName() === 'valid' - && $context->truthy(); + && $context->true(); } public function specifyTypes(MethodReflection $methodReflection, MethodCall $node, Scope $scope, TypeSpecifierContext $context): SpecifiedTypes
}

public function specifyTypes(MethodReflection $methodReflection, MethodCall $node, Scope $scope, TypeSpecifierContext $context): SpecifiedTypes
{
$calledOnType = $scope->getType($node->var);
$types = new SpecifiedTypes();

foreach (['current', 'key'] as $methodName) {
$methodCallExpr = new MethodCall($node->var, new Identifier($methodName));

$targetMethodReflection = $scope->getMethodReflection($calledOnType, $methodName);
if ($targetMethodReflection === null) {
continue;
}

$parametersAcceptor = ParametersAcceptorSelector::selectFromArgs(
$scope,
[],
$targetMethodReflection->getVariants(),
);

$baseReturnType = $parametersAcceptor->getReturnType();

$types = $types->unionWith($this->typeSpecifier->create(
$methodCallExpr,
$baseReturnType,
TypeSpecifierContext::createTrue(),
$scope,
));
}

return $types;
}

}
Loading
Loading