{"id":20747,"date":"2018-08-02T14:09:29","date_gmt":"2018-08-02T14:09:29","guid":{"rendered":"https:\/\/blog.jetbrains.com\/phpstorm\/?p=13109"},"modified":"2024-01-09T14:51:40","modified_gmt":"2024-01-09T13:51:40","slug":"move-instance-method-refactoring","status":"publish","type":"phpstorm","link":"https:\/\/blog.jetbrains.com\/pt-br\/phpstorm\/2018\/08\/move-instance-method-refactoring","title":{"rendered":"Move Instance Method Refactoring"},"content":{"rendered":"<p>When you need to move a method across the class hierarchy, PhpStorm provides you with several facilities for this. For example, you can move some functionality between a class and its parent using the <em>Pull Up\/Push Down<\/em> refactorings. Or you can use the <em>Make Static<\/em> refactoring in combination with <em>Move Static Method<\/em> to move a method to a different class.<\/p>\n<p>To cover the cases when a method cannot be made static or moved across the class hierarchy, PhpStorm 2018.2 introduces a new <em>Move Instance Method<\/em> refactoring.<\/p>\n<p>As its name states, the new refactoring allows moving an instance method to a parameter class or to a field class. Why use it after all? The primary reason is reducing the dependency between classes. If your method uses the data mostly contained in a different class, it probably makes sense to keep the method itself there, too, for simplicity and better consistency. See <a href=\"https:\/\/refactoring.com\/catalog\/moveMethod.html\" target=\"_blank\" rel=\"noopener\">here<\/a> for more theory behind this.<\/p>\n<p><!--more--><\/p>\n<p>To apply the refactoring, simply invoke the <em>Move<\/em> action on a method declaration. PhpStorm will check if the new refactoring can be applied and will then prompt you to choose the target class and the visibility scope for the method to be moved:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2018\/07\/phpstorm-basic_move_thumbnail.png\" alt=\"Basic move refactoring\" width=\"734\" data-gif-src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2018\/07\/phpstorm-basic_move.gif\" \/><\/p>\n<p>If the method being moved contains the <span class=\"lang:default highlight:0 decode:true crayon-inline \">$this<\/span>\u00a0 references to the current class, PhpStorm will let you add a parameter to replace them. The <span class=\"lang:default highlight:0 decode:true crayon-inline \">self<\/span>\u00a0 or <span class=\"lang:default highlight:0 decode:true crayon-inline \">parent<\/span>\u00a0 references will be handled correctly, too:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2018\/07\/phpstorm-references_move_thumbnail_edited.png\" alt=\"References move refactoring\" width=\"620\" data-gif-src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2018\/07\/phpstorm-references_move_edited.gif\" \/><\/p>\n<p>With this new refactoring in place, moving methods between the related classes should become easier, and your classes should become more coherent. As always, please share your feedback on what works or doesn&#8217;t work for you, and we&#8217;ll look into it.<\/p>\n<p><em>Your JetBrains PhpStorm Team<\/em><br \/>\n<em>The Drive to Develop<\/em><\/p>\n","protected":false},"author":745,"featured_media":0,"comment_status":"open","ping_status":"open","template":"","categories":[808,907],"tags":[743,91,195],"cross-post-tag":[],"acf":[],"_links":{"self":[{"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/phpstorm\/20747"}],"collection":[{"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/phpstorm"}],"about":[{"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/types\/phpstorm"}],"author":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/users\/745"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/comments?post=20747"}],"version-history":[{"count":1,"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/phpstorm\/20747\/revisions"}],"predecessor-version":[{"id":431965,"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/phpstorm\/20747\/revisions\/431965"}],"wp:attachment":[{"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/media?parent=20747"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/categories?post=20747"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/tags?post=20747"},{"taxonomy":"cross-post-tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/cross-post-tag?post=20747"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}