Is there a code style enforcer?

2019-04-07 16:32发布

I'm working with websites written in PHP, along with many other programmers, and sometimes I have to deal with really awful code.

Indentation totally messed up, curly braces in the wrong places, terrible whitespace use, it really hurts my eyes and actually makes my work harder and take longer.

Is there a tool where you can specify your coding style and then it applies it to files? I'm sure there must be such functions in different IDEs, but I need an independent tool as I code with simple notepads like Notepad++ or the like. ( if it's such a function in Notepad++, I didn't see it )

6条回答
三岁会撩人
2楼-- · 2019-04-07 16:59

See the SD PHP Formatter for a tool that prettyprints source files, one at a time, or in large batches, according to your preferred style.

查看更多
Emotional °昔
3楼-- · 2019-04-07 17:00

Try PHP Code Sniffer.

That should be exactly what you are looking for(at least that's what I thought afert reading the heading, see the update).

UPDATE: Actually what Code Sniffer does is go through your code and detect coding style violations, it does not however fix the problems.

But by telling you what's wrong it enforces you to adhere to a certain coding style. So that might be an option for you in the future.

查看更多
淡お忘
4楼-- · 2019-04-07 17:00

Well for a quick solution Aptana IDE does support a code format option

查看更多
欢心
5楼-- · 2019-04-07 17:07

From what I can find Notepad++ doesn't even do basic linting (syntax checking) so it won't be able to do code sniffing: http://sourceforge.net/p/notepad-plus/discussion/482781/thread/b802c218:

Can anyone write a PHP syntax checker plugin?

...

What's wrong with PHP itself? If you run PHP as a command line utility with -l (letter L), it will run as PHP lint and check for syntax errors. e.g.

php -l broken.php

Hook it up as an external tool, perhaps through a batch file.

But you can get Code Sniffers for other text editors e.g. Sublime Text, Atom.io.

查看更多
Explosion°爆炸
6楼-- · 2019-04-07 17:18

Eclipse PDT also can format your code. Not sure how configurable it is. Just hit CTRL-Shift-F.

查看更多
太酷不给撩
7楼-- · 2019-04-07 17:19

While I have never used any, as I (luckly?) don't use anyone else code much, google found this, PHP Beautifier.

EDIT
See this question which recommends PHP Code Beautifier which is free (but requires an account creation)

查看更多
登录 后发表回答