cmrcx / phptidy Goto Github PK
View Code? Open in Web Editor NEWThis is a little tool for formatting PHP code.
License: GNU General Public License v2.0
This is a little tool for formatting PHP code.
License: GNU General Public License v2.0
====== phptidy README ====== === Description === This is a little tool for formatting PHP code. It aims to make the code better readable and thus better understandable. Unlike other beautifiers, phptidy does not completely reformat the code and remove formatting, which was intended by the author. It only touches things which are usually not intended. In case of doubt, phptidy will not touch anything. Because of this strategy it is also very unlikely, that phptidy by fault changes the functionality of the code. phptidy is useful in many situations: - You can use it on your own code during development to keep the code tidy, wasting no time on indenting and formatting by hand. - In a project with more than one person coding you have less trouble with people who do not confirm to the style guide. - If you use a version control system you can require all participating people to run phptidy before committing changes. Then you won't have anymore trouble with unintended changes due to code formatting or with the trailing whitespace or wrong line breaks of some brain dead editors (or their users). - If you have to maintain some ultimately ugly code of someone else. After running phptidy the code will be much better readable. Well, the code will not be perfect, but it's a big help anyway. The used coding standard is mainly inspired by the PEAR Coding Standards: http://pear.php.net/manual/en/standards.php phptidy is released under the GPL and can be used for free. === System Requirements === - PHP 5 or 7 phptidy does not work on PHP 4, because some functions introduced in PHP 5 are used, and there are also some differences in the tokens. But it should be not too difficult to port it to PHP 4. - PHP CLI SAPI http://php.net/manual/en/features.commandline.php - PHP extension: Tokenizer http://php.net/manual/en/book.tokenizer.php Optional: - diff The standard Unix diff utility or similar Default is to use colordiff. Only required if you want to use phptidy's "diff" command. - PHP extension: mbstring http://php.net/manual/en/book.tokenizer.php Only required if you want to use the configration variable $encoding to check the encoding of your PHP files. phptidy should work on most operation systems where PHP works. I use it in Linux and MacOS X. Some people use it also on Windows. === Installation === phptidy consists of only one file, phptidy.php. Execute this file on command line to use it. $ ./phptidy.php There is actually no need to install phptidy, you can run it also with the path to the file on command line. $ ~/src/phptidy/phptidy.php To be able to call phptidy without having to supply the path, you can copy phptidy.php to a directory in your PATH, e.g. ~/bin (only for you) $ cp phptidy.php ~/bin or /usr/local/bin (for all users). # cp phptidy.php /usr/local/bin In the following examples I assume you have installed phptidy in your PATH. === Basic usage === The most simple case: You have a PHP script example.php and you want to tidy it up. $ phptidy.php suffix example.php phptidy will create a file "example.php.phptidy.php" with the tidy version of your script. To see what changes phptidy would make to your script, you can use phptidy's "diff" command: $ phptidy.php diff example.php If you are happy with the displayed changes, you can now overwrite the original file with the tidy version by using the "replace" command: $ phptidy.php replace example.php You will find a backup of the original version of the file in the hidden file ".example.php.phptidybak~". If you have a project consisting of multiple files, you can supply all these files as arguments: $ phptidy.php replace *.php inc/*.php === Using config files === If you have a project with many files, you can write the list of files in a config file. Then you do not have to supply the list on the command line anymore. The config file has to be named ".phptidy-config.php" and has to be in the project's top level directory. To have the same effect as in the last example the config file would have to have the following content: <?php $project_files = array("*.php", "inc/*.php"); Then you can call phptidy without the files list: $ phptidy.php replace Please notice that when using config files, you always have to call phptidy from the project's top level directory where also the config file resides. Otherwise phptidy won't be able to find the config file. With a config file you can also adjust phptidy's default settings to the specific requirements of your project. See the phptidy source comments for a list of available variables and their default settings. === Perspective === Don't hesitate to write me about your experiences with phptidy. Write me which features you miss, what did not work for you, or just that it works fine. I hope phptidy helps you with your projects as much as it helps me! Magnus Rosenbaum <[email protected]>
Hello. Executing this:
php phptidy.php replace lib/*.php
Outputs:
Error: File 'lib/*.php' does not exist or is not readable
Just works when I supply a file, without wildcards.
But documentation says I can do it.
PHP Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /app/vendor/cmrcx/phptidy/phptidy.php on line 1441
Applies for PHP 7.2 and phptidy 3.1
I'd like to keep <?=
but not to use short open tags <?
.
I saw the global variable open_tag
, but I think it affects both.
Even, if possible, I'd like to rewrite <?php echo
to <?=
.
Getting those with 3.2 and PHP 7.3.11
PHP Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /app/vendor/cmrcx/phptidy/phptidy.php on line 960
PHP Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /app/vendor/cmrcx/phptidy/phptidy.php on line 1003
So, I looks like it should be using break
or continue 2
?
Can this be used for html only pages with template definitions from php framework?
indent_curly_brace_open.php.expected.phptidy.php
does not look like the result I get using PHP 8.1.2-1ubuntu2.11 (cli)
I set:
$diff = "diff -u";
$curly_brace_newline = true;
expecting
<?
if (false)
return;
function x() {
if (true) {
}
function &xxx() {
}
{
exit;
}
{}
}
to become
<?
if (false)
return;
function x()
{
if (true)
{
}
function &xxx()
{
}
{
exit;
}
}
But sadly - nothing changed. No indented blocks and no wrapping of curly braces.
WordPress coding standard expects a space on both sides of the !
operator and currently it is not possible to achieve this.
eg:
if ( !$some_value ) {
should be
if ( ! $some_value ) {
Hi,
I appreciate your app because it's helping me to re-indent code written by others.
I would like to submit a feature request.
1/ In function add_doctags_to_doc_comment, is it possible to replace:
case "package":
$newtext .= "* @Package ".$GLOBALS['default_package']."\n";
break;
by
case "package":
if ($GLOBALS['default_package']) {
$newtext .= "* @Package ".$GLOBALS['default_package']."\n";
}
break;
2/ Is it possible to propose an option to indent 'switch case' like in psr style ?
https://www.php-fig.org/psr/psr-12/#52-switch-case
I failed to find an acceptable code.
3/ I also have some code like :
$object->function1(...)
->function2(...)
->function3(...)
'->' is always indented to same level as '$'.
I will have preferred intented one level more.
Regards,
phptidy does not work on the current PHP 7 prerelease versions, because the function array_splice() is broken in PHP 7: https://bugs.php.net/bug.php?id=70471
I hope this will be fixed until the final release.
To have #12 in a stable tag. Thank you in advance.
I am getting the following error in the latest version of phptidy
Notice: Undefined index: functions in /Users/sudar/Dropbox/code/wp/phptidy/phptidy.php on line 2271
Warning: in_array() expects parameter 2 to be array, null given in /Users/sudar/Dropbox/code/wp/phptidy/phptidy.php on line 2271
Steps to reproduce
$add_blank_lines = false;
$add_file_docblock = true;
$fix_docblock_format = true;
$fix_docblock_space = true;
$add_function_docblocks = true;
Sorry to ask that here, but it is not completely clear to me what exactly phptidy will do and what not. Can I reindent code formatting with phptidy? Thanks!
It would be nice to have some clean way to extend the functionality of phptidy with our own rules.
Looks like phptidy does not know about callback functions...
Possible syntax error detected (No string for function name found):
=> function ($action, $model
Possible syntax error detected (No string for function name found):
=> function ($url, $model
Possible syntax error detected (No string for function name found):
=> function ($url, $model
Possible syntax error detected (No string for function name found):
=> function ($model) {
Possible syntax error detected (No string for function name found):
=> function ($action, $model
Possible syntax error detected (No string for function name found):
=> function ($url, $model
Possible syntax error detected (No string for function name found):
=> function ($url, $model
Possible syntax error detected (No string for function name found):
=> function ($model) {
Possible syntax error detected (No string for function name found):
=> function ($action, $model
Possible syntax error detected (No string for function name found):
=> function ($url, $model
I don't really have an syntax error in my code.
This code does magic!
Could you create a composer package and make it available via vendor/bin
To have #17 in a stable tag-
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.