{"id":20610,"date":"2018-04-03T15:14:29","date_gmt":"2018-04-03T15:14:29","guid":{"rendered":"https:\/\/blog.jetbrains.com\/phpstorm\/?p=12726"},"modified":"2024-01-09T14:51:53","modified_gmt":"2024-01-09T13:51:53","slug":"inline-method-refactoring","status":"publish","type":"phpstorm","link":"https:\/\/blog.jetbrains.com\/phpstorm\/2018\/04\/inline-method-refactoring\/","title":{"rendered":"Inline method refactoring"},"content":{"rendered":"<p>In PhpStorm, you can use the <em>Extract method<\/em> refactoring to transform a group of statements into a method. Surprisingly enough, doing the opposite has not been possible up until recently. In PhpStorm 2018.1, we are introducing the <em>Inline method<\/em> refactoring.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-12807\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2018\/04\/phpstorm-inline-method-splash.png\" alt=\"inline-method-splash\" width=\"597\" height=\"308\" \/><\/p>\n<p><!--more--><\/p>\n<p>Sometimes using a method\u2019s body in your code turns out to be more obvious than using the method itself. The good solution for the situation is to replace the calls to the method with the actual method\u2019s content.<\/p>\n<p>The <em>Inline method<\/em> refactoring will assist you with that: simply invoke <em>Refactor | Inline<\/em> on a function or a static method call,\u00a0and automatically inline the code from the called function.\u00a0You can optionally delete the inlined function or method if there are no more usages:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2018\/04\/phpstorm-inline-basic.png\" alt=\"Run suite\" width=\"852\" data-gif-src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2018\/03\/phpstorm-inline-basic.gif\" \/><\/p>\n<p>If the refactoring is possible, PhpStorm will perform all the necessary transformations and reformat the resulting code.<\/p>\n<h2>Examples<\/h2>\n<p>It\u2019s not always as simple as it sounds though: in some cases, inlining a method can be quite tricky, and we\u2019ve done our best to make sure such cases are covered properly.<\/p>\n<p>For example, PhpStorm will handle functions with multiple calls as well as make use of the parameters\u2019 default values:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2018\/04\/phpstorm-inline-multiple-default.png\" alt=\"Inline multiple returns\" width=\"852\" data-gif-src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2018\/03\/phpstorm-inline-multiple-default.gif\" \/><\/p>\n<p>Functions with multiple return points are supported, as well:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2018\/04\/phpstorm-inline-multiple-returns.png\" alt=\"Inline multiple returns\" width=\"852\" data-gif-src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2018\/03\/phpstorm-inline-multiple-returns.gif\" \/><\/p>\n<p>For class methods, PhpStorm will auto-import classes and properly handle <em>self<\/em> to resolve a method within the classes hierarchy:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2018\/04\/phpstorm-inline-class-namespace.png\" alt=\"Inline multiple returns\" width=\"850\" data-gif-src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2018\/03\/phpstorm-inline-class-namespace.gif\" \/><\/p>\n<p>For now, PhpStorm only allows you to inline static class methods. As a workaround, you can use two refactorings on a desired method, <em>Make method static<\/em> and <em>Inline<\/em>, in sequence. In the future, we plan to support the inlining of non-static methods, too. And since this feature is under active development, feel free to report other inlining cases that don\u2019t work as you expect, or are simply not covered by the refactoring.<\/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\/fr\/wp-json\/wp\/v2\/phpstorm\/20610"}],"collection":[{"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/phpstorm"}],"about":[{"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/types\/phpstorm"}],"author":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/users\/745"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/comments?post=20610"}],"version-history":[{"count":1,"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/phpstorm\/20610\/revisions"}],"predecessor-version":[{"id":431976,"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/phpstorm\/20610\/revisions\/431976"}],"wp:attachment":[{"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/media?parent=20610"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/categories?post=20610"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/tags?post=20610"},{"taxonomy":"cross-post-tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/cross-post-tag?post=20610"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}