Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

amit028's avatar

Auto format all files using phpcs

Hello All,

I wanted to format all my contoller's code with a single phpcs command. But i'm not getting any suitable code/command to do it. I have one command which is used to format single file's code -

phpcbf --standard=PSR2 App/Providers/HelperServiceProvider.php

Does anybody has any idea about it?

Any help would be highly appreciated.

0 likes
12 replies
s4muel's avatar
s4muel
Best Answer
Level 50

are all of your controllers in one directory? if so,

phpcs --standard=PSR2 app/Http/Controllers

where app/Http/Controllers is path to your controllers

or if you are on linux, combining the find tool with execution of the phpcs command could be a way, possibly:

find  . -name "*Controller.php" -exec phpcs --standard=PSR2 {} \; 

run in your project root

1 like
laracoft's avatar

@automica

I wanted to format all files on command and also use the same as VSCode as I auto format on save.

Anyway I checked, VSCode uses php-cs too, so I installed it in my CLI. Testing now.

automica's avatar

@amit028 whilst I am a fan of clean code, I think relying on automatically linting promotes bad coding practice.

I use phpstorm and use the CTRL+ALT+L keyboard shortcut to code format, but wouldn't set it as automatic.

You need learn what good code looks like and then embed it into your muscle memory so you get into the habit of doing it.

A precommit hook to prevent commit if code isn't PSR-* would be more helpful IMHO as you'd then learn without the security of the linter.

1 like
automica's avatar

@laracoft because you can rely on it.

its a bit like not bothering to clean up your house as you have a cleaner to come in and do it.

you need to learn to not be messy.

laracoft's avatar

@automica sorry, i'm not seeing the relation here..

I mean... doing it is tedious

and not doing it != not knowing how it should look, right?

automica's avatar

@laracoft its not tedious writing PSR compliant code. If you have your IDE set up correctly, you'll get most of it for free as you write it.

Its not like its complicated or you got to go far to learn it.

If you code untidy, that's a sign of being muddled, and in amongst the muddle you'll find mistakes. Poorly named objects and classes, irregular indentation - all add to the confusion.

I'm in a job where I need PR before my code goes out and that has raised my code quality hugely. I realise not everyone doesn't have that but it doesn't stop me formatting my code for my personal projects.

laracoft's avatar

@automica ok, maybe let me rephrase..

Getting perfect can be tedious, I think my code without auto format is acceptably neat. :) I mean, I take the effort to format my markdown in most of my posts here on Laracast.

I need code to be perfect or should I say, absolutely consistent so that I don't get unnecessary Git whitespace changes.

automica's avatar

@laracoft I'm not objecting to linting and I definitely agree there is benefits to it. I'm just saying you should manually run the linter and that will help you learn what has been changed so you can get better at it next time.

It also allows you to make exceptions. For example, whilst I am a fan of phpdoc and return types, not everyone is. Sometimes its cleaner to do without them.

I guess anyone who is bothering to set up code sniffing and linting is already on board with the benefits of clean code, so we're preaching to the converted.

1 like

Please or to participate in this conversation.