Skip to content

Instantly share code, notes, and snippets.

@ilkermutlu
Forked from mattstauffer/.vimrc
Last active June 7, 2016 11:29
Show Gist options
  • Save ilkermutlu/59ae285edf8763bfa9af101335fad482 to your computer and use it in GitHub Desktop.
Save ilkermutlu/59ae285edf8763bfa9af101335fad482 to your computer and use it in GitHub Desktop.
Vimscripts to easily create PHP class and easily add dependency to PHP classTweaked versions of a script by Jeffrey Way
" Note: To add this to your .vimrc, you'll have to delete any character that is written with a caret (e.g. "^M") and enter the real value; to enter ^M, enter insert mode and type CTRL-V CTRL-M.
" Easily create class.
function! Class()
let name = input('Class Name? ')
let namespace = input('Any Namespace? ')
if strlen(namespace)
exec 'normal i<?php namespace ' . namespace . ';^M^M^['
else
exec 'normal i<?php^M^M^['
endif
" Open class
exec 'normal iclass ' . name . ' {^M}^[O^['
exec 'normal i^M public function __construct()^M {^M ^M }^M^[kkA'
endfunction
nmap <leader>1 :call Class()<cr>
function! AddDependency()
let dependency = input('Var Name: ')
let namespace = input('Class Path: ')
let segments = split(namespace, '\')
let typehint = segments[-1]
exec 'normal gg/construct^M%i, ' . typehint . ' $' . dependency . '^[/}^MO$this->^[a' . dependency . ' = $' . dependency . ';^[==o^[?{^MkO protected $' . dependency . ';^M^[?{^MOuse ' . namespace . ';^M^['
" Remove opening comma if there is only one dependency
exec 'normal :%s/(, /(/ge^M'
endfunction
nmap <leader>2 :call AddDependency()<cr>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment