-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathDowngradeStreamIsattyRector.php
More file actions
163 lines (135 loc) · 4.6 KB
/
DowngradeStreamIsattyRector.php
File metadata and controls
163 lines (135 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
declare(strict_types=1);
namespace Rector\DowngradePhp72\Rector\FuncCall;
use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Echo_;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Return_;
use PhpParser\Node\Stmt\Switch_;
use PHPStan\Analyser\Scope;
use Rector\Exception\ShouldNotHappenException;
use Rector\Naming\Naming\VariableNaming;
use Rector\NodeAnalyzer\ExprInTopStmtMatcher;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpParser\Enum\NodeGroup;
use Rector\PhpParser\Parser\InlineCodeParser;
use Rector\PHPStan\ScopeFetcher;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @changelog https://github.com/symfony/polyfill/commit/cc2bf55accd32b989348e2039e8c91cde46aebed
*
* @see \Rector\Tests\DowngradePhp72\Rector\FuncCall\DowngradeStreamIsattyRector\DowngradeStreamIsattyRectorTest
*/
final class DowngradeStreamIsattyRector extends AbstractRector
{
private ?Closure $cachedClosure = null;
public function __construct(
private readonly InlineCodeParser $inlineCodeParser,
private readonly VariableNaming $variableNaming,
private readonly ExprInTopStmtMatcher $exprInTopStmtMatcher
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Downgrade stream_isatty() function', [
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function run($stream)
{
$isStream = stream_isatty($stream);
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
public function run($stream)
{
$streamIsatty = 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 ? 0020000 === ($stat['mode'] & 0170000) : false;
}
return \function_exists('posix_isatty') && @posix_isatty($stream);
};
$isStream = $streamIsatty($stream);
}
}
CODE_SAMPLE
),
]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
$stmtsAware = NodeGroup::STMTS_AWARE;
return [...$stmtsAware, Switch_::class, Return_::class, Expression::class, Echo_::class];
}
/**
* @param StmtsAware|Switch_|Return_|Expression|Echo_ $node
* @return Node[]|null
*/
public function refactor(Node $node): ?array
{
$expr = $this->exprInTopStmtMatcher->match(
$node,
function (Node $subNode): bool {
if (! $subNode instanceof FuncCall) {
return false;
}
if (! $this->isName($subNode, 'stream_isatty')) {
return false;
}
// need pull Scope from target traversed sub Node
$scope = $subNode->getAttribute(AttributeKey::SCOPE);
if (! $scope instanceof Scope) {
return false;
}
return ! $scope->isInFunctionExists('stream_isatty');
}
);
if (! $expr instanceof FuncCall) {
return null;
}
$function = $this->createClosure();
$scope = ScopeFetcher::fetch($node);
$variable = new Variable($this->variableNaming->createCountedValueName('streamIsatty', $scope));
$assign = new Assign($variable, $function);
$expr->name = $variable;
return [new Expression($assign), $node];
}
private function createClosure(): Closure
{
if ($this->cachedClosure instanceof Closure) {
return clone $this->cachedClosure;
}
$stmts = $this->inlineCodeParser->parseFile(__DIR__ . '/../../snippet/isatty_closure.php.inc');
/** @var Expression $expression */
$expression = $stmts[0];
$expr = $expression->expr;
if (! $expr instanceof Closure) {
throw new ShouldNotHappenException();
}
$this->cachedClosure = $expr;
return $expr;
}
}