Home > PHP Framework > Laravel > Share a Laravel coding formatting tool: Laravel Pint

Share a Laravel coding formatting tool: Laravel Pint

青灯夜游
Release: 2022-10-26 19:26:07
forward
1466 people have browsed it

This article will introduce you to the official new project Share a Laravel coding formatting tool: Laravel Pint Pint, a coding formatting tool based on PHP-CS-Fixer specially optimized for Share a Laravel coding formatting tool: Laravel Pint. I hope it will be helpful to you!

Share a Laravel coding formatting tool: Laravel Pint

The long-awaited hype is finally over, Share a Laravel coding formatting tool: Laravel Pint’s latest open source CLI application has been released to the world, and we’ve started introducing it - Share a Laravel coding formatting tool: Laravel Pint Pint.

"Share a Laravel coding formatting tool: Laravel Pint pint is a zero-dependency PHP code style fixer built on PHP-CS-Fixer, suitable for minimalists.", the above is the original text of the readme.

As soon as I read this, I got excited, and I mean REALLY excited. In Modern PHP, we're always honing our craft, making our code more rigorous and better testable, and ensuring we have a consistent coding style. It all started when PHP-FIG was formed and they started publishing PSRs, and has grown as the framework has its own specific publishing style rules. This package is no different, it will automatically test and fix your coding style based on presets.

If you want to use this package, please install it with composer:

composer require laravel/pint --dev
Copy after login
Copy after login

After installation, it can run without configuration. You can run the following command:

./vendor/bin/pint
Copy after login
Copy after login
Copy after login

No configuration, no setup, no thought or effort - my project is instantly evaluated for PSR-12 style and automatically fixed. Isn't it amazing? Keeping your code clean and consistent sometimes requires some effort or thinking about how to set this up and what rules need to be implemented. Or using PHP CS Fixer, you have to create a configuration file and then create a finder to add the rules to an array of PHP files. Things get messy quickly, right?

If you create the pint.json file in the root directory of your project, it will simply load and read the configuration, otherwise, it will load and read the default values. The configuration is all formatted in JSON, making it easier to use and more readable.

Let's take a quick look at setting up a new pint configuration using presets. But let's look at doing this with a brand new Share a Laravel coding formatting tool: Laravel Pint project, so create a new project:

laravel new pint-demo
Copy after login

Open this new project in the terminal and install pint:

composer require laravel/pint --dev
Copy after login
Copy after login

Use us Brand new Share a Laravel coding formatting tool: Laravel Pint application, let's configure a preset for Share a Laravel coding formatting tool: Laravel Pint. Create a pint.json file and add:

{
    "preset": "laravel"
}
Copy after login

Now run pint:

./vendor/bin/pint
Copy after login
Copy after login
Copy after login

You should see output similar to the following:

Wuhu, take off?

So now let's change the preset to PSR-12 and check the output:

{
    "preset": "psr12"
}
Copy after login

Rerun pint, let's see the output:

./vendor/bin/pint
Copy after login
Copy after login
Copy after login

You should see the following output:

Let's check and see that it implements single_trait_insert_per_statement, braces and new_with_braces and other rules. What if we want to customize these a little bit? Simple, since we can easily define and delete rules, let's close the braces in the pint.json file:

{
    "preset": "psr12",
      "rules": {
        "braces": false
    }
}
Copy after login

That's it! It's easy to customize your code styles. No more messy PHP and arrays to configure your coding style. So let’s re-run pint, but this time we want to do a test run and see what changes:

./vendor/bin/pint —test
Copy after login

Share a Laravel coding formatting tool: Laravel Pint

Awesome, right? Nice clean output, give it a test run and see what fails and why! So what happens if you want more information? I've fixed the files above and gone back to user migrations as well as undoing the changes so we can test them. Let's try it again, but this time want to request verbose output using the -v flag:

We get nice output, just like using As with GitHub, changes can be made. So we can see the code style issue and what would happen if we fixed it - all from using one very simple command.

I don’t know about you, but this is my go-to software for all my upcoming projects, packages, and anything else I work on.

Original address: https://laravel-news.com/laravel-pint

Translation address: https://learnku.com/laravel/t/69321

[Related recommendations: laravel video tutorial]

The above is the detailed content of Share a Laravel coding formatting tool: Laravel Pint. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:learnku.com
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