Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\BoolReturnTypeFromBooleanConstAndStrictReturnsRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class BoolReturnTypeFromBooleanConstAndStrictReturnsRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

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

final class MixedConstAndStrict
{
public function checkContext($context)
{
if ($context === []) {
return true;
}

return count($context) > 0;
}
}

?>
-----
<?php

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

final class MixedConstAndStrict
{
public function checkContext($context): bool
{
if ($context === []) {
return true;
}

return count($context) > 0;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

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

final class MixedMultipleReturns
{
private $context;

public function checkContext($context)
{
if (empty($this->context)) {
return true;
}

if ($this->context == $context) {
return true;
}

return 0 === stripos($this->context, (string) $context);
}
}

?>
-----
<?php

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

final class MixedMultipleReturns
{
private $context;

public function checkContext($context): bool
{
if (empty($this->context)) {
return true;
}

if ($this->context == $context) {
return true;
}

return 0 === stripos($this->context, (string) $context);
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

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

final class SkipNonBoolMix
{
public function resolve($value)
{
if ($value) {
return true;
}

return $value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

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

final class SkipOnlyConst
{
public function resolve($value)
{
if ($value) {
return false;
}

return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

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

final class SkipOnlyStrict
{
public function resolve($first, $second)
{
if ($first > $second) {
return $first === $second;
}

return $first < $second;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\TypeDeclaration\Rector\ClassMethod\BoolReturnTypeFromBooleanConstAndStrictReturnsRector;

return RectorConfig::configure()
->withRules([BoolReturnTypeFromBooleanConstAndStrictReturnsRector::class]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
<?php

declare(strict_types=1);

namespace Rector\TypeDeclaration\Rector\ClassMethod;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\Return_;
use PHPStan\Analyser\Scope;
use Rector\NodeAnalyzer\ExprAnalyzer;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\PHPStan\ScopeFetcher;
use Rector\Rector\AbstractRector;
use Rector\TypeDeclaration\NodeAnalyzer\ReturnAnalyzer;
use Rector\ValueObject\PhpVersionFeature;
use Rector\VendorLocker\NodeVendorLocker\ClassMethodReturnTypeOverrideGuard;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\BoolReturnTypeFromBooleanConstAndStrictReturnsRector\BoolReturnTypeFromBooleanConstAndStrictReturnsRectorTest
*/
final class BoolReturnTypeFromBooleanConstAndStrictReturnsRector extends AbstractRector implements MinPhpVersionInterface
{
public function __construct(
private readonly ValueResolver $valueResolver,
private readonly BetterNodeFinder $betterNodeFinder,
private readonly ClassMethodReturnTypeOverrideGuard $classMethodReturnTypeOverrideGuard,
private readonly ReturnAnalyzer $returnAnalyzer,
private readonly ExprAnalyzer $exprAnalyzer
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Add bool return type when returns mix direct true/false consts with strict bool expressions',
[
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function resolve($value)
{
if ($value === []) {
return true;
}

return count($value) > 0;
}
}
CODE_SAMPLE

,
<<<'CODE_SAMPLE'
class SomeClass
{
public function resolve($value): bool
{
if ($value === []) {
return true;
}

return count($value) > 0;
}
}
CODE_SAMPLE
),
]
);
}

/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [ClassMethod::class, Function_::class];
}

/**
* @param ClassMethod|Function_ $node
*/
public function refactor(Node $node): ?Node
{
$scope = ScopeFetcher::fetch($node);
if ($this->shouldSkip($node, $scope)) {
return null;
}

$returns = $this->betterNodeFinder->findReturnsScoped($node);
if (! $this->returnAnalyzer->hasOnlyReturnWithExpr($node, $returns)) {
return null;
}

if (! $this->hasMixedBoolConstAndStrictReturns($returns)) {
return null;
}

$node->returnType = new Identifier('bool');

return $node;
}

public function provideMinPhpVersion(): int
{
return PhpVersionFeature::SCALAR_TYPES;
}

/**
* @param ClassMethod|Function_|Closure $node
*/
private function shouldSkip(Node $node, Scope $scope): bool
{
// already has the type, skip
if ($node->returnType instanceof Node) {
return true;
}

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

/**
* Every return must be bool, mixing at least one true/false const with at least one strict bool expression.
* Pure-const returns are handled by BoolReturnTypeFromBooleanConstReturnsRector, pure-strict by
* BoolReturnTypeFromBooleanStrictReturnsRector, so this rule stays disjoint from both.
*
* @param Return_[] $returns
*/
private function hasMixedBoolConstAndStrictReturns(array $returns): bool
{
$hasConstReturn = false;
$hasStrictReturn = false;

foreach ($returns as $return) {
if (! $return->expr instanceof Expr) {
return false;
}

if ($this->isBoolConst($return->expr)) {
$hasConstReturn = true;
continue;
}

if ($this->isStrictBool($return->expr)) {
$hasStrictReturn = true;
continue;
}

return false;
}

return $hasConstReturn && $hasStrictReturn;
}

private function isBoolConst(Expr $expr): bool
{
return $expr instanceof ConstFetch && $this->valueResolver->isTrueOrFalse($expr);
}

private function isStrictBool(Expr $expr): bool
{
return $this->exprAnalyzer->isBoolExpr($expr) || $this->exprAnalyzer->isCallLikeReturnNativeBool($expr);
}
}
2 changes: 2 additions & 0 deletions src/Config/Level/TypeDeclarationLevel.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use Rector\TypeDeclaration\Rector\ClassMethod\AddReturnTypeFromTryCatchTypeRector;
use Rector\TypeDeclaration\Rector\ClassMethod\AddVoidReturnTypeWhereNoReturnRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ArrayParamTypeByMethodCallTypeRector;
use Rector\TypeDeclaration\Rector\ClassMethod\BoolReturnTypeFromBooleanConstAndStrictReturnsRector;
use Rector\TypeDeclaration\Rector\ClassMethod\BoolReturnTypeFromBooleanConstReturnsRector;
use Rector\TypeDeclaration\Rector\ClassMethod\BoolReturnTypeFromBooleanStrictReturnsRector;
use Rector\TypeDeclaration\Rector\ClassMethod\KnownMagicClassMethodTypeRector;
Expand Down Expand Up @@ -111,6 +112,7 @@ final class TypeDeclarationLevel
NumericReturnTypeFromStrictScalarReturnsRector::class,

BoolReturnTypeFromBooleanStrictReturnsRector::class,
BoolReturnTypeFromBooleanConstAndStrictReturnsRector::class,
StringReturnTypeFromStrictStringReturnsRector::class,
NumericReturnTypeFromStrictReturnsRector::class,

Expand Down
Loading