I'm working on a project(PHP) and on every commit there are some breaks on code convention. I'm using git for version control. Is there a way for automated code formatting so that all the code stays clean?
VS Code Auto Format On SaveOpen Visual Studio Code editor. Click the “Settings” gear icon in the bottom-left corner. Search “Formatter” and click the “Editor: Default Formatter” option. From the drop-down menu, select whichever code formatter you want to use.
Auto formatting settings in Visual Studio Show activity on this post. Select the text you want to automatically indent. Click menu Edit → Advanced → *Format Selection, or press Ctrl + K , Ctrl + F . Format Selection applies the smart indenting rules for the language in which you are programming to the selected text.
Install the Flutter extension (see Editor setup) to get automatic formatting of code in VS Code. To automatically format the code in the current source code window, right-click in the code window and select Format Document . You can add a keyboard shortcut to this VS Code Preferences.
There are two parts to the question: automatically formatting the code, and detecting when it doesn't conform to your coding standards.
Automatically formatting the code is not really something you want to put inbetween you and your repo directly. Modifying files, or attempting to modify files, in a pre-commit
hook is likely to make a mess. As such it doesn't matter what vcs you are using.
Using a tool to format the code via your editor or as a process you run (manually, or semi-automated) as part of your development workflow would be appropriate. For example, vim has the =
function to auto-indent code, and as mentioned by others Pear's beautifier is one possibility to do this.
Detecting code standard devitions requires a cli tool that tells you when a file does not conform to coding standards - the obvious choice is PHP Code Sniffer (phpcs) - though it could simply be the same tool you use to beautify your code manually (if you use one) and checking that it doesn't change the file contents.
You may need to write your own standard to use with phpcs if none of the existing standards match your style.
You can use a pre-commit hook to trigger a check on the code right before you commit it - If there are code errors found, you'll be notified about them and the commit aborted. You can bypass your pre-commit hooks using git commit --no-verify
You may find this repo useful: https://github.com/AD7six/git-hooks
Example:
$ more foo.php
<?php
function bar() {
}
$ git add foo.php
$ git commit -v
running php/lint.php ... OK
running php/phpcs.php ... FAIL
phpcs -n -s --extensions=php,ctp --encoding=UTF-8 --standard=Cake '/tmp/cakephp-git-hooks'
FILE: foo.php
---------------------------------------
FOUND 3 ERROR(S) AFFECTING 2 LINE(S)
---------------------------------------
2 | ERROR | Space indented: Tabs for indents, spaces for alignment (Cake.WhiteSpace.ForceTabIndent)
2 | ERROR | Line indented incorrectly; expected 0 spaces, found 4 (Cake.WhiteSpace.ScopeIndent.Incorrect)
3 | ERROR | Space indented: Tabs for indents, spaces for alignment (Cake.WhiteSpace.ForceTabIndent)
---------------------------------------
Time: 0 seconds, Memory: 3.75Mb
$
(commit aborted, code does not meet code standards)
$ git commit -v --no-verify -m "dummy commit"
running misc/happy-commits ... OK
[2.1 2c432f1] dummy commit
1 files changed, 3 insertions(+), 0 deletions(-)
create mode 100644 foo.php
$
(commit succeeded - even though code standards were not met)
Git knows hooks. You could leverage them to have some form of checkstyle application running before each commit is accepted into the repository. Check the files in .git/hooks/
within your repository. It's possible to refuse a commit this way. I'm not sure if you can modify the commit though.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With