Skip to content

Commit

Permalink
[CodeQuality] Skip fopen() or die() on LogicalToBooleanRector on assi…
Browse files Browse the repository at this point in the history
…gn (#6635)

* [CodeQuality] Skip fopen() or die() on LogicalToBooleanRector

* [CodeQuality] Skip fopen() or die() on LogicalToBooleanRector

* add assign condition to ensure patch on this specific use case

* trigger CI

* Temporary pin php-parser to 5.3.1
  • Loading branch information
samsonasik authored Dec 30, 2024
1 parent eb272ee commit 17d266f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\LogicalAnd\LogicalToBooleanRector\Fixture;

final class SkipFopenOrDieInAssign
{
public function run(string $fileLocation)
{
$file = fopen($fileLocation, 'w') or die('Unable to open file!');
fwrite($file, "John Doe\nJane Doe\n");
fclose($file);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Rector\CodeQuality\Rector\LogicalAnd;

use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
use PhpParser\Node\Expr\BinaryOp\LogicalAnd;
Expand Down Expand Up @@ -51,8 +52,14 @@ public function getNodeTypes(): array
/**
* @param LogicalOr|LogicalAnd $node
*/
public function refactor(Node $node): BooleanAnd|BooleanOr
public function refactor(Node $node): BooleanAnd|BooleanOr|null
{
$type = $this->nodeTypeResolver->getNativeType($node->left);

if ($node->left instanceof Assign && ! $type->isBoolean()->yes()) {
return null;
}

return $this->refactorLogicalToBoolean($node);
}

Expand Down

0 comments on commit 17d266f

Please sign in to comment.