How to standardize the formatting of javascript and php codes?

Asked

Viewed 444 times

1

I have some time already in languages, but whenever I come across other codes or I’m in a different job, each one has a different way to standardize.

Is there any recognized standard, type W3C, or any tool that validates the code?

3 answers

1

Yes. There are several standards, including online tools that "beautify" your code according to some existing standard.

http://beta.phpformatter.com

http://jsbeautifier.org

There’s also Php_codesniffer, an integral Pear package, that you can embed into your favorite IDE.

In my case, I use it to validate collaborators' PHP code before accepting a pull request in the versioning tools. Whenever a PHP file is saved, my IDE (Komodo Edit) automatically calls the command:

/usr/lib/php/pear/phpcs %F --standard=phpcs

Example of part of a result without validation:


FILE: phpexample.php

FOUND 908 ERROR(S) AND 3 WARNING(S) AFFECTING 238 LINE(S)

102 | ERROR | The variable Names for Parameters $get (3) and $main (4) do | | not align

102 | ERROR | The comments for Parameters $get (3) and $main (4) do not | | align

199 | ERROR | Spaces must be used to indent Lines; tabs are not allowed

199 | ERROR | Line indented incorrectly; expected at least 4 Spaces, found | | 1


And you choose between various formats.

0

  • 3

    thanks. I looked at this and the others, but it seems that nobody likes tabs, except us Brazilians

0

For php there are some recommended proposals that are:

PSR-0, Regulates the definition of namespaces, so that the way to load (autoload) classes is an independent standard of the framework/lib used, because today some frameworks use own mechanisms, that is to say if you only need one component you are almost obliged to use the entire framework because of the dependencies or autoload itself.

PSR-1, Qugere a standardization, of what kind of code some files should have and a bit of codestand.

PSR-2, Basically suggests codestand

There are some tools like the codesniffer which serves to show shortcomings in the code for example duplicated code or wrong tab, this is done through user-defined rules, here is a article for more information.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.