From 89636b9b4c3e1e5774135f2ff24e1faf20df5b5d Mon Sep 17 00:00:00 2001 From: David Freer Date: Thu, 18 May 2023 16:46:36 -0500 Subject: [PATCH] PHP Snippets Quality of Life and Continuity (#174889) * QoL & continuity update to existing php snippets - Sorted snippets by snippet title for discoverability - Removed all doc comments from non 'doc_' prefixed snippets - Added doc_trait for continuity with other patterns - Updated snippet prefixes - Add kvp to keyval - Prefixed fun with class_ - Changed doc_param to doc_var - Qualified 'con' to 'constructor' to not be confused with const - Updated class snippet to default the 'class' placeholder to the file's base name - Add snippet choices for private, public, protected where appropriate - Add an additional tab stop to end of functions for additional args/params - Remove array kvps since arrays can be associative or just values * Add try catch finally php snippet * Add param php snippet * Add php func snippets - Add fun for a general function out of scope of class - Add fun_anonymous for anonymous functions - Add fun_arrow for arrow functions * Add promotion style constructor to php snippets * Add property snippet to php snippets * Add spaceship snippet to php snippets * Add goto snippet to php snippets * Add class_const snippet to php snippets * Add here/now doc snippets to php snippets * Add namespace related snippets to php snippets * Add superglobal snippets to php snippets * Add php 8 snippets - Add match expression - Add Attribute snippets - Add sensitive parameter - Add attribute target - Add attribute with target - Rename class_const to const - Add Dynamic Property Class snippet - Add enum snippets - Basic Enum - Backed Enum - foreach enum * Merge master to resolve conflict * Remove php snippets as candidates - Remove heredoc - Remove nowdoc - Remove php super global snippets - Remove namespace_path - Remove ethis (echo $this->) - Remove try/catch finally * Trim snippets --------- Co-authored-by: Rob Lourens --- extensions/php/snippets/php.code-snippets | 383 +++++++++++++--------- 1 file changed, 225 insertions(+), 158 deletions(-) diff --git a/extensions/php/snippets/php.code-snippets b/extensions/php/snippets/php.code-snippets index 3c213765b4b..7ac2cddcb08 100644 --- a/extensions/php/snippets/php.code-snippets +++ b/extensions/php/snippets/php.code-snippets @@ -1,8 +1,66 @@ { + "$… = ( … ) ? … : …": { + "prefix": "if?", + "body": "$${1:retVal} = (${2:condition}) ? ${3:a} : ${4:b} ;", + "description": "Ternary conditional assignment" + }, + "$… = array (…)": { + "prefix": "array", + "body": "$${1:arrayName} = array($0);", + "description": "Array initializer" + }, + "$… = […]": { + "prefix": "shorray", + "body": "$${1:arrayName} = [$0];", + "description": "Array initializer" + }, + "… => …": { + "prefix": "keyval,kvp", + "body": "'$1' => $2$0", + "description": "Key-Value pair" + }, + "$a <=> $b": { + "prefix": "spaceship", + "body": "(${1:$$a} <=> ${2:$$b} === ${3|0,1,-1|})", + "description": "Spaceship equality check" + }, + "attribute": { + "prefix": "attr", + "body": [ + "#[\\\\Attribute]", + "class ${1:My}Attribute${2: extends ${3:MyOther}Attribute} {", + "\t$0", + "}" + ], + "description": "Attribute" + }, + "attribute target": { + "prefix": "attr_target", + "body": "\\Attribute::${1|TARGET_ALL,TARGET_CLASS,TARGET_FUNCTION,TARGET_METHOD,TARGET_PROPERTY,TARGET_CLASS_CONSTANT,TARGET_PARAMETER,IS_REPEATABLE|}$0" + }, + "attribute with target": { + "prefix": "attr_with_target", + "body": [ + "#[\\\\Attribute(\\Attribute::${1|TARGET_ALL,TARGET_CLASS,TARGET_FUNCTION,TARGET_METHOD,TARGET_PROPERTY,TARGET_CLASS_CONSTANT,TARGET_PARAMETER,IS_REPEATABLE|}$2)]", + "class ${3:My}Attribute${4: extends ${5:MyOther}Attribute} {", + "\t$0", + "}" + ], + "description": "Attribute - Chain targets with attr_target snippet" + }, + "case …": { + "prefix": "case", + "body": [ + "case '${1:value}':", + "\t${0:# code...}", + "\tbreak;" + ], + "description": "Case Block" + }, "class …": { "prefix": "class", "body": [ - "class ${1:ClassName} ${2:extends ${3:AnotherClass}} ${4:implements ${5:Interface}}", + "${1:${2|final ,readonly |}}class ${3:${TM_FILENAME_BASE}}${4: extends ${5:AnotherClass}} ${6:implements ${7:Interface}}", "{", "\t$0", "}", @@ -10,88 +68,29 @@ ], "description": "Class definition" }, - "PHPDoc class …": { - "prefix": "doc_class", - "isFileTemplate": true, + "class function …": { + "prefix": "class_fun", "body": [ - "/**", - " * ${6:undocumented class}", - " */", - "class ${1:ClassName} ${2:extends ${3:AnotherClass}} ${4:implements ${5:Interface}}", + "${1|public ,private ,protected |}${2: static }function ${3:FunctionName}(${4:${5:${6:Type} }$${7:var}${8: = ${9:null}}}$10) : ${11:Returntype}", "{", - "\t$0", - "}", - "" + "\t${0:# code...}", + "}" ], - "description": "Documented Class Declaration" + "description": "Function for classes, traits and enums" }, - "function __construct": { - "prefix": "con", + "const": { + "prefix": "const", + "body": "${1|public ,private ,protected |}const ${2:NAME} = $3;", + "description": "Constant for classes, traits, enums" + }, + "enum": { + "prefix": "enum", "body": [ - "${1:public} function __construct(${2:${3:Type} $${4:var}${5: = ${6:null}}}) {", - "\t\\$this->${4:var} = $${4:var};$0", - "}" + "enum $1 {", + "\tcase $2;$0", + "}" ] }, - "PHPDoc property": { - "prefix": "doc_v", - "body": [ - "/** @var ${1:Type} $${2:var} ${3:description} */", - "${4:protected} $${2:var}${5: = ${6:null}};$0" - ], - "description": "Documented Class Variable" - }, - "PHPDoc function …": { - "prefix": "doc_f", - "isFileTemplate": true, - "body": [ - "/**", - " * ${1:undocumented function summary}", - " *", - " * ${2:Undocumented function long description}", - " *", - "${3: * @param ${4:Type} $${5:var} ${6:Description}}", - "${7: * @return ${8:type}}", - "${9: * @throws ${10:conditon}}", - " **/", - "${11:public }function ${12:FunctionName}(${13:${14:${4:Type} }$${5:var}${15: = ${16:null}}})", - "{", - "\t${0:# code...}", - "}" - ], - "description": "Documented function" - }, - "PHPDoc param …": { - "prefix": "param", - "body": [ - "* @param ${1:Type} ${2:var} ${3:Description}$0" - ], - "description": "Parameter documentation" - }, - "function …": { - "prefix": "fun", - "body": [ - "${1:public }function ${2:FunctionName}(${3:${4:${5:Type} }$${6:var}${7: = ${8:null}}})", - "{", - "\t${0:# code...}", - "}" - ], - "description": "Function" - }, - "trait …": { - "prefix": "trait", - "body": [ - "/**", - " * $1", - " */", - "trait ${2:TraitName}", - "{", - "\t$0", - "}", - "" - ], - "description": "Trait" - }, "define(…, …)": { "prefix": "def", "body": [ @@ -109,14 +108,41 @@ ], "description": "Do-While loop" }, - "while …": { - "prefix": "while", + "for …": { + "prefix": "for", "body": [ - "while (${1:$${2:a} <= ${3:10}}) {", + "for ($${1:i}=${2:0}; $${1:i} < $3; $${1:i}++) { ", "\t${0:# code...}", "}" ], - "description": "While-loop" + "description": "For-loop" + }, + "foreach …": { + "prefix": "foreach", + "body": [ + "foreach ($${1:variable} as $${2:key}${3: => $${4:value}}) {", + "\t${0:# code...}", + "}" + ], + "description": "Foreach loop" + }, + "function": { + "prefix": "fun", + "body": [ + "function ${1:FunctionName}($2)${3: : ${4:Returntype}} {", + "\t$0", + "}" + ], + "description": "Function - use param snippet for parameters" + }, + "anonymous function": { + "prefix": "fun_anonymous", + "body": [ + "function ($1)${2: use ($${3:var})} {", + "\t$0", + "}" + ], + "description": "Anonymous Function" }, "if …": { "prefix": "if", @@ -139,61 +165,101 @@ ], "description": "If Else block" }, - "$… = ( … ) ? … : …": { - "prefix": "if?", - "body": "$${1:retVal} = (${2:condition}) ? ${3:a} : ${4:b} ;", - "description": "Ternary conditional assignment" - }, - "else …": { - "prefix": "else", + "match": { + "prefix": "match", "body": [ - "else {", + "match (${1:expression}) {", + "\t$2 => $3,", + "\t$4 => $5,$0", + "}" + ], + "description": "Match expression; like switch with identity checks. Use keyval snippet to chain expressions" + }, + "param": { + "prefix": "param", + "body": "${1:Type} $${2:var}${3: = ${4:null}}$5", + "description": "Parameter definition" + }, + "property": { + "prefix": "property", + "body": "${1|public ,private ,protected |}${2|static ,readonly |}${3:Type} $${4:var}${5: = ${6:null}};$0", + "description": "Property" + }, + "PHPDoc class …": { + "prefix": "doc_class", + "body": [ + "/**", + " * ${8:undocumented class}", + " */", + "${1:${2|final ,readonly |}}class ${3:${TM_FILENAME_BASE}}${4: extends ${5:AnotherClass}} ${6:implements ${7:Interface}}", + "{", + "\t$0", + "}", + "" + ], + "description": "Documented Class Declaration" + }, + "PHPDoc function …": { + "prefix": "doc_fun", + "body": [ + "/**", + " * ${1:undocumented function summary}", + " *", + " * ${2:Undocumented function long description}", + " *", + "${3: * @param ${4:Type} $${5:var} ${6:Description}}", + "${7: * @return ${8:type}}", + "${9: * @throws ${10:conditon}}", + " **/", + "${11:public }function ${12:FunctionName}(${13:${14:${4:Type} }$${5:var}${15: = ${16:null}}}17)", + "{", "\t${0:# code...}", "}" ], - "description": "Else block" + "description": "Documented function" }, - "elseif …": { - "prefix": "elseif", + "PHPDoc param …": { + "prefix": "doc_param", "body": [ - "elseif (${1:condition}) {", - "\t${0:# code...}", - "}" + "* @param ${1:Type} ${2:var} ${3:Description}$0" ], - "description": "Elseif block" + "description": "Paramater documentation" }, - "for …": { - "prefix": "for", + "PHPDoc trait": { + "prefix": "doc_trait", "body": [ - "for ($${1:i}=${2:0}; $${1:i} < $3; $${1:i}++) { ", - "\t${0:# code...}", - "}" + "/**", + " * $1", + " */", + "trait ${2:TraitName}", + "{", + "\t$0", + "}", + "" ], - "description": "For-loop" + "description": "Trait" }, - "foreach …": { - "prefix": "foreach", + "PHPDoc var": { + "prefix": "doc_var", "body": [ - "foreach ($${1:variable} as $${2:key} ${3:=> $${4:value}}) {", - "\t${0:# code...}", - "}" + "/** @var ${1:Type} $${2:var} ${3:description} */", + "${4:protected} $${2:var}${5: = ${6:null}};$0" ], - "description": "Foreach loop" + "description": "Documented Class Variable" }, - "$… = array (…)": { - "prefix": "array", - "body": "$${1:arrayName} = array('$2' => $3${4:,} $0);", - "description": "Array initializer" + "Region End": { + "prefix": "#endregion", + "body": [ + "#endregion" + ], + "description": "Folding Region End" }, - "$… = […]": { - "prefix": "shorray", - "body": "$${1:arrayName} = ['$2' => $3${4:,} $0];", - "description": "Array initializer" - }, - "… => …": { - "prefix": "keyval", - "body": "'$1' => $2${3:,} $0", - "description": "Key-Value initializer" + "Region Start": { + "prefix": "#region", + "body": [ + "#region" + ], + "description": "Folding Region Start" }, "switch …": { "prefix": "switch", @@ -210,46 +276,16 @@ ], "description": "Switch block" }, - "case …": { - "prefix": "case", + "trait …": { + "prefix": "trait", "body": [ - "case '${1:value}':", - "\t${0:# code...}", - "\tbreak;" + "trait ${1:TraitName}", + "{", + "\t$0", + "}", + "" ], - "description": "Case Block" - }, - "$this->…": { - "prefix": "this", - "body": "\\$this->$0;", - "description": "$this->..." - }, - "echo $this->…": { - "prefix": "ethis", - "body": "echo \\$this->$0;", - "description": "Echo this" - }, - "Throw Exception": { - "prefix": "throw", - "body": [ - "throw new $1Exception(${2:\"${3:Error Processing Request}\"}${4:, ${5:1}});", - "$0" - ], - "description": "Throw exception" - }, - "Region Start": { - "prefix": "#region", - "body": [ - "#region" - ], - "description": "Folding Region Start" - }, - "Region End": { - "prefix": "#endregion", - "body": [ - "#endregion" - ], - "description": "Folding Region End" + "description": "Trait" }, "Try Catch Block": { "prefix": "try", @@ -261,5 +297,36 @@ "}" ], "description": "Try catch block" + }, + "use function": { + "prefix": "use_fun", + "body": "use function $1;" + }, + "use const": { + "prefix": "use_const", + "body": "use const $1;" + }, + "use grouping": { + "prefix": "use_group", + "body": [ + "use${1| const , function |}$2\\{", + "\t$0,", + "}" + ], + "description": "Use grouping imports" + }, + "use as ": { + "prefix": "use_as", + "body": "use${1| const , function |}$2 as $3;", + "description": "Use as alias" + }, + "while …": { + "prefix": "while", + "body": [ + "while (${1:$${2:a} <= ${3:10}}) {", + "\t${0:# code...}", + "}" + ], + "description": "While-loop" } }