1414use PhpParser \Node \Stmt \Property ;
1515use Rector \NodeAnalyzer \ExprAnalyzer ;
1616use Rector \NodeTypeResolver \Node \AttributeKey ;
17+ use Rector \PhpParser \Node \BetterNodeFinder ;
18+ use Rector \PhpParser \NodeFinder \PropertyFetchFinder ;
1719use Rector \Rector \AbstractRector ;
1820use Rector \ValueObject \MethodName ;
1921use Symplify \RuleDocGenerator \ValueObject \CodeSample \CodeSample ;
@@ -27,9 +29,19 @@ final class InlineConstructorDefaultToPropertyRector extends AbstractRector
2729 * @readonly
2830 */
2931 private ExprAnalyzer $ exprAnalyzer ;
30- public function __construct (ExprAnalyzer $ exprAnalyzer )
32+ /**
33+ * @readonly
34+ */
35+ private BetterNodeFinder $ betterNodeFinder ;
36+ /**
37+ * @readonly
38+ */
39+ private PropertyFetchFinder $ propertyFetchFinder ;
40+ public function __construct (ExprAnalyzer $ exprAnalyzer , BetterNodeFinder $ betterNodeFinder , PropertyFetchFinder $ propertyFetchFinder )
3141 {
3242 $ this ->exprAnalyzer = $ exprAnalyzer ;
43+ $ this ->betterNodeFinder = $ betterNodeFinder ;
44+ $ this ->propertyFetchFinder = $ propertyFetchFinder ;
3345 }
3446 public function getRuleDefinition (): RuleDefinition
3547 {
@@ -121,11 +133,24 @@ private function matchAssignedLocalPropertyName(Assign $assign): ?string
121133 }
122134 return $ propertyName ;
123135 }
136+ private function isFoundInAnyPropertyHooks (Class_ $ class , string $ propertyName ): bool
137+ {
138+ $ propertyHooks = array_reduce ($ class ->getProperties (), static fn (array $ hooks , Property $ property ): array => array_merge ($ hooks , $ property ->hooks ), []);
139+ return (bool ) $ this ->betterNodeFinder ->findFirst ($ propertyHooks , function (Node $ subNode ) use ($ class , $ propertyName ): bool {
140+ if (!$ subNode instanceof PropertyFetch) {
141+ return \false;
142+ }
143+ return $ this ->propertyFetchFinder ->isLocalPropertyFetchByName ($ subNode , $ class , $ propertyName );
144+ });
145+ }
124146 private function refactorProperty (Class_ $ class , string $ propertyName , Expr $ defaultExpr , ClassMethod $ constructClassMethod , int $ key ): bool
125147 {
126148 if ($ class ->isReadonly ()) {
127149 return \false;
128150 }
151+ if ($ this ->isFoundInAnyPropertyHooks ($ class , $ propertyName )) {
152+ return \false;
153+ }
129154 foreach ($ class ->stmts as $ classStmt ) {
130155 if (!$ classStmt instanceof Property) {
131156 continue ;
0 commit comments