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 )
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.
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)
Eclipse PDT also can format your code. Not sure how configurable it is. Just hit CTRL-Shift-F.
Well for a quick solution Aptana IDE does support a code format option
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.
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.
Related
I searched around the webs and found tools that can indent java,C,php,HTML etc.
Is there a tool, that can reliably indent mixed sourcecode and different languages?
-> one indent tool to rule them all?
I am looking for a standalone opensource program or even a NetBeans plugin (but there seems to be only jindent).
The indentation rules for different languages are different. If there were a "universal indenter", it would first determine in which language the text was written, and then invoke what is essentially a language-specific indentation subroutine.
There are packages for LaTeX and the like which can indent more than one language, but you tell them which language they're dealing with...
Do you have a particular task you wish to accomplish?
I ended up using NetBeans' own indention functionality ( [SHIFT]+[ctrl]+F - on MacOS ).
It works quite well, though I have to indent by hand sometimes.
For really bad structured code I use the common tools.
SourceFormat claims to be a multi-language pretty printer. But it isn't free and only runs on windows.
WebStorm and IDEA (both from Jetbrains) can reformat mixed source, but they aren't open source. (via T.J. Crowder)
When using emacs and writing PHP I sometimes need to put html in there and I would like to see a difference in highlighting from PHP to HTML. Also the same goes for if I am putting JavaScript in say an HTML file, how can I get the syntax highlighting to change?
nXhtml is the most comprehensive attempt to deal with this problem (specifically for web development) that I'm aware of.
I currently use notepad++ on windows or gedit on ubuntu. Both of them work great with code highlighting and hinting etc. But both of them suffer from a huge flaw. I am yet to find a code editor that can handle this concept:
<?php
// ooh, look I am doing some php
?><a onclick="alert('hay, some javascript in here now!')">
This link is HTML?!</a>
<?PHP
echo("NOW we have some php as well!");
?>
At the moment, I just have to settle for the one language. I want something that can think of a that text as a default as HTML, but notice when sections are PHP. I want those sections of PHP to have there own code hinting and highlighting. Even more, lets say in an 'if else' I exit PHP, write some HTML then back into PHP, I want it to work out how the braces ( '{' and '}' ) should match up and let me know if I have missed one. I want the sections of in-line JavaScript to be picked up as such. I want all of these languages to get checked for syntax!
Damn it, I want to tool that understands more than one language at once!
Extra
Should point out that I am not willing to pay for such luxury :P
My files are saved as '.php'.
Notepad++ is able to to work out that I am using PHP but when I drop out of php and do some HTML and/or some JavaScript.
PHPStorm is probably the most powerful IDE for your requirements. Pricey, but give it a go. I think it's worth it...
Try Eclipse with PDT or IDEs based on Eclipse like Aptana or Zend Studio.
Or try Netbeans. Should all be able to do what you want.
In case you cannot decide which to use, see the various Q&A's on that topic
https://stackoverflow.com/search?q=which+IDE+php
Emacs, with the nxhtml mode.
gedit, being based on gtksourceview, absolutely does support nested language blocks. If I save your example as .php, I get syntax highlighting for both PHP and HTML.
And indeed for JavaScript in <script> blocks, though not inline event handler attributes. (Which would be tricky because those aren't CDATA, they're HTML: the syntax highlighter would have to know what if (a&&b) represented. Anyway, you don't want to be using event handler attributes.)
I'm looking so long for a Dreamweaver extension to auto beautify PHP / JavaScript / jQuery code. Currently Dreamweaver can beautify only HTML and CSS (Apply Source Formatting).
Any kind of help will be much appreciated.
Online solutions for validation and formatting JavaScript:
Validate JavaScript code:
http://www.javascriptlint.com/
Format JavaScript code:
http://jsbeautifier.org/
For the PHP side of things use https://github.com/fabpot/PHP-CS-Fixer
This short excerpt and more information is taken from the README.
Installation:
Download the php-cs-fixer.phar file and store it somewhere on your
computer.
Usage:
The fix command tries to fix as much coding standards problems as
possible on a given file or directory:
php php-cs-fixer.phar fix /path/to/dir
php php-cs-fixer.phar fix /path/to/file
I have found this great dreamweaver extenstion, just give it a try
Their Homepage: http://www.decodize.com/projects/
extension download page: http://www.adobe.com/cfusion/exchange/index.cfm?event=extensionDetail&loc=en_us&extid=2107536
You can use PhpEdit, it's an extra debugger!
DW doesn't do debug, you find the error if and only if you run your code.
With PhpEdit, it is onTime.
Maybe try http://www.javascriptlint.com/
says it works on some IDEs, maybe DW is one of them.
Try googling, but use 'code format' not 'beautify'
I was wondering if you could elaborate on how to set this up. I see the CSS and Tag Libraries options under "Advanced Formatting", but I am stuck at that point as to how to get my PHP to start auto-indenting.
Or, if Dreamweaver has somehow fixed this problem, let me know! It seems like every other IDE has an auto-indent feature! Thanks :)
It's been a while since I have used DW, I use Aptana now and there is an option to format the code from one of the drop down menus. Just use something like "control a" (To select all your code), "control f" (To format your code). There might be another key combination but I think this will work if memory serves.
Maybe "esc control f"
I've not seen anything for Dreamweaver, but if you can copy ans paste the code, http://jsbeautifier.org/ can help you with making code look a good bit better. I've just tried it on some packed JavaScript and it worked out very well. You can also download the application and run it locally (it's only HTML and JavaScript), so you won't need an internet connection to get your code prettier.
Best way i found is to set the way Dreamweaver displays code yourself. Sure it takes some time to do but its worth it.
Here is how:
Dreamweaver: Edit > Preferences > Code Format
Then under Advanced Formatting you can pick CSS... or Tag Libraries...
You can make any of your code look as it should this way.
Like i said, will take some time but do it once and you can keep using it with all your future Dreamweaver versions.
In Dreamweaver CS6 no need any other extension or tools.
Just select your code and then Go to 'Commands' > 'Apply Source Formatting' or 'Apply Source Formatting to Selection'
Done. :)
I am going to start working on a website that has already been built by someone else.
The main script was bought and then adjusted by the lead programmer. The lead has left and I am the only programmer.
Never met the lead and there are no papers, documentation or comments in the code to help me out, also there are many functions with single letter names. There are also parts of the code that are all compressed in one line (like where there should be 200 lines there is one).
There are a few hundred files.
My questions are:
Does anyone have any advice on how to understand this system?
Has anyone had any similar experiences?
Does anyone have a quick way of decompressing the lines?
Please help me out here. This is my first big break and I really want this to work out well.
Thanks
EDIT:
On regards to the question:
- Does anyone have a quick way of decompressing the lines?
I just used notepad++ (extended replace) and netbeans (the format option) to change a file from 1696 lines to 5584!!
This is going to be a loooonnngggg project
For reformatting the source, try this online pretty-printer: http://www.prettyprinter.de/
For understanding the HTML and CSS, use Firebug.
For understanding the PHP code, step through it in a debugger. (I can't personally recommend a PHP debugger, but I've heard good things about Komodo.)
Start by checking the whole thing into source control, if you haven't already, and then as you work out what the various functions and variables do, rename them to something sensible and check in your changes.
If you can cobble together some rough regression tests (eg. with Selenium) before you start then you can be reasonably sure you aren't breaking anything as you go.
Ouch! I feel your pain!
A few things to get started:
If you're not using source control, don't do anything else until you get that set up. As you hack away at the files, you need to be able to revert to previous, presumably-working versions. Which source-control system you use isn't as important as using one. Subversion is easy and widely used.
Get an editor with a good PHP syntax highlighter and code folder. Which one is largely down to platform and personal taste; I like JEdit and Notepad++. These will help you navigate the code within a page. JEdit's folder is the best around. Notepad++ has a cool feature that when you highlight a word it highlights the other occurrences in the same file, so you can easily see e.g. where a tag begins, or where a variable is used.
Unwind those long lines by search-and-replace ';' with ';\n' -- at least you'll get every statement on a line of its own. The pretty-printer mentioned above will do the same plus indent. But I find that going in and indenting the code manually is a nice way to start to get familiar with it.
Analyze the website's major use cases and trace each one. If you're a front-end guy, this might be easier if you start from the front-end and work your way back to the DB; if you're a back-end guy, start with the DB and see what talks to it, and then how that's used to render pages -- either way works. Use FireBug in Firefox to inspect e.g. forms to see what names the fields take and what page they post to. Look at the PHP page to see what happens next. Use some echo() statements to print out the values of variables at various places. Finally, crack open the DB and get familiar with its schema.
Lather, rinse, repeat.
Good luck!
Could you get a copy of the original script version which was bought? It might be that that is documented. You could then use a comparison tool like Beyond Compare in order to extract any modifications that have been made.
If the functions names are only one letter it could be that the code is encoded with some kind of tool (I think Zend had a tool like that - Zend Encoder?) so that people cannot copy it. You should try to find an unencoded version, if there is one because that would save a lot of time.