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 ;
2013use Rector \DeadCode \ConditionResolver ;
21- use Rector \DeadCode \ValueObject \VersionCompareCondition ;
2214use Rector \NodeAnalyzer \ArgsAnalyzer ;
15+ use Rector \NodeTypeResolver \Node \AttributeKey ;
2316use Rector \PhpParser \Node \Value \ValueResolver ;
2417use Rector \PHPStan \ScopeFetcher ;
2518use Rector \Rector \AbstractRector ;
@@ -87,32 +80,19 @@ public function run()
8780 */
8881 public function getNodeTypes (): array
8982 {
90- return [If_::class, Ternary::class, FuncCall::class];
83+ return [FuncCall::class];
9184 }
9285
9386 /**
94- * @param If_|Ternary|FuncCall $node
95- * @return null|NodeVisitor::DONT_TRAVERSE_CHILDREN|Node
87+ * @param FuncCall $node
9688 */
97- public function refactor (Node $ node ): null | int | Node
89+ public function refactor (Node $ node ): ? FuncCall
9890 {
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-
91+ if ($ this ->shouldSkip ($ node )) {
11292 return null ;
11393 }
11494
115- if ($ this -> shouldSkip ( $ node )) {
95+ if ($ node -> getAttribute (AttributeKey:: PHP_VERSION_CONDITIONED )) {
11696 return null ;
11797 }
11898
@@ -134,54 +114,6 @@ public function refactor(Node $node): null|int|Node
134114 return $ node ;
135115 }
136116
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-
185117 private function shouldSkip (FuncCall $ funcCall ): bool
186118 {
187119 if ($ funcCall ->isFirstClassCallable ()) {
0 commit comments