Fix indented PHP files with php-cs-fixer

I have several hundred terribly indented PHP files with mixed tabs and spaces (and even mixed line endings, I suppose), I would like to fix them with php-cs-fixer v2 +.

I configured php-cs-fixer for my needs, and the code is smoothed accordingly - except indentation. I tried the minimal configuration as shown below to solve the problem. But I can not get the indentation fix:

return PhpCsFixer\Config::create()
    ->setRules([
        '@PSR2' => true,
        'indentation_type' => true,
        'braces' => ['position_after_functions_and_oop_constructs' => 'same'],
    ])
    ->setIndent("\t")
    ->setLineEnding("\r\n")

I am currently running this in my Windows window using the following command (here for one file):

php-cs-fixer.bat fix new_user.php --config /full/windowspath/to/php_cs.dist

Just in case, the generated php_cs.cache file (which contains the actually applicable rules in JSON) looks like this:

{
    "php": "5.6.31",
    "version": "2.6.0:v2.6.0#5642a36a60c11cdd01488d192541a89bb44a4abf",
    "rules": {
        "blank_line_after_namespace": true,
        "braces": {
            "position_after_functions_and_oop_constructs": "same"
        },
        "class_definition": true,
        "elseif": true,
        "function_declaration": true,
        "indentation_type": true,
        "line_ending": true,
        "lowercase_constants": true,
        "lowercase_keywords": true,
        "method_argument_space": {
            "ensure_fully_multiline": true
        },
        "no_break_comment": true,
        "no_closing_tag": true,
        "no_spaces_after_function_name": true,
        "no_spaces_inside_parenthesis": true,
        "no_trailing_whitespace": true,
        "no_trailing_whitespace_in_comment": true,
        "single_blank_line_at_eof": true,
        "single_class_element_per_statement": {
            "elements": ["property"]
        },
        "single_import_per_statement": true,
        "single_line_after_imports": true,
        "switch_case_semicolon_to_colon": true,
        "switch_case_space": true,
        "visibility_required": true,
        "encoding": true,
        "full_opening_tag": true
    },
    "hashes": {
        "new_students.org_.php": -151826318
    }
}

And here are a few indented indented files.

<?php
session_start();

  include 'connect.php';
include 'functions.php';    

$test= "abc";
    $additional_studs = "";
    if (date('m') == 12 and $term='SP') {
        $yr_suffix = date('y') + 1;
    } else {
        $yr_suffix = date('y');
    }


    function dup_stud($id, $conn)
    {//...
}

$i = 0;

$test="abc"; include 'connect.php'; /, .

. , , , .

NetBeans, , , , .

+5
4

braces .

. . .

indentation_type .

@PSR2, .

. README.


php-cs-fixer 2.6

<?php
$test= "abc";
    $additional_studs = "";
    if (date('m') == 12 and $term='SP') {
        $yr_suffix = date('y') + 1;
    } else {
        $yr_suffix = date('y');
    }


    function dup_stud($id, $conn)
    {//...
    }

$i = 0;

.

<?php
echo "a";
    echo "b";
echo "c";

php-cs-fixer.

+5

, , .

, . , .

php-cs-fixer, phpcbf , sed script :

sed "s/^[ \t]*//" -i test.php

php-cs-fixer phpcbf, , PSR-2. , - (.. ). , , . .

, sed, phpcbf

phpcbf --standard="PSR2" test.php

php-cs-fixer fix test.php --rules=@PSR2

PHP PSR-2. , .

:

  • , 4- , , php_cs PSR-2 ( , , ..).
  • 4 , PSR-2. , , .
  • , php-cs-fixer phpcbf . , , , .
+2

. Visual Studio Code. , phpfmt . , , .

0

. , , .

PHP Formatter . OP " ", PHP Formatter:

C:\>DMSFormat PHP~v7 \temp\test.php
PHP~v7 PrettyPrinter Version 1.3.17
Copyright (C) 2004-2016 Semantic Designs, Inc; All Rights Reserved; SD Confidential
Powered by DMS (R) Software Reengineering Toolkit
DMS_PHP~v7_INPUT_ENCODING=ISO-8859-1
DMS_PHP~v7_OUTPUT_ENCODING=ISO-8859-1
Parsing \temp\test.php [encoding ISO-8859-1 +CRLF +LF +CR +NEL +1 /^I]


<?php
include 'connect.php';
include 'functions.php';
$test="abc";
$additional_studs="";
if (date('m') == 12 and $term='SP') {
  $yr_suffix=date('y')+1;
}
else {
  $yr_suffix=date('y');
}

function dup_stud($id,$conn) { //...
}
$i=0;

(

<?php

, .)

. , [, , , OP].

PHP PHP , prettyprinter AST . .

-3

Source: https://habr.com/ru/post/1685971/


All Articles