Skip to content

Commit

Permalink
[DowngradePhp72] Handle variable exists on DowngradeStreamIsattyRector (
Browse files Browse the repository at this point in the history
#1997)

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Apr 2, 2022
1 parent 232257d commit e077d7c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Rector\Tests\DowngradePhp72\Rector\FuncCall\DowngradeStreamIsattyRector\Fixture;

class VariableExists
{
public function run($stream)
{
$streamIsatty = function () {};
$isStream = stream_isatty($stream);

$streamIsatty();
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp72\Rector\FuncCall\DowngradeStreamIsattyRector\Fixture;

class VariableExists
{
public function run($stream)
{
$streamIsatty = function () {};
$streamIsatty2 = function ($stream) {
if (\function_exists('stream_isatty')) {
return stream_isatty($stream);
}
if (!\is_resource($stream)) {
trigger_error('stream_isatty() expects parameter 1 to be resource, ' . \gettype($stream) . ' given', \E_USER_WARNING);
return false;
}
if ('\\' === \DIRECTORY_SEPARATOR) {
$stat = @fstat($stream);
// Check if formatted mode is S_IFCHR
return $stat ? 020000 === ($stat['mode'] & 0170000) : false;
}
return \function_exists('posix_isatty') && @posix_isatty($stream);
};
$isStream = $streamIsatty2($stream);

$streamIsatty();
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use Rector\Core\PhpParser\Parser\InlineCodeParser;
use Rector\Core\Rector\AbstractRector;
use Rector\DowngradePhp72\NodeAnalyzer\FunctionExistsFunCallAnalyzer;
use Rector\Naming\Naming\VariableNaming;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

Expand All @@ -26,7 +28,8 @@ final class DowngradeStreamIsattyRector extends AbstractRector
{
public function __construct(
private readonly InlineCodeParser $inlineCodeParser,
private readonly FunctionExistsFunCallAnalyzer $functionExistsFunCallAnalyzer
private readonly FunctionExistsFunCallAnalyzer $functionExistsFunCallAnalyzer,
private readonly VariableNaming $variableNaming
) {
}

Expand Down Expand Up @@ -99,11 +102,13 @@ public function refactor(Node $node): ?Node
}

$function = $this->createClosure();
$assign = new Assign(new Variable('streamIsatty'), $function);
$scope = $node->getAttribute(AttributeKey::SCOPE);
$variable = new Variable($this->variableNaming->createCountedValueName('streamIsatty', $scope));
$assign = new Assign($variable, $function);

$this->nodesToAddCollector->addNodeBeforeNode($assign, $node);

return new FuncCall(new Variable('streamIsatty'), $node->args);
return new FuncCall($variable, $node->args);
}

private function createClosure(): Closure
Expand Down

0 comments on commit e077d7c

Please sign in to comment.