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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ tests:

.PHONY: lint
lint:
php vendor/bin/parallel-lint --colors \
php vendor/bin/parallel-lint --colors -e php,php3,php4,php5,phtml,phpt,fixed \

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

src tests

.PHONY: cs-install
Expand Down
47 changes: 29 additions & 18 deletions src/Rules/Cast/UselessCastRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,28 +55,39 @@ public function processNode(Node $node, Scope $scope): array
$expressionType = $scope->getNativeType($node->expr);
}
if ($castType->isSuperTypeOf($expressionType)->yes()) {
$addTip = function (RuleErrorBuilder $ruleErrorBuilder) use ($scope, $node, $castType): RuleErrorBuilder {
if (!$this->treatPhpDocTypesAsCertain) {
return $ruleErrorBuilder;
}
$ruleErrorBuilder = RuleErrorBuilder::message(sprintf(
'Casting to %s something that\'s already %s.',
$castType->describe(VerbosityLevel::typeOnly()),
$expressionType->describe(VerbosityLevel::typeOnly()),
))->identifier('cast.useless');

if (!$this->treatPhpDocTypesAsCertainTip) {
return $ruleErrorBuilder;
}
$nativeType = $scope->getNativeType($node->expr);
if ($castType->isSuperTypeOf($nativeType)->yes()) {
$ruleErrorBuilder->fixNode($node, static function (Cast $node) use ($nativeType): Node {
if ($node instanceof Cast\Bool_) {
if (!$nativeType->isBoolean()->yes()) {
return $node;
}
} elseif ($node instanceof Cast\Int_) {
if (
$node->expr instanceof Node\Expr\UnaryMinus
|| !$nativeType->isInteger()->yes()
) {
return $node;
}
} else {
// skip all other casts
return $node;
}

$expressionTypeWithoutPhpDoc = $scope->getNativeType($node->expr);
if ($castType->isSuperTypeOf($expressionTypeWithoutPhpDoc)->yes()) {
return $ruleErrorBuilder;
}
return $node->expr;
});
} elseif ($this->treatPhpDocTypesAsCertain && $this->treatPhpDocTypesAsCertainTip) {
$ruleErrorBuilder->treatPhpDocTypesAsCertainTip();
}

return $ruleErrorBuilder->treatPhpDocTypesAsCertainTip();
};
return [
$addTip(RuleErrorBuilder::message(sprintf(
'Casting to %s something that\'s already %s.',
$castType->describe(VerbosityLevel::typeOnly()),
$expressionType->describe(VerbosityLevel::typeOnly()),
)))->identifier('cast.useless')->build(),
$ruleErrorBuilder->build(),
];
}

Expand Down
31 changes: 30 additions & 1 deletion tests/Rules/Cast/UselessCastRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ class UselessCastRuleTest extends RuleTestCase

private bool $treatPhpDocTypesAsCertain;

private bool $treatPhpDocTypesAsCertainTip = true;

protected function getRule(): Rule
{
return new UselessCastRule(
$this->treatPhpDocTypesAsCertain,
true,
$this->treatPhpDocTypesAsCertainTip,
);
}

Expand Down Expand Up @@ -95,4 +97,31 @@ public function testBug14565(): void
$this->analyse([__DIR__ . '/data/bug-14565.php'], []);
}

public function testVoidCastIsNotFixed(): void
{
$this->treatPhpDocTypesAsCertain = true;
$this->fix(__DIR__ . '/data/bug-14565.php', __DIR__ . '/data/bug-14565.php');
}

/**
* @return iterable<array{bool, bool}>
*/
public function dataFix(): iterable
{
yield [true, true];
yield [true, false];
yield [false, true];
yield [false, false];
}

/**
* @dataProvider dataFix
*/
public function testFix(bool $treatPhpDocTypesAsCertain, bool $treatPhpDocTypesAsCertainTip): void
{
$this->treatPhpDocTypesAsCertain = $treatPhpDocTypesAsCertain;
$this->treatPhpDocTypesAsCertainTip = $treatPhpDocTypesAsCertainTip;
$this->fix(__DIR__ . '/data/useless-cast-fixable.php', __DIR__ . '/data/useless-cast-fixable.php.fixed');
}

}
115 changes: 115 additions & 0 deletions tests/Rules/Cast/data/useless-cast-fixable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php declare(strict_types = 1);

namespace UselessCastFixable;

use stdClass;

/**
* @param bool $phpDocBoolean
* @param int $phpDocInteger
* @param float $phpDocFloat
* @param string $phpDocString
* @param array<int> $phpDocArray
* @param stdClass $phpDocObject
* @param non-empty-string $string
*/
function casts(
bool $boolean,
int $integer,
float $float,
string $string,
array $array,
stdClass $object,
?int $nullableInteger,
$phpDocBoolean,
$phpDocInteger,
$phpDocFloat,
$phpDocString,
$phpDocArray,
$phpDocObject,
$mixed
): void
{
(bool) $boolean;
(int) $integer;
(float) $float;
(string) $string;
(array) $array;
(object) $object;

(bool) $phpDocBoolean;
(int) $phpDocInteger;
(float) $phpDocFloat;
(string) $phpDocString;
(array) $phpDocArray;
(object) $phpDocObject;

(string) $integer;
(int) $nullableInteger;
(int) $mixed;

if ($nullableInteger !== null) {
(int) $nullableInteger;
}
if (is_int($mixed)) {
(int) $mixed;
}

(int) 42;
(bool) true;
(string) 'value';
(array) [1, 2];
(object) new stdClass();
(int) ($integer + 1) * 2;
2 - (int) ($integer + 1);
(bool) ($boolean || true) && false;
(int) ($integer = 1) + 2;
(int) ($integer + 1) ** 2;
(int) ($boolean ? $integer : 0);
(int) ++$integer;
(int) strlen($string);
$b = $integer-(int)-$integer;
}

class Foo
{
public static function bug(string $key): void
{
$a = [$key => 1];

foreach ($a as $k => $v) {
$l = strlen((string)$k); // type cast is needed for auto-coerced non-decimal-int-string
}
}
}

// https://3v4l.org/krTct#v
function doFoo() {
$array = [0, 1];
foreach ((array) $array as &$v) {
$v=2;
}
var_dump($array);
}
function doBar() {
$array = [0, 1];
foreach ($array as &$v) {
$v=2;
}
var_dump($array);
}
doFoo();
doBar();

class X {
public int $i;
public string $s;
}

function doBaz(X $x) {
echo (int) $x->i;
echo (int) $x->s;

echo (string) $x->i;
echo (string) $x->s;
}
115 changes: 115 additions & 0 deletions tests/Rules/Cast/data/useless-cast-fixable.php.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php declare(strict_types = 1);

namespace UselessCastFixable;

use stdClass;

/**
* @param bool $phpDocBoolean
* @param int $phpDocInteger
* @param float $phpDocFloat
* @param string $phpDocString
* @param array<int> $phpDocArray
* @param stdClass $phpDocObject
* @param non-empty-string $string
*/
function casts(
bool $boolean,
int $integer,
float $float,
string $string,
array $array,
stdClass $object,
?int $nullableInteger,
$phpDocBoolean,
$phpDocInteger,
$phpDocFloat,
$phpDocString,
$phpDocArray,
$phpDocObject,
$mixed
): void
{
$boolean;
$integer;
(float) $float;
(string) $string;
(array) $array;
(object) $object;

(bool) $phpDocBoolean;
(int) $phpDocInteger;
(float) $phpDocFloat;
(string) $phpDocString;
(array) $phpDocArray;
(object) $phpDocObject;

(string) $integer;
(int) $nullableInteger;
(int) $mixed;

if ($nullableInteger !== null) {
$nullableInteger;
}
if (is_int($mixed)) {
$mixed;
}

42;
true;
(string) 'value';
(array) [1, 2];
(object) new stdClass();
$integer + 1 * 2;
2 - $integer + 1;
$boolean || true && false;
$integer = 1 + 2;
($integer + 1) ** 2;
$boolean ? $integer : 0;
++$integer;
strlen($string);
$b = $integer-(int)-$integer;
}

class Foo
{
public static function bug(string $key): void
{
$a = [$key => 1];

foreach ($a as $k => $v) {
$l = strlen((string)$k); // type cast is needed for auto-coerced non-decimal-int-string
}
}
}

// https://3v4l.org/krTct#v
function doFoo() {
$array = [0, 1];
foreach ((array) $array as &$v) {
$v=2;
}
var_dump($array);
}
function doBar() {
$array = [0, 1];
foreach ($array as &$v) {
$v=2;
}
var_dump($array);
}
doFoo();
doBar();

class X {
public int $i;
public string $s;
}

function doBaz(X $x) {
echo $x->i;
echo (int) $x->s;

echo (string) $x->i;
echo (string) $x->s;
}
Loading