Skip to content

Commit

Permalink
Deprecate ReturnTypeFromStrictScalarReturnExprRector functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jul 3, 2024
1 parent e55b834 commit 552535e
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 234 deletions.
2 changes: 1 addition & 1 deletion build/target-repository/docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -7431,7 +7431,7 @@ Add string return type based on returned string scalar values
final class SomeClass
{
- public function foo($condition)
+ public function foo($condition): string;
+ public function foo($condition): string
{
if ($condition) {
return 'yes';
Expand Down
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -296,3 +296,5 @@ parameters:
message: '#Parameters should have "PhpParser\\Node\\Stmt\\ClassMethod" types as the only types passed to this method#'
path: src/VendorLocker/ParentClassMethodTypeOverrideGuard.php

# deprecated
- '#Property Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromStrictScalarReturnExprRector\:\:\$hardCodedOnly is never read, only written#'

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,11 @@
use PhpParser\Node;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\UnionType;
use PHPStan\Analyser\Scope;
use PHPStan\Type\NullType;
use PHPStan\Type\Type;
use Rector\Configuration\Deprecation\Contract\DeprecatedInterface;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Rector\Rector\AbstractScopeAwareRector;
use Rector\StaticTypeMapper\StaticTypeMapper;
use Rector\TypeDeclaration\NodeAnalyzer\ReturnTypeAnalyzer\StrictScalarReturnTypeAnalyzer;
use Rector\ValueObject\PhpVersion;
use Rector\VendorLocker\NodeVendorLocker\ClassMethodReturnTypeOverrideGuard;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand All @@ -35,15 +28,11 @@ final class ReturnTypeFromStrictScalarReturnExprRector extends AbstractScopeAwar
*/
public const HARD_CODED_ONLY = 'hard_coded_only';

/**
* @api
*/
private bool $hardCodedOnly = false;

public function __construct(
private readonly StrictScalarReturnTypeAnalyzer $strictScalarReturnTypeAnalyzer,
private readonly ClassMethodReturnTypeOverrideGuard $classMethodReturnTypeOverrideGuard,
private readonly StaticTypeMapper $staticTypeMapper
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Change return type based on strict scalar returns - string, int, float or bool', [
Expand Down Expand Up @@ -106,43 +95,8 @@ public function getNodeTypes(): array
*/
public function refactorWithScope(Node $node, Scope $scope): ?Node
{
// already added → skip
if ($node->returnType instanceof Node) {
return null;
}

$scalarReturnType = $this->strictScalarReturnTypeAnalyzer->matchAlwaysScalarReturnType(
$node,
$this->hardCodedOnly
);
if (! $scalarReturnType instanceof Type) {
return null;
}

// skip null as often placeholder value and not an only type
if ($scalarReturnType instanceof NullType) {
return null;
}

$returnTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($scalarReturnType, TypeKind::RETURN);
if (! $returnTypeNode instanceof Node) {
return null;
}

if ($node instanceof ClassMethod && $this->classMethodReturnTypeOverrideGuard->shouldSkipClassMethod(
$node,
$scope
)) {
return null;
}

// handled by another rule
if ($returnTypeNode instanceof UnionType) {
return null;
}

$node->returnType = $returnTypeNode;
return $node;
// deprecated
return null;
}

public function provideMinPhpVersion(): int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function foo($condition)
<<<'CODE_SAMPLE'
final class SomeClass
{
public function foo($condition): string;
public function foo($condition): string
{
if ($condition) {
return 'yes';
Expand Down
106 changes: 0 additions & 106 deletions rules/TypeDeclaration/TypeAnalyzer/AlwaysStrictScalarExprAnalyzer.php

This file was deleted.

0 comments on commit 552535e

Please sign in to comment.