Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
December 1, 2021 12:57 pm GMT

PHP Code Style

Few ways you can simplify, how you should standardise your PHP code style, by following PSR-12.

For start, you need to use PHP CS Fixer to automate fixing the code style. Do install it and try it out.

Few options available for you to use - details as following.

Option 1

You can create your own bash script, to simplify your command to fix your code. One of my choice, is using a Bash script:

#!/usr/bin/env bashCSFIX="php-cs-fixer"if [ ! -f "$CSFIX" ]; then    if ! type "php-cs-fixer" > /dev/null; then      clear      echo " You need PHP CS Fixer to run this command."       return    fi    fi$CSFIX fixif [[ `git status --porcelain` ]]; then  git add .  git commit -m "Apply PHP CS Fixer"  MESSAGE=" Successfully complied with PSR-2"else  MESSAGE=" You already complied with PSR-2"fiecho $MESSAGE

Make sure to make above script executable - chmod +x csfix, then you can run on your current project.

. ./csfix

I like this way, cause you can put some fancy words, and messages on each scenario when running the PHP CS Fixer.

Option 2

Your team may use different operating system - MacOS, Windows, Ubuntu - and above option may not very friendly to Windows. You may go with this option - put it on Composer script command.

In the scripts section in your composer.json file, add the following:

{    "scripts": {        "csfix": [            "PHP_CS_FIXER_IGNORE_ENV=1 vendor/bin/php-cs-fixer fix",            "git add . && git commit -m ' Apply PHP CS Fixer'"        ]    }}

Use the PHP_CS_FIXER_IGNORE_ENV=1 if you are running on PHP8.1. At the moment, still unable to run PHP CS Fixer on PHP8.1.

With above setup, you can run:

composer csfix> PHP_CS_FIXER_IGNORE_ENV=1 vendor/bin/php-cs-fixer fixPHP needs to be a minimum version of PHP 7.2.5 and maximum version of PHP 8.0.*.Current PHP version: 8.1.0.Ignoring environment requirements because `PHP_CS_FIXER_IGNORE_ENV` is set. Execution may be unstable.Loaded config default from "/Users/nasrulhazim/Projects/2021/demo/.php-cs-fixer.php".Using cache file ".php-cs-fixer.cache".   1) database/seeders/Mock/DemoSeeder.phpFixed all files in 0.021 seconds, 18.000 MB memory used> git add . && git commit -m ' Apply PHP CS Fixer'[develop 254c4d7]  Apply PHP CS Fixer 1 file changed, 1 insertion(+), 1 deletion(-)

Noticed that I use the git as well - am too lazy to check if there's changes or not, just commit it right away.

So, with this approach, all your team members can have the same command to be use - composer csfix.

Option 3

If you are looking at more, automated process - you can go with StyleCI, or even have your own Github Action / Bitbucket Pipeline to do all the job.

For Github Action, you may refer to this example, how it can be done or you can refer to the following snippet:

name: Check & fix stylingon: [push]jobs:  php-cs-fixer:    runs-on: ubuntu-latest    steps:      - name: Checkout code        uses: actions/checkout@v2        with:          ref: ${{ github.head_ref }}      - name: Run PHP CS Fixer        uses: docker://oskarstark/php-cs-fixer-ga        with:          args: --config=.php_cs.dist.php --allow-risky=yes      - name: Commit changes        uses: stefanzweifel/git-auto-commit-action@v4        with:          commit_message: Fix styling

Hopes the options given will help you on make your project much cleaner and standard across your team practices.

Photo by Eugen Str on Unsplash


Original Link: https://dev.to/nasrulhazim/php-code-style-3go8

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To