Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to ReFactor your Code using phpStorm

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you how to use phpStorm to reconstruct your code, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Refactoring code is an important aspect of building and maintaining software.

When refactoring code manually, whether you are using old code or creating new code, it is easy to make mistakes, such as where you forget to use the code when renaming the method. This is why I like to use PhpStorm's refactoring feature on a regular basis.

If this is the first time you've heard the term, Martin Fowler will describe refactoring as:

Refactoring is a controlled technology used to improve the design of existing code bases. Its essence is to apply a series of small behavior retention transformations, each of which is "too small to be worth doing". However the cumulative effect of each transformation is significant. By doing this step by step, you can reduce the risk of introducing errors. You can also avoid damaging the system during reorganization, which allows you to gradually ReFactor the system over a long period of time.

Refactoring covers a range of different techniques, including moving, extracting, copying, deleting, and renaming. These cover all the types of changes that you may constantly make to your code.

Happily, PhpStorm's refactoring capabilities (included in the core package) support all of these features. In this tutorial, I will introduce some of them step by step. In particular:

Extract the code to a new method

Rename function

Change the signature of the function

Extract the code into a new method

The new way to extract code is refactoring, and I do more work than most (maybe anything else). I can't count the number of times I've encountered long functions. I can break them down into a series of smaller, more reusable, more testable functions to better organize them.

Take the following function as an example.

Public function populate ($data) {if (is_array ($data) & & empty ($data)) {throw new HydrationException ();} $this- > id = $data ['id']; $this- > userId = $data [' userId']; $this- > entry = $data ['entry']; $this- > created = $data [' created']; $this- > updated = $data ['updated'];}

Although small, it is sufficient for this example. Assume that the last five elements of the function are needed by the rest of the class, or can be better used in a separate function.

What we need to do is extract them into a separate method. To do this, we first highlight the row to be extracted, and then click ReFactor-> extract-> method. This displays the extraction method dialog box, which you can see below, with a set of default options pre-populated.

The least we need to do is to fill in a name for the method. The remaining options can be retained because they are a good set of default values. However, another thing I did was to specify a type hint for the only parameter.

$data

In this way, the functionality is clearer, and the generated PhpDoc block will contain this information. Click ReFactor to either generate a new method or replace the highlighted code with a call to it, as you can see below.

Public function populate ($data) {if (is_array ($data) & & empty ($data)) {throw new HydrationException ();} $this- > hydrateMemberVariables ($data);} / /. Intervening codepublic function populate ($data) {if (is_array ($data) & & empty ($data)) {throw new HydrationException ();} $this- > hydrateMemberVariables ($data) } these are all the contents of the article "how to ReFactor your Code with phpStorm". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report