vim php代码规范

Aug 08, 2016 am 09:22 AM
must php symfony

vim 代码规范工具php-cs-fixer.phar
(参考https://github.com/FriendsOfPHP/PHP-CS-Fixer)
INSTALL

<code>curl http:<span>//get.sensiolabs.org/php-cs-fixer.phar -o php-cs-fixer</span>
  sudo chmod a<span>+</span>x php<span>-cs</span><span>-fixer</span>
  sudo mv php<span>-cs</span><span>-fixer</span> /usr/<span>local</span>/bin/php<span>-cs</span><span>-fixer</span></code>
Copier après la connexion

至此,你可以直接用php-cs-fixer格式化代码了

USAGE

(目前支持的level有psr0, psr1, psr2,symfony,contrib)

<code>  php<span>-cs</span><span>-fixer</span> fix test<span>.</span>php <span>--</span>level<span>=</span>psr2
   php<span>-cs</span><span>-fixer</span> fix <span>.</span><span>/</span><span>--</span>level<span>=</span>psr2</code>
Copier après la connexion

这并不能满足一个vimer的小心灵

vim-php-cs-fixer
INSTALL

<code>Bundle <span>'stephpy/vim-php-cs-fixer</span></code>
Copier après la connexion

CONFIGURE
.vimrc(参考:https://github.com/stephpy/vim-php-cs-fixer)

<code><span>" If php-cs-fixer is in $PATH, you don't need to define line below
"</span><span>let</span> g:php_cs_fixer_path = <span>"~/php-cs-fixer.phar"</span><span>" define the path to the php-cs-fixer.phar
let g:php_cs_fixer_level = "</span>psr2<span>"              "</span> which level ?(psr0, psr1, psr2, symfony)
<span>let</span> g:php_cs_fixer_config = <span>"default"</span><span>" configuration
let g:php_cs_fixer_php_path = "</span>php<span>"               "</span> Path <span>to</span> PHP
<span>" If you want to define specific fixers:
"</span><span>let</span> g:php_cs_fixer_fixers_list = <span>"linefeed,short_tag,indentation"</span><span>let</span> g:php_cs_fixer_enable_default_mapping = <span>1</span><span>" Enable the mapping by default (<leader>pcd)
let g:php_cs_fixer_dry_run = 0                    "</leader></span> Call command <span>with</span> dry-run <span>option</span><span>let</span> g:php_cs_fixer_verbose = <span>0</span><span>" Return the output of command if 1, else an inline information.</span></code>
Copier après la connexion

以上配置我们发现

<code><span><span><span>let g:php_cs_fixer_enable_default_mapping = 1     "</span> Enable the mapping by <span>default</span> (<leader>pcd)</leader></span></span></code>
Copier après la connexion

这个配置快捷键为\pcd 实际操作中会发现极易出错,而且太长了吧
.vim/bundle/vim-php-cs-fixer/plugin/php-cs-fixer.vim

<code>if(g:php_cs_fixer_enable_default_mapping == 1)
    nnoremap <span>silent</span>><span>leader</span>>pcd :call PhpCsFixerFixDirectory()<span>CR</span>>
    nnoremap <span>silent</span>><span>leader</span>>pcf :call PhpCsFixerFixFile()<span>CR</span>>
endif</code>
Copier après la connexion

可以发现
pcd是格式化目录
pcf是格式化文件
现在可以把pcd,和pcf换成自己喜欢的快捷键了

ps:是nomal模式,是一个快捷键的前缀默认是\,可在vim中用map命令查看

附录

<code>psr0 [PSR-<span>0</span>] Classes must be <span>in</span> a path <span>that</span> matches their namespace, be <span>at</span> least one namespace deep, <span>and</span><span>the</span><span>class</span><span>name</span> should match <span>the</span><span>file</span><span>name</span>.

encoding [PSR-<span>1</span>] PHP code MUST use only UTF-<span>8</span><span>without</span> BOM (remove BOM).

short_tag [PSR-<span>1</span>] PHP code must use <span>the</span> long <?php ?> tags <span>or</span><span>the</span> short-echo = ?> tags; <span>it</span> must <span>not</span> use <span>the</span> other tag variations.

braces [PSR-<span>2</span>] The body <span>of</span> each structure MUST be enclosed <span>by</span> braces. Braces should be properly placed. Body <span>of</span> braces should be properly indented.

elseif [PSR-<span>2</span>] The keyword elseif should be used <span>instead of</span><span>else</span><span>if</span> so <span>that</span> all control keywords looks like single <span>words</span>.

eof_ending [PSR-<span>2</span>] A <span>file</span> must always <span>end</span><span>with</span> a single empty line feed.

function_call_space [PSR-<span>2</span>] When making a method <span>or</span> function call, there MUST NOT be a <span>space</span><span>between</span><span>the</span> method <span>or</span> function <span>name</span><span>and</span><span>the</span> opening parenthesis.

function_declaration [PSR-<span>2</span>] Spaces should be properly placed <span>in</span> a function declaration.

indentation [PSR-<span>2</span>] Code MUST use an indent <span>of</span><span>4</span> spaces, <span>and</span> MUST NOT use tabs <span>for</span> indenting.

line_after_namespace [PSR-<span>2</span>] There MUST be one blank line <span>after</span><span>the</span> namespace declaration.

<span>linefeed</span> [PSR-<span>2</span>] All PHP files must use <span>the</span> Unix LF (<span>linefeed</span>) line ending.

lowercase_constants [PSR-<span>2</span>] The PHP constants <span>true</span>, <span>false</span>, <span>and</span> null MUST be <span>in</span> lower case.

lowercase_keywords [PSR-<span>2</span>] PHP keywords MUST be <span>in</span> lower case.

method_argument_space [PSR-<span>2</span>] In method arguments <span>and</span> method call, there MUST NOT be a <span>space</span><span>before</span> each comma <span>and</span> there MUST be one <span>space</span><span>after</span> each comma.

multiple_use [PSR-<span>2</span>] There MUST be one use keyword per declaration.

parenthesis [PSR-<span>2</span>] There MUST NOT be a <span>space</span><span>after</span><span>the</span> opening parenthesis. There MUST NOT be a <span>space</span><span>before</span><span>the</span> closing parenthesis.

php_closing_tag [PSR-<span>2</span>] The closing ?> tag MUST be omitted <span>from</span> files containing only PHP.

single_line_after_imports [PSR-<span>2</span>] Each namespace use MUST go <span><span>on</span></span><span>its</span> own line <span>and</span> there MUST be one blank line <span>after</span><span>the</span> use statements block.

trailing_spaces [PSR-<span>2</span>] Remove trailing whitespace <span>at</span><span>the</span><span>end</span><span>of</span> non-blank lines.

visibility [PSR-<span>2</span>] Visibility MUST be declared <span><span>on</span></span> all properties <span>and</span> methods; abstract <span>and</span> final MUST be declared <span>before</span><span>the</span> visibility; static MUST be declared <span>after</span><span>the</span> visibility.

blankline_after_open_tag [symfony] Ensure there <span>is</span> no code <span><span>on</span></span><span>the</span> same line <span>as</span><span>the</span> PHP open tag <span>and</span><span>it</span><span>is</span> followed <span>by</span> a blankline.

concat_without_spaces [symfony] Concatenation should be used <span>without</span> spaces.

double_arrow_multiline_whitespaces [symfony] Operator => should <span>not</span> be arounded <span>by</span> multi-line whitespaces.

duplicate_semicolon [symfony] Remove duplicated semicolons.

empty_return [symfony] A <span>return</span> statement wishing <span>to</span><span>return</span> nothing should be simply <span>"return"</span>.

extra_empty_lines [symfony] Removes extra empty lines.

include [symfony] Include <span>and</span><span>file</span> path should be divided <span>with</span> a single <span>space</span>. File path should <span>not</span> be placed under brackets.

join_function [symfony] Implode function should be used <span>instead of</span> join function.

list_commas [symfony] Remove trailing commas <span>in</span><span>list</span> function calls.

multiline_array_trailing_comma [symfony] PHP multi-line arrays should have a trailing comma.

namespace_no_leading_whitespace [symfony] The namespace declaration line shouldn't <span>contain</span> leading whitespace.

new_with_braces [symfony] All instances created <span>with</span> new keyword must be followed <span>by</span> braces.

no_blank_lines_after_class_opening [symfony] There should be no empty lines <span>after</span><span>class</span> opening brace.

no_empty_lines_after_phpdocs [symfony] There should <span>not</span> be blank lines <span>between</span> docblock <span>and</span><span>the</span> documented element.

object_operator [symfony] There should <span>not</span> be <span>space</span><span>before</span><span>or</span><span>after</span> object T_OBJECT_OPERATOR.

operators_spaces [symfony] Binary operators should be arounded <span>by</span><span>at</span> least one <span>space</span>.

phpdoc_indent [symfony] Docblocks should have <span>the</span> same indentation <span>as</span><span>the</span> documented subject.

phpdoc_no_access [symfony] @access annotations should be omitted <span>from</span> phpdocs.

phpdoc_no_empty_return [symfony] @<span>return</span> void <span>and</span> @<span>return</span> null annotations should be omitted <span>from</span> phpdocs.

phpdoc_no_package [symfony] @package <span>and</span> @subpackage annotations should be omitted <span>from</span> phpdocs.

phpdoc_params [symfony] All items <span>of</span><span>the</span> @param, @throws, @<span>return</span>, @var, <span>and</span> @type phpdoc tags must be aligned vertically.

phpdoc_scalar [symfony] Scalar types should always be written <span>in</span><span>the</span> same form. <span>"int"</span>, <span>not</span><span>"integer"</span>; <span>"bool"</span>, <span>not</span><span>"boolean"</span>; <span>"float"</span>, <span>not</span><span>"real"</span><span>or</span><span>"double"</span>.

phpdoc_separation [symfony] Annotations <span>in</span> phpdocs should be grouped together so <span>that</span> annotations <span>of</span><span>the</span> same type immediately follow each other, <span>and</span> annotations <span>of</span> a different type are separated <span>by</span> a single blank line.

phpdoc_short_description [symfony] Phpdocs short descriptions should <span>end</span><span>in</span> either a full stop, exclamation mark, <span>or</span> question mark.

phpdoc_to_comment [symfony] Docblocks should only be used <span><span>on</span></span> structural elements.

phpdoc_trim [symfony] Phpdocs should start <span>and</span><span>end</span><span>with</span> content, excluding <span>the</span> very <span>first</span><span>and</span><span>last</span> line <span>of</span><span>the</span> docblocks.

phpdoc_type_to_var [symfony] @type should always be written <span>as</span> @var.

phpdoc_var_without_name [symfony] @var <span>and</span> @type annotations should <span>not</span><span>contain</span><span>the</span> variable <span>name</span>.

pre_increment [symfony] Pre incrementation/decrementation should be used <span>if</span> possible.

remove_leading_slash_use [symfony] Remove leading slashes <span>in</span> use clauses.

remove_lines_between_uses [symfony] Removes line breaks <span>between</span> use statements.
<span>
return</span> [symfony] An empty line feed should precede a <span>return</span> statement.

self_accessor [symfony] Inside a classy element <span>"self"</span> should be preferred <span>to</span><span>the</span><span>class</span><span>name</span> itself.

single_array_no_trailing_comma [symfony] PHP single-line arrays should <span>not</span> have trailing comma.

single_blank_line_before_namespace [symfony] There should be exactly one blank line <span>before</span> a namespace declaration.

single_quote [symfony] Convert double quotes <span>to</span> single quotes <span>for</span> simple strings.

spaces_before_semicolon [symfony] Single-line whitespace <span>before</span> closing semicolon are prohibited.

spaces_cast [symfony] A single <span>space</span> should be <span>between</span> cast <span>and</span> variable.

standardize_not_equal [symfony] Replace all  <span>with</span> !=.

ternary_spaces [symfony] Standardize spaces <span>around</span> ternary operator.

trim_array_spaces [symfony] Arrays should be formatted like function/method arguments, <span>without</span> leading <span>or</span> trailing single line <span>space</span>.

unalign_double_arrow [symfony] Unalign double arrow symbols.

unalign_equals [symfony] Unalign <span>equals</span> symbols.

unary_operators_spaces [symfony] Unary operators should be placed adjacent <span>to</span> their operands.

unused_use [symfony] Unused use statements must be removed.

whitespacy_lines [symfony] Remove trailing whitespace <span>at</span><span>the</span><span>end</span><span>of</span> blank lines.

align_double_arrow [contrib] Align double arrow symbols <span>in</span> consecutive lines.

align_equals [contrib] Align <span>equals</span> symbols <span>in</span> consecutive lines.

concat_with_spaces [contrib] Concatenation should be used <span>with</span><span>at</span> least one whitespace <span>around</span>.

ereg_to_preg [contrib] Replace deprecated ereg regular expression functions <span>with</span> preg. Warning! This could change code behavior.

header_comment [contrib] Add, replace <span>or</span> remove header comment.

long_array_syntax [contrib] Arrays should use <span>the</span> long syntax.

multiline_spaces_before_semicolon [contrib] Multi-line whitespace <span>before</span> closing semicolon are prohibited.

newline_after_open_tag [contrib] Ensure there <span>is</span> no code <span><span>on</span></span><span>the</span> same line <span>as</span><span>the</span> PHP open tag.

no_blank_lines_before_namespace [contrib] There should be no blank lines <span>before</span> a namespace declaration.

ordered_use [contrib] Ordering use statements.

php4_constructor [contrib] Convert PHP4-style constructors <span>to</span>
__construct. Warning! This could change code behavior.

phpdoc_order [contrib] Annotations <span>in</span> phpdocs should be ordered so <span>that</span> param annotations come <span>first</span>, <span>then</span> throws annotations, <span>then</span><span>return</span> annotations.

phpdoc_var_to_type [contrib] @var should always be written <span>as</span> @type.

short_array_syntax [contrib] PHP arrays should use <span>the</span> PHP <span>5.4</span> short-syntax.

short_echo_tag [contrib] Replace short-echo = <span>with</span> long format <?php echo syntax.

strict [contrib] Comparison should be strict. Warning! This could change code behavior.

strict_param [contrib] Functions should be used <span>with $strict param. Warning! This could change code behavior.</code>
Copier après la connexion

以上就介绍了vim php代码规范,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn

Outils d'IA chauds

Undresser.AI Undress

Undresser.AI Undress

Application basée sur l'IA pour créer des photos de nu réalistes

AI Clothes Remover

AI Clothes Remover

Outil d'IA en ligne pour supprimer les vêtements des photos.

Undress AI Tool

Undress AI Tool

Images de déshabillage gratuites

Clothoff.io

Clothoff.io

Dissolvant de vêtements AI

Video Face Swap

Video Face Swap

Échangez les visages dans n'importe quelle vidéo sans effort grâce à notre outil d'échange de visage AI entièrement gratuit !

Outils chauds

Bloc-notes++7.3.1

Bloc-notes++7.3.1

Éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise

SublimeText3 version chinoise

Version chinoise, très simple à utiliser

Envoyer Studio 13.0.1

Envoyer Studio 13.0.1

Puissant environnement de développement intégré PHP

Dreamweaver CS6

Dreamweaver CS6

Outils de développement Web visuel

SublimeText3 version Mac

SublimeText3 version Mac

Logiciel d'édition de code au niveau de Dieu (SublimeText3)

Guide d'installation et de mise à niveau de PHP 8.4 pour Ubuntu et Debian Guide d'installation et de mise à niveau de PHP 8.4 pour Ubuntu et Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 apporte plusieurs nouvelles fonctionnalités, améliorations de sécurité et de performances avec une bonne quantité de dépréciations et de suppressions de fonctionnalités. Ce guide explique comment installer PHP 8.4 ou mettre à niveau vers PHP 8.4 sur Ubuntu, Debian ou leurs dérivés. Bien qu'il soit possible de compiler PHP à partir des sources, son installation à partir d'un référentiel APT comme expliqué ci-dessous est souvent plus rapide et plus sécurisée car ces référentiels fourniront les dernières corrections de bogues et mises à jour de sécurité à l'avenir.

7 fonctions PHP que je regrette de ne pas connaître auparavant 7 fonctions PHP que je regrette de ne pas connaître auparavant Nov 13, 2024 am 09:42 AM

Si vous êtes un développeur PHP expérimenté, vous aurez peut-être le sentiment d'y être déjà allé et de l'avoir déjà fait. Vous avez développé un nombre important d'applications, débogué des millions de lignes de code et peaufiné de nombreux scripts pour réaliser des opérations.

Comment configurer Visual Studio Code (VS Code) pour le développement PHP Comment configurer Visual Studio Code (VS Code) pour le développement PHP Dec 20, 2024 am 11:31 AM

Visual Studio Code, également connu sous le nom de VS Code, est un éditeur de code source gratuit – ou environnement de développement intégré (IDE) – disponible pour tous les principaux systèmes d'exploitation. Avec une large collection d'extensions pour de nombreux langages de programmation, VS Code peut être c

Expliquez les jetons Web JSON (JWT) et leur cas d'utilisation dans les API PHP. Expliquez les jetons Web JSON (JWT) et leur cas d'utilisation dans les API PHP. Apr 05, 2025 am 12:04 AM

JWT est une norme ouverte basée sur JSON, utilisée pour transmettre en toute sécurité des informations entre les parties, principalement pour l'authentification de l'identité et l'échange d'informations. 1. JWT se compose de trois parties: en-tête, charge utile et signature. 2. Le principe de travail de JWT comprend trois étapes: la génération de JWT, la vérification de la charge utile JWT et l'analyse. 3. Lorsque vous utilisez JWT pour l'authentification en PHP, JWT peut être généré et vérifié, et les informations sur le rôle et l'autorisation des utilisateurs peuvent être incluses dans l'utilisation avancée. 4. Les erreurs courantes incluent une défaillance de vérification de signature, l'expiration des jetons et la charge utile surdimensionnée. Les compétences de débogage incluent l'utilisation des outils de débogage et de l'exploitation forestière. 5. L'optimisation des performances et les meilleures pratiques incluent l'utilisation des algorithmes de signature appropriés, la définition des périodes de validité raisonnablement,

Comment analysez-vous et traitez-vous HTML / XML dans PHP? Comment analysez-vous et traitez-vous HTML / XML dans PHP? Feb 07, 2025 am 11:57 AM

Ce tutoriel montre comment traiter efficacement les documents XML à l'aide de PHP. XML (Language de balisage extensible) est un langage de balisage basé sur le texte polyvalent conçu à la fois pour la lisibilité humaine et l'analyse de la machine. Il est couramment utilisé pour le stockage de données et

Programme PHP pour compter les voyelles dans une chaîne Programme PHP pour compter les voyelles dans une chaîne Feb 07, 2025 pm 12:12 PM

Une chaîne est une séquence de caractères, y compris des lettres, des nombres et des symboles. Ce tutoriel apprendra à calculer le nombre de voyelles dans une chaîne donnée en PHP en utilisant différentes méthodes. Les voyelles en anglais sont a, e, i, o, u, et elles peuvent être en majuscules ou en minuscules. Qu'est-ce qu'une voyelle? Les voyelles sont des caractères alphabétiques qui représentent une prononciation spécifique. Il y a cinq voyelles en anglais, y compris les majuscules et les minuscules: a, e, i, o, u Exemple 1 Entrée: String = "TutorialSpoint" Sortie: 6 expliquer Les voyelles dans la chaîne "TutorialSpoint" sont u, o, i, a, o, i. Il y a 6 yuans au total

Expliquez la liaison statique tardive en PHP (statique: :). Expliquez la liaison statique tardive en PHP (statique: :). Apr 03, 2025 am 12:04 AM

Liaison statique (statique: :) ​​implémente la liaison statique tardive (LSB) dans PHP, permettant à des classes d'appel d'être référencées dans des contextes statiques plutôt que de définir des classes. 1) Le processus d'analyse est effectué au moment de l'exécution, 2) Recherchez la classe d'appel dans la relation de succession, 3) il peut apporter des frais généraux de performance.

Quelles sont les méthodes PHP Magic (__construct, __ destruct, __ call, __get, __set, etc.) et fournir des cas d'utilisation? Quelles sont les méthodes PHP Magic (__construct, __ destruct, __ call, __get, __set, etc.) et fournir des cas d'utilisation? Apr 03, 2025 am 12:03 AM

Quelles sont les méthodes magiques de PHP? Les méthodes magiques de PHP incluent: 1. \ _ \ _ Construct, utilisé pour initialiser les objets; 2. \ _ \ _ Destruct, utilisé pour nettoyer les ressources; 3. \ _ \ _ Appel, gérer les appels de méthode inexistants; 4. \ _ \ _ GET, Implémentez l'accès à l'attribut dynamique; 5. \ _ \ _ SET, Implémentez les paramètres d'attribut dynamique. Ces méthodes sont automatiquement appelées dans certaines situations, améliorant la flexibilité et l'efficacité du code.

See all articles