├── .gitignore ├── .travis.yml ├── CHANGELOG.md ├── LICENSE ├── README.md ├── README.src.md ├── build.php ├── build └── phpf.phar ├── src ├── .gitignore └── phpf.php └── testing ├── phpunit.xml └── tests ├── TestAlignPHPCode.php ├── TestAlignPHPCode2.php ├── TestRemoveIncludeParentheses.php ├── TestSpaceAroundControlStructures.php ├── TestSpaceAroundExclamationMark.php ├── TestSpaceAroundParentheses.php └── bootstrap.php /.gitignore: -------------------------------------------------------------------------------- 1 | .phpintel 2 | *.sublime* 3 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | matrix: 2 | include: 3 | # Linux 4 | - os: linux 5 | language: php 6 | php: '5.6' 7 | 8 | - os: linux 9 | language: php 10 | php: '7.0' 11 | 12 | - os: linux 13 | language: php 14 | php: '7.1' 15 | 16 | - os: linux 17 | language: php 18 | php: 'nightly' 19 | 20 | before_script: 21 | - if [[ $TRAVIS_OS_NAME == 'osx' ]] ; then brew update ; fi; # travis image is quite old, update to have latest dependecies available 22 | - if [[ $TRAVIS_OS_NAME == 'osx' ]] ; then brew tap homebrew/dupes ; fi; 23 | - if [[ $TRAVIS_OS_NAME == 'osx' ]] ; then brew tap homebrew/versions ; fi; 24 | - if [[ $TRAVIS_OS_NAME == 'osx' ]] ; then brew tap homebrew/homebrew-php ; fi; 25 | - if [[ $TRAVIS_OS_NAME == 'osx' ]] ; then brew install ${OSX_PHP_VERSION} ; fi; # install PHP 26 | - if [[ $TRAVIS_OS_NAME == 'osx' ]] ; then curl -sS https://getcomposer.org/installer | php ; fi; # install composer 27 | - if [[ $TRAVIS_OS_NAME == 'osx' ]] ; then mv composer.phar /usr/local/bin/composer | php ; fi; # install composer globally 28 | - if [[ $TRAVIS_OS_NAME == 'osx' ]] ; then export PATH="$(brew --prefix homebrew/php/${OSX_PHP_VERSION})/bin:$PATH" ; fi; # app php to path 29 | - if [[ $TRAVIS_OS_NAME == 'osx' ]] ; then brew install coreutils ; fi; # truncate command: 30 | - composer self-update 31 | - composer require phpunit/phpunit:4.8.35 --prefer-dist --no-interaction --dev 32 | - composer update 33 | 34 | script: 35 | - vendor/bin/phpunit -c testing/phpunit.xml 36 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ## 19.7 4 | 5 | ### 19.7.4 6 | 7 | * Fixed AlignPHPCode heredoc alignment bug 8 | 9 | ### 19.7.3 10 | 11 | * Added AlignPHPCode2 pass 12 | * Fixed [AlignPHPCode bug](https://github.com/akalongman/sublimetext-codeformatter/issues/250) 13 | * Added new tests 14 | 15 | ### 19.7.0 16 | 17 | * Added `SpaceAroundParentheses` pass 18 | 19 | ## 19.6.4 20 | 21 | * Old stable version 22 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) phpF and its authors 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 5 | 6 | 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 7 | 8 | 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 9 | 10 | 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 11 | 12 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # phpF 2 | 3 | ## Project Abandoned 4 | 5 | This project has been abandoned due to lack of interest. [You may use other forks of this](https://github.com/subins2000/phpF/network). 6 | 7 | [![Build Status](https://travis-ci.org/subins2000/phpF.svg?branch=master)](https://travis-ci.org/subins2000/phpF) 8 | 9 | **phpF** formats PHP code by making it readable and helps your code to follow coding guidelines. 10 | 11 | This is a fork of the project **phpfmt**. 12 | 13 | ## Requirements 14 | 15 | * PHP 5.6 or newer 16 | 17 | ## Usage 18 | 19 | ``` 20 | php phpf.phar 21 | ``` 22 | 23 | ## Arguments 24 | 25 | | Argument | Description | 26 | | -------- | ----------- | 27 | | --cache[=FILENAME] | cache file. Default: .php.tools.cache | 28 | | --cakephp | Apply CakePHP coding style | 29 | | --config=FILENAME | configuration file. Default: .phpf.ini | 30 | | --constructor=type | analyse classes for attributes and generate constructor - camel, snake, golang | 31 | | --dry-run | Runs the formatter without atually changing files; returns exit code 1 if changes would have been applied | 32 | | --enable_auto_align | disable auto align of ST_EQUAL and T_DOUBLE_ARROW | 33 | | --exclude=pass1,passN,... | disable specific passes | 34 | | --help-pass | show specific information for one pass | 35 | | --ignore=PATTERN-1,PATTERN-N,... | ignore file names whose names contain any PATTERN-N | 36 | | --indent_with_space=SIZE | use spaces instead of tabs for indentation. Default 4 | 37 | | --lint-before | lint files before pretty printing (PHP must be declared in %PATH%/$PATH) | 38 | | --list | list possible transformations | 39 | | --list-simple | list possible transformations - greppable | 40 | | --no-backup | no backup file (original.php~) | 41 | | --passes=pass1,passN,... | call specific compiler pass | 42 | | --profile=NAME | use one of profiles present in configuration file | 43 | | --psr | activate PSR1 and PSR2 styles | 44 | | --psr1 | activate PSR1 style | 45 | | --psr1-naming | activate PSR1 style - Section 3 and 4.3 - Class and method names case. | 46 | | --psr2 | activate PSR2 style | 47 | | --setters_and_getters=type | analyse classes for attributes and generate setters and getters - camel, snake, golang | 48 | | --smart_linebreak_after_curly | convert multistatement blocks into multiline blocks | 49 | | --visibility_order | fixes visibiliy order for method in classes - PSR-2 4.2 | 50 | | --yoda | yoda-style comparisons | 51 | | -h, --help | this help message | 52 | | -o=- | output the formatted code to standard output | 53 | | -o=file | output the formatted code to "file" | 54 | | -v | verbose | 55 | 56 | 57 | ### Currently Supported Transformations: 58 | 59 | * AddMissingParentheses Add extra parentheses in new instantiations. 60 | * AliasToMaster Replace function aliases to their masters - only basic syntax alias. 61 | * AlignConstVisibilityEquals Vertically align "=" of visibility and const blocks. 62 | * AlignDoubleArrow Vertically align T_DOUBLE_ARROW (=>). 63 | * AlignDoubleSlashComments Vertically align "//" comments. 64 | * AlignEquals Vertically align "=". 65 | * AlignGroupDoubleArrow Vertically align T_DOUBLE_ARROW (=>) by line groups. 66 | * AlignPHPCode Align PHP code within HTML block. 67 | * AlignPHPCode2 Align PHP code within opening and closing php block. 68 | * AlignTypehint Vertically align function type hints. 69 | * AllmanStyleBraces Transform all curly braces into Allman-style. 70 | * AutoPreincrement Automatically convert postincrement to preincrement. 71 | * AutoSemicolon Add semicolons in statements ends. 72 | * CakePHPStyle Applies CakePHP Coding Style 73 | * ClassToSelf "self" is preferred within class, trait or interface. 74 | * ClassToStatic "static" is preferred within class, trait or interface. 75 | * ConvertOpenTagWithEcho Convert from " implode()). 83 | * LeftWordWrap Word wrap at 80 columns - left justify. 84 | * LongArray Convert short to long arrays. 85 | * MergeElseIf Merge if with else. 86 | * SplitElseIf Merge if with else. 87 | * MergeNamespaceWithOpenTag Ensure there is no more than one linebreak before namespace 88 | * MildAutoPreincrement Automatically convert postincrement to preincrement. (Deprecated pass. Use AutoPreincrement instead). 89 | * NewLineBeforeReturn Add an empty line before T_RETURN. 90 | * OrganizeClass Organize class, interface and trait structure. 91 | * OrderAndRemoveUseClauses Order use block and remove unused imports. 92 | * OnlyOrderUseClauses Order use block - do not remove unused imports. 93 | * OrderMethod Organize class, interface and trait structure. 94 | * OrderMethodAndVisibility Organize class, interface and trait structure. 95 | * PHPDocTypesToFunctionTypehint Read variable types from PHPDoc blocks and add them in function signatures. 96 | * PrettyPrintDocBlocks Prettify Doc Blocks 97 | * PSR2EmptyFunction Merges in the same line of function header the body of empty functions. 98 | * PSR2MultilineFunctionParams Break function parameters into multiple lines. 99 | * ReindentAndAlignObjOps Align object operators. 100 | * ReindentSwitchBlocks Reindent one level deeper the content of switch blocks. 101 | * RemoveIncludeParentheses Remove parentheses from include declarations. 102 | * RemoveSemicolonAfterCurly Remove semicolon after closing curly brace. 103 | * RemoveUseLeadingSlash Remove leading slash in T_USE imports. 104 | * ReplaceBooleanAndOr Convert from "and"/"or" to "&&"/"||". Danger! This pass leads to behavior change. 105 | * ReplaceIsNull Replace is_null($a) with null === $a. 106 | * RestoreComments Revert any formatting of comments content. 107 | * ReturnNull Simplify empty returns. 108 | * ShortArray Convert old array into new array. (array() -> []) 109 | * SmartLnAfterCurlyOpen Add line break when implicit curly block is added. 110 | * SortUseNameSpace Organize use clauses by length and alphabetic order. 111 | * SpaceAroundControlStructures Add space around control structures. 112 | * SpaceAroundExclamationMark Add spaces around exclamation mark. 113 | * SpaceAroundParentheses Add spaces inside parentheses. 114 | * SpaceBetweenMethods Put space between methods. 115 | * StrictBehavior Activate strict option in array_search, base64_decode, in_array, array_keys, mb_detect_encoding. Danger! This pass leads to behavior change. 116 | * StrictComparison All comparisons are converted to strict. Danger! This pass leads to behavior change. 117 | * StripExtraCommaInArray Remove trailing commas within array blocks 118 | * StripNewlineAfterClassOpen Strip empty lines after class opening curly brace. 119 | * StripNewlineAfterCurlyOpen Strip empty lines after opening curly brace. 120 | * StripNewlineWithinClassBody Strip empty lines after class opening curly brace. 121 | * StripSpaces Remove all empty spaces 122 | * StripSpaceWithinControlStructures Strip empty lines within control structures. 123 | * TightConcat Ensure string concatenation does not have spaces, except when close to numbers. 124 | * TrimSpaceBeforeSemicolon Remove empty lines before semi-colon. 125 | * UpgradeToPreg Upgrade ereg_* calls to preg_* 126 | * WordWrap Word wrap at 80 columns. 127 | * WrongConstructorName Update old constructor names into new ones. http://php.net/manual/en/language.oop5.decon.php 128 | * YodaComparisons Execute Yoda Comparisons. 129 | -------------------------------------------------------------------------------- /README.src.md: -------------------------------------------------------------------------------- 1 | # phpF 2 | 3 | [![Build Status](https://travis-ci.org/subins2000/phpF.svg?branch=master)](https://travis-ci.org/subins2000/phpF) 4 | 5 | **phpF** formats PHP code by making it readable and helps your code to follow coding guidelines. 6 | 7 | This is a fork of the project **phpfmt**. 8 | 9 | ## Requirements 10 | 11 | * PHP 5.6 or newer 12 | 13 | ## Usage 14 | 15 | ``` 16 | php phpf.phar 17 | ``` 18 | 19 | ## Arguments 20 | 21 | | Argument | Description | 22 | | -------- | ----------- | 23 | %USAGE% 24 | 25 | ### Currently Supported Transformations: 26 | 27 | %PASSES% 28 | -------------------------------------------------------------------------------- /build.php: -------------------------------------------------------------------------------- 1 | setStub( $phar->createDefaultStub( 'phpf.php' ) ); 32 | 33 | /** 34 | * Generate READNE 35 | */ 36 | $readme_src = file_get_contents( __DIR__ . '/README.src.md' ); 37 | $readme_out = __DIR__ . '/README.md'; 38 | 39 | $testEnv = true; 40 | require $srcRoot . '/phpf.php'; 41 | 42 | $usage = ''; 43 | foreach ( getOptions( false ) as $k => $v ) { 44 | $usage .= '| ' . $k . ' | ' . $v . " |\n"; 45 | } 46 | 47 | $cmd = sprintf( 'php %s/phpf.php --list-simple', $srcRoot ); 48 | $passes = explode( PHP_EOL, trim( `$cmd` ) ); 49 | $passes = implode( PHP_EOL, 50 | array_map( function ( $v ) { 51 | return ' * ' . $v; 52 | }, $passes ) 53 | ); 54 | 55 | file_put_contents( $readme_out, 56 | strtr( $readme_src, [ 57 | '%USAGE%' => $usage, 58 | '%PASSES%' => $passes, 59 | ] ) 60 | ); 61 | -------------------------------------------------------------------------------- /build/phpf.phar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/subins2000/phpF/3c60b1a0cd986a029287d44bcf953b053ee8ea5d/build/phpf.phar -------------------------------------------------------------------------------- /src/.gitignore: -------------------------------------------------------------------------------- 1 | composer.lock 2 | vendor 3 | -------------------------------------------------------------------------------- /testing/phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 14 | tests/ 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /testing/tests/TestAlignPHPCode.php: -------------------------------------------------------------------------------- 1 | 12 |
13 | 17 | 18 |
19 | HTML; 20 | ?> 21 | 22 | CODE; 23 | 24 | public function testEnabled() { 25 | $output = executeCommand( 26 | array( 27 | '--passes' => 'AlignPHPCode', 28 | ), 29 | $this->code 30 | ); 31 | 32 | $expected_result = << 36 |
37 | 41 | 42 |
43 | HTML; 44 | ?> 45 | 46 | CODE; 47 | 48 | $this->assertContains( $expected_result, $output ); 49 | } 50 | 51 | public function testDisabled() { 52 | $output = executeCommand( 53 | array( 54 | '--exclude' => 'AlignPHPCode', 55 | ), 56 | $this->code 57 | ); 58 | 59 | $expected_result = << 63 |
64 | 68 | 69 |
70 | HTML; 71 | ?> 72 | 73 | CODE; 74 | 75 | $this->assertContains( $expected_result, $output ); 76 | } 77 | 78 | } 79 | -------------------------------------------------------------------------------- /testing/tests/TestAlignPHPCode2.php: -------------------------------------------------------------------------------- 1 | 12 |
13 | 20 | 21 |
22 | HTML; 23 | ?> 24 | 25 | CODE; 26 | 27 | public function testEnabled() { 28 | $output = executeCommand( 29 | array( 30 | '--passes' => 'AlignPHPCode2', 31 | ), 32 | $this->code 33 | ); 34 | 35 | $expected_result = << 39 |
40 | 47 | 48 |
49 | HTML; 50 | ?> 51 | 52 | CODE; 53 | 54 | $this->assertContains( $expected_result, $output ); 55 | } 56 | 57 | public function testDisabled() { 58 | $output = executeCommand( 59 | array( 60 | '--exclude' => 'AlignPHPCode2', 61 | ), 62 | $this->code 63 | ); 64 | 65 | $expected_result = << 69 |
70 | 77 | 78 |
79 | HTML; 80 | ?> 81 | 82 | CODE; 83 | 84 | $this->assertContains( $expected_result, $output ); 85 | } 86 | 87 | } 88 | -------------------------------------------------------------------------------- /testing/tests/TestRemoveIncludeParentheses.php: -------------------------------------------------------------------------------- 1 | 13 | CODE; 14 | 15 | public function testEnabled() { 16 | $output = executeCommand( 17 | array( 18 | '--passes' => 'RemoveIncludeParentheses', 19 | ), 20 | $this->code 21 | ); 22 | 23 | $this->assertContains( "include 'a.php';", $output ); 24 | $this->assertContains( "require 'a.php';", $output ); 25 | } 26 | 27 | public function testDisabled() { 28 | $output = executeCommand( 29 | array( 30 | '--exclude' => 'RemoveIncludeParentheses', 31 | ), 32 | $this->code 33 | ); 34 | 35 | $this->assertContains( "include ('a.php');", $output ); 36 | $this->assertContains( "require ('a.php');", $output ); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /testing/tests/TestSpaceAroundControlStructures.php: -------------------------------------------------------------------------------- 1 | 29 | CODE; 30 | 31 | public function testEnabled() 32 | { 33 | $output = executeCommand( 34 | array( 35 | '--passes' => 'SpaceAroundControlStructures', 36 | ), 37 | $this->code 38 | ); 39 | 40 | $this->assertContains( 41 | << 'SpaceAroundControlStructures', 76 | ), 77 | $this->code 78 | ); 79 | 80 | $this->assertContains( 81 | << 13 | CODE; 14 | 15 | public function testEnabled() { 16 | $output = executeCommand( 17 | array( 18 | '--passes' => 'SpaceAroundExclamationMark', 19 | ), 20 | $this->code 21 | ); 22 | 23 | $this->assertContains( 'if ( ! true)', $output ); 24 | $this->assertContains( 'if ( ! $foo)', $output ); 25 | } 26 | 27 | public function testDisabled() { 28 | $output = executeCommand( 29 | array( 30 | '--exclude' => 'SpaceAroundExclamationMark', 31 | ), 32 | $this->code 33 | ); 34 | 35 | $this->assertContains( 'if (!true)', $output ); 36 | $this->assertContains( 'if (!$foo)', $output ); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /testing/tests/TestSpaceAroundParentheses.php: -------------------------------------------------------------------------------- 1 | array( 14 | 1, 2, 15 | ), 16 | ); 17 | ?> 18 | CODE; 19 | 20 | public function testEnabled() { 21 | $output = executeCommand( 22 | array( 23 | '--passes' => 'SpaceAroundParentheses', 24 | ), 25 | $this->code 26 | ); 27 | 28 | $this->assertContains( "foo( 'a.php' );", $output ); 29 | $this->assertContains( 'foo();', $output ); 30 | $this->assertContains( 31 | << array( 34 | 1, 2, 35 | ), 36 | ); 37 | CODE 38 | , $output 39 | ); 40 | } 41 | 42 | public function testDisabled() { 43 | $output = executeCommand( 44 | array( 45 | '--exclude' => 'SpaceAroundParentheses', 46 | ), 47 | $this->code 48 | ); 49 | 50 | $this->assertContains( "foo('a.php');", $output ); 51 | $this->assertContains( "foo();", $output ); 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /testing/tests/bootstrap.php: -------------------------------------------------------------------------------- 1 | $value ) { 9 | if ( is_numeric( $option ) ) { 10 | $arguments .= ' ' . escapeshellarg( $value ); 11 | } else if ( $value === true ) { 12 | $arguments .= " $option"; 13 | } else { 14 | $arguments .= " $option=" . escapeshellarg( $value ); 15 | } 16 | } 17 | 18 | $cmd = sprintf( 'php %s %s -o=- -', $path_to_phpf, $arguments ); 19 | 20 | $descriptorspec = array( 21 | 0 => array( 'pipe', 'r' ), // stdin is a pipe that the child will read from 22 | 1 => array( 'pipe', 'w' ), // stdout is a pipe that the child will write to 23 | ); 24 | 25 | $process = proc_open( $cmd, $descriptorspec, $pipes ); 26 | 27 | if ( is_resource( $process ) ) { 28 | // $pipes now looks like this: 29 | // 0 => writeable handle connected to child stdin 30 | // 1 => readable handle connected to child stdout 31 | 32 | fwrite( $pipes[0], $stdin ); // file_get_contents('php://stdin') 33 | fclose( $pipes[0] ); 34 | 35 | $stdout = stream_get_contents( $pipes[1] ); 36 | fclose( $pipes[1] ); 37 | 38 | // It is important that you close any pipes before calling 39 | // proc_close in order to avoid a deadlock 40 | $return_value = proc_close( $process ); 41 | 42 | return $stdout; 43 | } 44 | } 45 | 46 | ?> 47 | --------------------------------------------------------------------------------