Strict typed snippets for PHP getters that check declare(strict_types=1) or for g:ultisnips_php_scalar_types=1

This commit is contained in:
Mathew Attlee 2016-06-22 10:30:27 +01:00
parent 15d7e5ec26
commit fff3ae4061
2 changed files with 13 additions and 2 deletions

View File

@ -1,5 +1,14 @@
priority -50 priority -50
global !p
import vim
# Set g:ultisnips_php_scalar_types to 1 if you'd like to enable PHP 7's scalar types for return values
def isPHPScalarTypesEnabled():
isEnabled = vim.eval("get(g:, 'ultisnips_php_scalar_types', 0)") == "1"
return isEnabled or re.match('<\?php\s+declare\(strict_types=[01]\);', '\n'.join(vim.current.window.buffer))
endglobal
## Snippets from SnipMate, taken from ## Snippets from SnipMate, taken from
## https://github.com/scrooloose/snipmate-snippets.git ## https://github.com/scrooloose/snipmate-snippets.git
@ -9,7 +18,7 @@ snippet gm "PHP Class Getter" b
* *
* @return ${2:string} * @return ${2:string}
*/ */
public function get${1/\w+\s*/\u$0/}() public function get${1/\w+\s*/\u$0/}()`!p snip.rv = ': '+t[2] if isPHPScalarTypesEnabled() else ''`
{ {
return $this->$1; return $this->$1;
} }
@ -36,7 +45,7 @@ snippet gs "PHP Class Getter Setter" b
* *
* @return ${2:string} * @return ${2:string}
*/ */
public function get${1/\w+\s*/\u$0/}() public function get${1/\w+\s*/\u$0/}()`!p snip.rv = ': '+t[2] if isPHPScalarTypesEnabled() else ''`
{ {
return $this->$1; return $this->$1;
} }

View File

@ -2,6 +2,8 @@ snippet <?
<?php <?php
${0} ${0}
snippet dst "declare(strict_types=1)"
declare(strict_types=${1:1});
snippet ec snippet ec
echo ${0}; echo ${0};
snippet <?e snippet <?e