Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add - number support, deprecate ReturnTypeFromStrictScalarReturnExprRector functionality, already split #6114

Merged
merged 3 commits into from
Jul 3, 2024
Merged
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
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#'
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\NumericReturnTypeFromStrictReturnsRector\Fixture;

final class DirectReturn
final class SkipDirectReturn
{
public function resolve()
{
return 1000;
}

public function next()
{
return -400;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\NumericReturnTypeFromStrictScalarReturnsRector\Fixture;

final class IncludeUnaryMinus
{
public function resolve()
{
return -1000;
}

public function next()
{
return +1000;
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\NumericReturnTypeFromStrictScalarReturnsRector\Fixture;

final class IncludeUnaryMinus
{
public function resolve(): int
{
return -1000;
}

public function next()
{
return +1000;
}
}

?>

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\TypeDeclaration\Rector\ClassMethod;

use PhpParser\Node\Expr\UnaryMinus;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Identifier;
Expand Down Expand Up @@ -151,11 +152,16 @@ private function isAlwaysNumeric(array $returns): bool
$isAlwaysInt = true;

foreach ($returns as $return) {
if (! $return->expr instanceof DNumber) {
$epxr = $return->expr;
if ($epxr instanceof UnaryMinus) {
$epxr = $epxr->expr;
}

if (! $epxr instanceof DNumber) {
$isAlwaysFloat = false;
}

if (! $return->expr instanceof LNumber) {
if (! $epxr instanceof LNumber) {
$isAlwaysInt = false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\TypeDeclaration\Rector\ClassMethod;

use PhpParser\Node\Expr\UnaryMinus;
use PhpParser\Node;
use PhpParser\Node\Identifier;
use PhpParser\Node\Scalar\DNumber;
Expand Down Expand Up @@ -84,11 +85,16 @@ public function refactorWithScope(Node $node, Scope $scope): ?Node
$isAlwaysFloat = true;

foreach ($returns as $return) {
if (! $return->expr instanceof DNumber) {
$expr = $return->expr;
if ($expr instanceof UnaryMinus) {
$expr = $expr->expr;
}

if (! $expr instanceof DNumber) {
$isAlwaysFloat = false;
}

if (! $return->expr instanceof LNumber) {
if (! $expr instanceof LNumber) {
$isAlwaysInt = false;
}
}
Expand Down
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,6 @@ final class ReturnTypeFromStrictScalarReturnExprRector extends AbstractScopeAwar
*/
public const HARD_CODED_ONLY = 'hard_coded_only';

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 +90,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 All @@ -154,7 +103,5 @@ public function configure(array $configuration): void
{
$hardCodedOnly = $configuration[self::HARD_CODED_ONLY] ?? false;
Assert::boolean($hardCodedOnly);

$this->hardCodedOnly = $hardCodedOnly;
}
}
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
Loading