vscode php-cs-fixe 세팅

💦💦💦💦·2022년 5월 30일
0

팀 개발환경

목록 보기
1/4

동료와 코딩스타일을 맞춰보자

컴포저 php-cs-fixer 설치 (내 계정에 global 설치됩니다)

composer global require friendsofphp/php-cs-fixer

vscode 플러그인 php-cs-fixer 설치

php cs fixer - Visual Studio Marketplace

setting.json (vscode 원격서버 세팅파일)

{
    "php-cs-fixer.executablePath": "/www/사번/.config/composer/vendor/bin/php-cs-fixer",
    "php-cs-fixer.executablePathWindows": "", //eg: php-cs-fixer.bat
    "php-cs-fixer.onsave": false,
    "php-cs-fixer.rules": "@PSR2",
    "php-cs-fixer.config": "/www/vscode_config/.php-cs-fixer.php",
    "php-cs-fixer.allowRisky": false,
    "php-cs-fixer.pathMode": "override",
    "php-cs-fixer.exclude": [],
    "php-cs-fixer.autoFixByBracket": true,
    "php-cs-fixer.autoFixBySemicolon": false,
    "php-cs-fixer.formatHtml": false,
    "php-cs-fixer.documentFormattingProvider": true
}

bash_profile 설정

경로 : ~/.bash_profile

PATH 변수를 수정합니다

PATH=$PATH:$HOME/.local/bin:$HOME/bin:/usr/bin/php:$HOME/.config/composer/vendor/bin

수정후 아래 명령어 실행합니다
source .bash_profile

vscode 프로그램 재시작

마우스 드래그 > 블록지정 > 우클릭 > "선택영역 서식" : 선택된 영역만 적용

일반 우클릭 > "문서서식" : 파일 전체에 포맷이 적용된다.

제외한 규칙 2개 (vscode에서 설정시 에러 발생했음..)

single_class_element_per_statement
single_blank_line_before_namespace

.php-cs-fixer.php 설정파일

<?php
/*
 * This document has been generated with
 * https://mlocati.github.io/php-cs-fixer-configurator/#version:3.0.2|configurator
 * you can change this configuration by importing this file.
 */
$config = new PhpCsFixer\Config();

return $config
    // 들여쓰기 탭
    ->setIndent("\t")
    ->setRules([
        // There MUST be one blank line after the namespace declaration.
        'blank_line_after_namespace' => true,
        //중괄호 {} 처리
        'braces' => true,
        // Whitespace around the keywords of a class, trait or interfaces definition should be one space.
        'class_definition' => true,
        // Remove extra spaces in a nullable typehint.
        'compact_nullable_typehint' => true,
        // The PHP constants `true`, `false`, and `null` MUST be written using the correct casing.
        'constant_case' => true,
        // Equal sign in declare statement should be surrounded by spaces or not following configuration.
        'declare_equal_normalize' => true,
        // The keyword `elseif` should be used instead of `else if` so that all control keywords look like single words.
        'elseif' => true,
        // PHP code MUST use only UTF-8 without BOM (remove BOM).
        'encoding' => true,
        // PHP code must use the long `<?php` tags or short-echo `<?=` tags and not other tag variations.
        'full_opening_tag' => true,
        // Spaces should be properly placed in a function declaration.
        'function_declaration' => true,
        // Code MUST use configured indentation type.
        'indentation_type' => true,
        // All PHP files must use same line ending.
        'line_ending' => true,
        // PHP keywords MUST be in lower case.
        'lowercase_keywords' => true,
        // Class static references `self`, `static` and `parent` MUST be in lower case.
        'lowercase_static_reference' => true,
        // In method arguments and method call, there MUST NOT be a space before each comma and there MUST be one space after each comma. Argument lists MAY be split across multiple lines, where each subsequent line is indented once. When doing so, the first item in the list MUST be on the next line, and there MUST be only one argument per line.
        'method_argument_space' => true,
        // There must be a comment when fall-through is intentional in a non-empty case body.
        'no_break_comment' => true,
        // The closing `? >` tag MUST be omitted from files containing only PHP.
        'no_closing_tag' => true,
        // Remove leading slashes in `use` clauses.
        'no_leading_import_slash' => true,
        // When making a method or function call, there MUST NOT be a space between the method or function name and the opening parenthesis.
        'no_spaces_after_function_name' => true,
        // There MUST NOT be a space after the opening parenthesis. There MUST NOT be a space before the closing parenthesis.
        'no_spaces_inside_parenthesis' => true,
        // Remove trailing whitespace at the end of non-blank lines.
        'no_trailing_whitespace' => true,
        // There MUST be no trailing spaces inside comment or PHPDoc.
        'no_trailing_whitespace_in_comment' => true,
        // Orders the elements of classes/interfaces/traits.
        'ordered_class_elements' => true,
        // Ordering `use` statements.
        'ordered_imports' => true,
        // There should be one or no space before colon, and one space after it in return type declarations, according to configuration.
        'return_type_declaration' => true,
        // Cast `(boolean)` and `(integer)` should be written as `(bool)` and `(int)`, `(double)` and `(real)` as `(float)`, `(binary)` as `(string)`.
        'short_scalar_cast' => true,
        // A PHP file without end tag must always end with a single empty line feed.
        'single_blank_line_at_eof' => true,
        // There MUST be one use keyword per declaration.
        'single_import_per_statement' => true,
        // Each namespace use MUST go on its own line and there MUST be one blank line after the use statements block.
        'single_line_after_imports' => true,
        // Each trait `use` must be done as single statement.
        'single_trait_insert_per_statement' => true,
        // A case should be followed by a colon and not a semicolon.
        'switch_case_semicolon_to_colon' => true,
        // Removes extra spaces between colon and case value.
        'switch_case_space' => true,
        // Visibility MUST be declared on all properties and methods; `abstract` and `final` MUST be declared before the visibility; `static` MUST be declared after the visibility.
        'visibility_required' => true,
        //배열 들여쓰기
        'array_indentation' => true,
        //배열 변경 array() => []
        'array_syntax' => ['syntax' => 'short'],
        //unset() 합치기
        'combine_consecutive_unsets' => true,
        // 클레스및 인터페이스 빈줄로 구분 지어줌
        'class_attributes_separation' => ['elements' => ['method' => 'one', ]],
        //줄사이를 1줄이하로 조정
        'multiline_whitespace_before_semicolons' => false,
        //따옴표를 작은 따옴표로 변경
        'single_quote' => true,
        //비교 연산자 빈칸 처리
        'binary_operator_spaces' => [
            'operators' => [
                // '=>' => 'align',
                // '=' => 'align'
            ]
        ],
        // <?php 태그 다음에 빈줄 추가
        'blank_line_after_opening_tag' => true,
        //return 전에 빈줄 추가
        'blank_line_before_statement' => true,
        //형변환 예 (int) 에 빈칸 처리
        'cast_spaces' => true,
        'concat_space' => ['spacing' => 'one'],
        //declare()내부 빈칸처리
        'declare_equal_normalize' => true,
        //함수 인자 타입 빈칸처리
        'function_typehint_space' => true,
        'single_line_comment_style' => ['comment_types' => ['hash']],
        //include ,require 관련 빈칸 처리
        'include' => true,
        //형변환 소문자
        'lowercase_cast' => true,
        // 기본제공함수 소문자로 변환
        'native_function_casing' => true,
        // new 키워드 사용시 () 사용하기
        'new_with_braces' => true,
        // 클래스 여는 중괄호 뒤에 빈 줄이 없어야 합니다.
        'no_blank_lines_after_class_opening' => true,
        // 'no_blank_lines_after_phpdoc' => true,
        'no_blank_lines_before_namespace' => true,
        // 'no_empty_comment' => true,
        // 'no_empty_phpdoc' => true,
        // 'no_empty_statement' => true,
        'no_extra_blank_lines' => [
            'tokens' => [
                'curly_brace_block',
                'extra',
                // 'parenthesis_brace_block',
                // 'square_brace_block',
                'throw',
                'use',
            ]
        ],
        // 'no_leading_namespace_whitespace' => true,
        // 'no_mixed_echo_print' => array('use' => 'echo'),
        // '=>' 사이에 공간 삭제
        'no_multiline_whitespace_around_double_arrow' => true,
        // 'no_short_bool_cast' => true,
        // 'no_singleline_whitespace_before_semicolons' => true,
        //배열 값 사이 빈칸 삭제
        'no_spaces_around_offset' => true,
        // 'no_trailing_comma_in_list_call' => true,
        // 'no_trailing_comma_in_singleline_array' => true,
        // 'no_unneeded_control_parentheses' => true,
        // 'no_unused_imports' => true,
        // 배열에서 콤마 앞의 빈칸 삭제,
        'no_whitespace_before_comma_in_array' => true,
        //빈줄에 빈칸 삭제
        'no_whitespace_in_blank_line' => true,
        // 'normalize_index_brace' => true,
        // '->' 사이 빈칸 삭제
        'object_operator_without_whitespace' => true,
        // 'php_unit_fqcn_annotation' => true,
        // 'phpdoc_align' => true,
        // 'phpdoc_annotation_without_dot' => true,
        // 'phpdoc_indent' => true,
        // 'phpdoc_inline_tag' => true,
        // 'phpdoc_no_access' => true,
        // 'phpdoc_no_alias_tag' => true,
        // 'phpdoc_no_empty_return' => true,
        // 'phpdoc_no_package' => true,
        // 'phpdoc_no_useless_inheritdoc' => true,
        // 'phpdoc_return_self_reference' => true,
        // 'phpdoc_scalar' => true,
        // 'phpdoc_separation' => true,
        // 'phpdoc_single_line_var_spacing' => true,
        // 'phpdoc_summary' => true,
        // 'phpdoc_to_comment' => true,
        // 'phpdoc_trim' => true,
        // 'phpdoc_types' => true,
        // 'phpdoc_var_without_name' => true,
        // 'increment_style' => true,
        // 'self_accessor' => true,
        // 'standardize_not_equals' => true,
        // 삼항연산자에 빈칸 삽입
        'ternary_operator_spaces' => true,
        // 'trailing_comma_in_multiline_array' => true,
        //array 앞뒤 빈칸 삭제
        'trim_array_spaces' => true,
        //연산자 빈칸 삭제
        'unary_operator_spaces' => true,
        //배열 콤마',' 뒤에 빈칸 추가
        'whitespace_after_comma_in_array' => true,
        //세미콜론 뒤에 빈칸 삽입
        'space_after_semicolon' => true,
    ])
    ->setFinder(
        PhpCsFixer\Finder::create()
        ->exclude('vendor')
        ->in(__DIR__)
    )
;
profile
속도보다 방향

0개의 댓글