How to refactor your code using phpStorm

(*-*)浩
Release: 2020-01-13 15:53:11
Original
4886 people have browsed it

How to refactor your code using phpStorm

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

When refactoring code by hand, whether you are using old code or creating new code, it is easy to make mistakes, such as forgetting to use code when renaming a method. This is why I like to use PhpStorm’s refactoring feature on a regular basis. (Recommended learning: phpstorm)

If this is the first time you have heard of the term, Martin Fowler describes refactoring as:

重构是一种用于改进现有代码库设计的受控技术。它的本质是应用一系列小的行为保留转换,每个转换“都太小,不值得做 ”。但是,每个转换的累积效果都非常显着。通过分步进行,可以降低引入错误的风险。您还可以避免在进行重组时损坏系统,这使您可以在较长的时间内逐步重构系统。
Copy after login

Refactoring covers a range of different techniques, including move, extract, copy, delete and rename. These cover all types of changes you might make to your code over time.

Happily, PhpStorm's refactoring feature (included in the core package) supports all of these features. In this tutorial I'll walk through some of them step by step. Special:

Extract code to new method

Rename function

Change signature of function

Extract code to new method Method

Extracting code into new methods is refactoring that I do more than most (and maybe anything else). I can't count the number of times I've encountered long functions that could be better organized by breaking them down into a series of smaller, more reusable, more testable functions.

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'];
}
Copy after login

Although small, it is sufficient for this example. Let's say the five lines at the end of the function are needed by other parts of the class or can be better used in a separate function.

What we want to do is extract them into a separate method. To do this, we first highlight the row we want to extract and click "Refactor->Extract->Method". This will display the Extraction Method dialog box, which you can see below, pre-populated with a set of default options.

How to refactor your code using phpStormThe least we have to do is fill in a name for the method. The remaining options can be left as they are a good set of defaults. However, one more thing I did was specify a type hint for the only parameter

$data
Copy after login

This way the functionality is clearer and the generated PhpDoc block will also contain that information. Clicking Refactor will both generate a new method and replace the highlighted code with a call to it, which you can see below.

public function populate($data)
{
    if (is_array($data) && empty($data)) {
        throw new HydrationException();
    }
    $this->hydrateMemberVariables($data);
}
///...intervening code
public function populate($data)
{
    if (is_array($data) && empty($data)) {
        throw new HydrationException();
    }
    $this->hydrateMemberVariables($data);
}
Copy after login

The above is the detailed content of How to refactor your code using phpStorm. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!