{"id":21104,"date":"2019-04-26T08:05:35","date_gmt":"2019-04-26T08:05:35","guid":{"rendered":"https:\/\/blog.jetbrains.com\/phpstorm\/?p=14402"},"modified":"2024-01-09T14:51:08","modified_gmt":"2024-01-09T13:51:08","slug":"reworked-imports-code-cleanup","status":"publish","type":"phpstorm","link":"https:\/\/blog.jetbrains.com\/phpstorm\/2019\/04\/reworked-imports-code-cleanup\/","title":{"rendered":"Reworked Imports &#038; Code Cleanup"},"content":{"rendered":"<p>In 2019.1 release, we\u2019ve reworked the inspections and intention actions related to importing namespaces and using FQN. We\u2019ve also added some PHP-specific intentions to the <em>Code Cleanup<\/em> tool, which will help you automatically run safe transformations on the whole project or a part of it.<\/p>\n<p><!--more--><\/p>\n<h2 id=\"reworked_imports\">Reworked Imports<\/h2>\n<p>We\u2019ve reworked the inspections and intention actions related to importing namespaces and using FQN. The main idea behind them is to avoid qualifiers as much as possible.<\/p>\n<p>To get started, make sure that the inspections <em>Fully Qualified name usage<\/em> and <em>Unnecessary fully qualified name <\/em>are enabled under <em>Preferences | Editor | Inspections | PHP | Code Style<\/em>.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2019\/04\/phpstorm-fqn_usage_inspections_settings.png\" alt=\"fqn_usage_inspections_settings\" width=\"800\" class=\"aligncenter size-full wp-image-14406\" \/><\/p>\n<p>In the <em>Options<\/em> section for each of these inspections, you can additionally choose whether they should apply to files not bound to any specific namespace, and whether the entities from the global namespace should be subject to imports optimization or not. In most cases, however, you\u2019re good to go with the above default values.<\/p>\n<p>Now, PhpStorm will now let you simply remove a redundant qualifier if possible:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2019\/04\/phpstorm-remove_fqn.png\" data-gif-src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2019\/04\/phpstorm-remove_fqn.gif\" alt=\"remove_fqn\" width=\"800\" class=\"aligncenter size-full wp-image-14407\" \/><\/p>\n<p>Otherwise, it will let you replace it with a <em>use<\/em> import statement. <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2019\/04\/phpstorm-replace_fqn_import.png\" data-gif-src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2019\/04\/phpstorm-replace_fqn_import.gif\" alt=\"replace_fqn_import\" width=\"800\" class=\"aligncenter size-full wp-image-14408\" \/><\/p>\n<p>Also, when you have a class imported as an alias, PhpStorm will prompt you to reuse this alias when you paste some code into a file.<\/p>\n<p><img decoding=\"async\" data-gif-src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2019\/04\/phpstorm-replace_with_alias.gif\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2019\/04\/phpstorm-replace_with_alias.png\" alt=\"replace_with_alias\" width=\"800\" class=\"aligncenter size-full wp-image-14409\" \/><\/p>\n<h2 id=\"sort_use_statements\">Sort use statements<\/h2>\n<p>If you have multiple use statements in a file, you can now automatically sort them either alphabetically or by length. Go to the <em>Code Conversion<\/em> tab under <em>Preferences | Editor | Code Style | PHP<\/em> and choose the sorting type.<\/p>\n<p>Having done that, use <em>Code | Optimize Imports<\/em> to sort your statements.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2019\/04\/phpstorm-optimize_imports_sort.png\" data-gif-src=\"https:\/\/i.imgur.com\/jmW2Wj0.gif\" alt=\"optimize_imports_sort\" width=\"800\" class=\"aligncenter size-full\" \/><\/p>\n<h2 id=\"code_cleanup\">Code Cleanup<\/h2>\n<p>Code Cleanup is a batch action that lets you run a number of safe transformations on the whole project or a part of it. Earlier, this tool included only JavaScript inspections inherited from WebStorm. In PhpStorm 2019.1, we are bringing PHP-specific intentions to <em>Code Cleanup<\/em>. <\/p>\n<p>You can run the cleanup tasks at any time by selecting <em>Code | Code Cleanup\u2026<\/em> from the main menu. <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2019\/04\/phpstorm-code_cleanup_inspections.png\" alt=\"code_cleanup_inspections\" width=\"800\" class=\"aligncenter size-full wp-image-14405\" \/><\/p>\n<p>Under <em>Code Style<\/em>, you\u2019ll find two inspections fixing the redundant usages of fully qualified class names. PhpStorm will get rid of them by either adding a use statement or just removing the extra namespace part.<\/p>\n<p>The <em>Quality Tools<\/em> inspections provide support for two popular fixers: <em>PHP CS Fixer<\/em> and <em>PHP_CodeSniffer\u2019s phpcbf<\/em>, which you can run to fix the code style automatically.<\/p>\n<p>What\u2019s more, <em>Code Cleanup<\/em> can run automatically on the files selected for commit to VCS. To opt in, simply select the<em> <\/em><em>Cleanup<\/em> checkbox in the<em> Commit Changes <\/em>dialog.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2019\/04\/phpstorm-cleanup_commit_dialog.png\" alt=\"cleanup_commit_dialog\" width=\"800\" class=\"aligncenter size-full wp-image-14404\" \/><\/p>\n<p><em>Your JetBrains PhpStorm Team<\/em><br \/>\n<em>The Drive to Develop<\/em><\/p>\n","protected":false},"author":869,"featured_media":21105,"comment_status":"open","ping_status":"open","template":"","categories":[808,907],"tags":[744,2015,806,2630,2629,2533],"cross-post-tag":[],"acf":[],"_links":{"self":[{"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/phpstorm\/21104"}],"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\/869"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/comments?post=21104"}],"version-history":[{"count":1,"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/phpstorm\/21104\/revisions"}],"predecessor-version":[{"id":431941,"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/phpstorm\/21104\/revisions\/431941"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/media\/21105"}],"wp:attachment":[{"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/media?parent=21104"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/categories?post=21104"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/tags?post=21104"},{"taxonomy":"cross-post-tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/cross-post-tag?post=21104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}