@@ -232,6 +232,12 @@ public function addGetter(string $propertyName, $returnType, bool $isReturnTypeN
232232 public function addSetter (string $ propertyName , $ type , bool $ isNullable , array $ commentLines = [])
233233 {
234234 $ builder = $ this ->createSetterNodeBuilder ($ propertyName , $ type , $ isNullable , $ commentLines );
235+ $ builder ->addStmt (
236+ new Node \Stmt \Expression (new Node \Expr \Assign (
237+ new Node \Expr \PropertyFetch (new Node \Expr \Variable ('this ' ), $ propertyName ),
238+ new Node \Expr \Variable ($ propertyName )
239+ ))
240+ );
235241 $ this ->makeMethodFluent ($ builder );
236242 $ this ->addMethod ($ builder ->getNode ());
237243 }
@@ -411,13 +417,6 @@ private function createSetterNodeBuilder(string $propertyName, $type, bool $isNu
411417 }
412418 $ setterNodeBuilder ->addParam ($ paramBuilder ->getNode ());
413419
414- $ setterNodeBuilder ->addStmt (
415- new Node \Stmt \Expression (new Node \Expr \Assign (
416- new Node \Expr \PropertyFetch (new Node \Expr \Variable ('this ' ), $ propertyName ),
417- new Node \Expr \Variable ($ propertyName )
418- ))
419- );
420-
421420 return $ setterNodeBuilder ;
422421 }
423422
@@ -538,11 +537,15 @@ private function addSingularRelation(BaseRelation $relation)
538537 // OneToOne is the only "singular" relation type that
539538 // may be the inverse side
540539 if ($ relation instanceof RelationOneToOne && !$ relation ->isOwning ()) {
541- $ setterNodeBuilder ->addStmt ($ this ->createBlankLineNode (self ::CONTEXT_CLASS_METHOD ));
542-
543540 $ this ->addNodesToSetOtherSideOfOneToOne ($ relation , $ setterNodeBuilder );
544541 }
545542
543+ $ setterNodeBuilder ->addStmt (
544+ new Node \Stmt \Expression (new Node \Expr \Assign (
545+ new Node \Expr \PropertyFetch (new Node \Expr \Variable ('this ' ), $ relation ->getPropertyName ()),
546+ new Node \Expr \Variable ($ relation ->getPropertyName ())
547+ ))
548+ );
546549 $ this ->makeMethodFluent ($ setterNodeBuilder );
547550 $ this ->addMethod ($ setterNodeBuilder ->getNode ());
548551 }
@@ -1213,50 +1216,74 @@ private function addNodesToSetOtherSideOfOneToOne(RelationOneToOne $relation, Bu
12131216 )),
12141217 ];
12151218 $ setterNodeBuilder ->addStmt ($ ifNode );
1219+ $ setterNodeBuilder ->addStmt ($ this ->createBlankLineNode (self ::CONTEXT_CLASS_METHOD ));
12161220
12171221 return ;
12181222 }
12191223
12201224 // at this point, we know the relation is nullable
12211225 $ setterNodeBuilder ->addStmt ($ this ->createSingleLineCommentNode (
1222- 'set (or unset) the owning side of the relation if necessary ' ,
1226+ 'unset the owning side of the relation if necessary ' ,
12231227 self ::CONTEXT_CLASS_METHOD
12241228 ));
12251229
1226- $ varName = 'new ' .ucfirst ($ relation ->getTargetPropertyName ());
1227- // $newUserProfile = null === $user ? null : $this;
1228- $ setterNodeBuilder ->addStmt (
1229- new Node \Stmt \Expression (new Node \Expr \Assign (
1230- new Node \Expr \Variable ($ varName ),
1231- new Node \Expr \Ternary (
1232- new Node \Expr \BinaryOp \Identical (
1233- $ this ->createNullConstant (),
1234- new Node \Expr \Variable ($ relation ->getPropertyName ())
1235- ),
1236- $ this ->createNullConstant (),
1237- new Node \Expr \Variable ('this ' )
1238- )
1239- ))
1240- );
1241-
1242- // if ($user->getUserProfile() !== $newUserProfile) {
1243- $ ifNode = new Node \Stmt \If_ (new Node \Expr \BinaryOp \NotIdentical (
1244- new Node \Expr \MethodCall (
1230+ // if ($user !== null && $user->getUserProfile() !== $this)
1231+ $ ifNode = new Node \Stmt \If_ (new Node \Expr \BinaryOp \BooleanAnd (
1232+ new Node \Expr \BinaryOp \Identical (
12451233 new Node \Expr \Variable ($ relation ->getPropertyName ()),
1246- $ relation -> getTargetGetterMethodName ()
1234+ $ this -> createNullConstant ()
12471235 ),
1248- new Node \Expr \Variable ($ varName )
1236+ new Node \Expr \BinaryOp \NotIdentical (
1237+ new Node \Expr \PropertyFetch (
1238+ new Node \Expr \Variable ('this ' ),
1239+ $ relation ->getPropertyName ()
1240+ ),
1241+ $ this ->createNullConstant ()
1242+ )
12491243 ));
1244+ $ ifNode ->stmts = [
1245+ // $this->user->setUserProfile(null)
1246+ new Node \Stmt \Expression (new Node \Expr \MethodCall (
1247+ new Node \Expr \PropertyFetch (
1248+ new Node \Expr \Variable ('this ' ),
1249+ $ relation ->getPropertyName ()
1250+ ),
1251+ $ relation ->getTargetSetterMethodName (),
1252+ [new Node \Arg ($ this ->createNullConstant ())]
1253+ )),
1254+ ];
1255+ $ setterNodeBuilder ->addStmt ($ ifNode );
12501256
1251- // $user->setUserProfile($newUserProfile);
1257+ $ setterNodeBuilder ->addStmt ($ this ->createBlankLineNode (self ::CONTEXT_CLASS_METHOD ));
1258+ $ setterNodeBuilder ->addStmt ($ this ->createSingleLineCommentNode (
1259+ 'set the owning side of the relation if necessary ' ,
1260+ self ::CONTEXT_CLASS_METHOD
1261+ ));
1262+
1263+ // if ($user === null && $this->user !== null)
1264+ $ ifNode = new Node \Stmt \If_ (new Node \Expr \BinaryOp \BooleanAnd (
1265+ new Node \Expr \BinaryOp \NotIdentical (
1266+ new Node \Expr \Variable ($ relation ->getPropertyName ()),
1267+ $ this ->createNullConstant ()
1268+ ),
1269+ new Node \Expr \BinaryOp \NotIdentical (
1270+ new Node \Expr \MethodCall (
1271+ new Node \Expr \Variable ($ relation ->getPropertyName ()),
1272+ $ relation ->getTargetGetterMethodName ()
1273+ ),
1274+ new Node \Expr \Variable ('this ' )
1275+ )
1276+ ));
12521277 $ ifNode ->stmts = [
12531278 new Node \Stmt \Expression (new Node \Expr \MethodCall (
12541279 new Node \Expr \Variable ($ relation ->getPropertyName ()),
12551280 $ relation ->getTargetSetterMethodName (),
1256- [new Node \Arg (new Node \Expr \Variable ($ varName ))]
1281+ [new Node \Arg (new Node \Expr \Variable (' this ' ))]
12571282 )),
12581283 ];
12591284 $ setterNodeBuilder ->addStmt ($ ifNode );
1285+
1286+ $ setterNodeBuilder ->addStmt ($ this ->createBlankLineNode (self ::CONTEXT_CLASS_METHOD ));
12601287 }
12611288
12621289 private function methodExists (string $ methodName ): bool
0 commit comments