66
77use PhpParser \Node ;
88use PhpParser \Node \Arg ;
9- use PhpParser \Node \Expr ;
10- use PhpParser \Node \Expr \Assign ;
119use PhpParser \Node \Expr \ConstFetch ;
1210use PhpParser \Node \Expr \FuncCall ;
13- use PhpParser \Node \Expr \Ternary ;
1411use PhpParser \Node \Scalar \String_ ;
15- use PhpParser \Node \Stmt \Expression ;
16- use PhpParser \Node \Stmt \If_ ;
17- use PhpParser \Node \Stmt \Return_ ;
18- use PhpParser \NodeVisitor ;
1912use PHPStan \Type \IntegerRangeType ;
20- use Rector \DeadCode \ConditionResolver ;
21- use Rector \DeadCode \ValueObject \VersionCompareCondition ;
2213use Rector \NodeAnalyzer \ArgsAnalyzer ;
14+ use Rector \NodeTypeResolver \Node \AttributeKey ;
2315use Rector \PhpParser \Node \Value \ValueResolver ;
2416use Rector \PHPStan \ScopeFetcher ;
2517use Rector \Rector \AbstractRector ;
@@ -48,7 +40,6 @@ final class DowngradeHashAlgorithmXxHashRector extends AbstractRector
4840 public function __construct (
4941 private readonly ArgsAnalyzer $ argsAnalyzer ,
5042 private readonly ValueResolver $ valueResolver ,
51- private readonly ConditionResolver $ conditionResolver
5243 ) {
5344 }
5445
@@ -87,32 +78,19 @@ public function run()
8778 */
8879 public function getNodeTypes (): array
8980 {
90- return [If_::class, Ternary::class, FuncCall::class];
81+ return [FuncCall::class];
9182 }
9283
9384 /**
94- * @param If_|Ternary|FuncCall $node
95- * @return null|NodeVisitor::DONT_TRAVERSE_CHILDREN|Node
85+ * @param FuncCall $node
9686 */
97- public function refactor (Node $ node ): null | int | Node
87+ public function refactor (Node $ node ): ? FuncCall
9888 {
99- if ($ node instanceof If_) {
100- if ($ this ->isVersionCompareIf ($ node )) {
101- return NodeVisitor::DONT_TRAVERSE_CHILDREN ;
102- }
103-
104- return null ;
105- }
106-
107- if ($ node instanceof Ternary) {
108- if ($ this ->isVersionCompareTernary ($ node )) {
109- return NodeVisitor::DONT_TRAVERSE_CHILDREN ;
110- }
111-
89+ if ($ this ->shouldSkip ($ node )) {
11290 return null ;
11391 }
11492
115- if ($ this -> shouldSkip ( $ node )) {
93+ if ($ node -> getAttribute (AttributeKey:: PHP_VERSION_CONDITIONED )) {
11694 return null ;
11795 }
11896
@@ -134,54 +112,6 @@ public function refactor(Node $node): null|int|Node
134112 return $ node ;
135113 }
136114
137- private function isVersionCompareIf (If_ $ if ): bool
138- {
139- if ($ if ->cond instanceof FuncCall) {
140- // per use case reported only
141- if (count ($ if ->stmts ) !== 1 ) {
142- return false ;
143- }
144-
145- $ versionCompare = $ this ->conditionResolver ->resolveFromExpr ($ if ->cond );
146-
147- if (! $ versionCompare instanceof VersionCompareCondition || $ versionCompare ->getSecondVersion () !== 80100 ) {
148- return false ;
149- }
150-
151- if ($ versionCompare ->getCompareSign () !== '>= ' ) {
152- return false ;
153- }
154-
155- if ($ if ->stmts [0 ] instanceof Expression && $ if ->stmts [0 ]->expr instanceof Assign && $ if ->stmts [0 ]->expr ->expr instanceof FuncCall) {
156- return $ this ->isName ($ if ->stmts [0 ]->expr ->expr , 'hash ' );
157- }
158-
159- if ($ if ->stmts [0 ] instanceof Return_ && $ if ->stmts [0 ]->expr instanceof FuncCall) {
160- return $ this ->isName ($ if ->stmts [0 ]->expr , 'hash ' );
161- }
162- }
163-
164- return false ;
165- }
166-
167- private function isVersionCompareTernary (Ternary $ ternary ): bool
168- {
169- if ($ ternary ->if instanceof Expr && $ ternary ->cond instanceof FuncCall) {
170- $ versionCompare = $ this ->conditionResolver ->resolveFromExpr ($ ternary ->cond );
171- if ($ versionCompare instanceof VersionCompareCondition && $ versionCompare ->getSecondVersion () === 80100 ) {
172- if ($ versionCompare ->getCompareSign () === '>= ' ) {
173- return $ ternary ->if instanceof FuncCall && $ this ->isName ($ ternary ->if , 'hash ' );
174- }
175-
176- if ($ versionCompare ->getCompareSign () === '< ' ) {
177- return $ ternary ->else instanceof FuncCall && $ this ->isName ($ ternary ->else , 'hash ' );
178- }
179- }
180- }
181-
182- return false ;
183- }
184-
185115 private function shouldSkip (FuncCall $ funcCall ): bool
186116 {
187117 if ($ funcCall ->isFirstClassCallable ()) {
0 commit comments