Are you happy with your logging solution? Would you help us out by taking a 30-second survey? Click here


PHP Code Formatter

Subscribe to updates I use phpcf

Statistics on phpcf

Number of watchers on Github 163
Number of open issues 3
Average time to close an issue about 1 month
Main language PHP
Average time to merge a PR 4 days
Open pull requests 1+
Closed pull requests 0+
Last commit over 2 years ago
Repo Created over 5 years ago
Repo Last Updated almost 2 years ago
Size 158 KB
Organization / Authorbadoo
Page Updated
Do you use phpcf? Leave a review!
View open issues (3)
View phpcf activity
View on github
Fresh, new opensource launches 🚀🚀🚀
Trendy new open source projects in your inbox! View examples

Subscribe to our mailing list

Evaluating phpcf for your project? Score Explanation
Commits Score (?)
Issues & PR Score (?)


The formatter was created to basically only modify whitespaces, for example line feed, tabs, spaces, etc. It means that phpcf does not replace similar utilities like PHP Code Sniffer (phpcs) or PHP Coding Standards Fixer ( by Fabien Potencier. The utility supplements others and does all the dirty work with whitespace characters. It is worth noting that our utility respects the initial file formatting and only changes whitespace characters that do not follow the chosen ruleset (some utilities remove all whitespace first and reconstruct file from scratch, which is not necessarily what people want).

Our utility is extensible and supports arbitrary style sets. You can define your own formatting style pretty easily to replace Badoo formatting standard that is a bit different from PSR.

Below is a little usage example.

  • phpcf apply formats the specified file
  • phpcf check checks that formatting is correct and returns non-zero exit code when file is not formatted properly
$ cat minifier.php
$tokens=token_get_all(file_get_contents($argv[1]));$contents='';foreach($tokens as $tok){if($tok[0]===T_WHITESPACE||$tok[0]===T_COMMENT)continue;if($tok[0]===T_AS||$tok[0]===T_ELSE)$contents.=' '.$tok[1].' '; else $contents.=is_array($tok)?$tok[1]:$tok;}echo$contents."\n";

$ phpcf apply minifier.php
minifier.php formatted successfully

$ cat minifier.php
$tokens = token_get_all(file_get_contents($argv[1]));
$contents = '';
foreach ($tokens as $tok) {
    if ($tok[0] === T_WHITESPACE || $tok[0] === T_COMMENT) continue;
    if ($tok[0] === T_AS || $tok[0] === T_ELSE) $contents .= ' ' . $tok[1] . ' ';
    else $contents .= is_array($tok) ? $tok[1] : $tok;
echo $contents . "\n";

$ phpcf check minifier.php; echo $?
minifier.php does not need formatting

Our utility is also capable of formatting part of file. To do so, you need to specify line number ranges after a colon:

$ cat zebra.php 
echo "White "."strip".PHP_EOL;
echo "Black "."strip".PHP_EOL; // not formatted
echo "Arse".PHP_EOL;

$ phpcf apply zebra.php:1-2,4
zebra.php formatted successfully

$ cat zebra.php 
echo "White " . "strip" . PHP_EOL;
echo "Black "."strip".PHP_EOL; // not formatted
echo "Arse" . PHP_EOL;

$ phpcf check zebra.php
zebra.php issues:
        Expected one space before binary operators (= < > * . etc)   on line 3 column 14
        Expected one space after binary operators (= < > * . etc)   on line 3 column 15

$ echo $?

It is worth noting again that phpcf is designed to only change whitespace characters and to do the most simple tasks such as:

  • replacing with
  • removing extra closing tag from the end of file
  • change cyrillic letters to english in function names
  • expressions that are aligned manually using spaces are not touched

The formatter works as a finite state machine with rules that are set by user instead of using hard-coded replacements. We supply the default config that follows Badoo formatting rules. So if you would like to get automatic var -> public replacement or similar we suggest looking at PHP-CS-Fixer. The latter does not really touch whitespace characters but it can do much more sophisticated replacements than our utility.

phpcf open issues Ask a question     (View All Issues)
  • over 3 years phpcf-php7 make error
  • over 5 years Support for __halt_compiler() PHP function
  • over 5 years Annotations are formatted in a wrong way
  • over 5 years Formatting of mixed PHP-HTML content
phpcf open pull requests (View All Pulls)
  • Supports stub *.phar
phpcf list of languages used
Other projects in PHP