aboutsummaryrefslogtreecommitdiff
path: root/vim/bundle/vim-snippets/snippets/php.snippets
blob: 4faa167314e7f6d2f5b3ec8eb34bb729dd67d6da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
snippet <?
	<?php

	${0}
snippet ec
	echo ${0};
snippet <?e
	<?php echo ${0} ?>
# this one is for php5.4
snippet <?=
	<?=${0}?>
snippet ?=
	<?= ${0} ?>
snippet ?
	<?php ${0} ?>
snippet ?f
	<?php foreach ($${1:vars} as $${2:$var}): ?>
		${0}
	<?php endforeach ?>
snippet ?i
	<?php if ($${1:var}): ?>
		${0}
	<?php endif ?>
snippet ns
	namespace ${1:Foo\Bar\Baz};
	${0}
snippet c
	class ${1:`vim_snippets#Filename()`}
	{
		${0}
	}
snippet i
	interface ${1:`vim_snippets#Filename()`}
	{
		${0}
	}
snippet t.
	$this->
snippet f
	function ${1}(${3})
	{
		${0}
	}
# method
snippet m
	${1:protected} function ${2:foo}()
	{
		${0}
	}
snippet sm "PHP Class Setter"
	/**
	 * Sets the value of ${1:foo}
	 *
	 * @param ${2:string} $$1 ${3:description}
	 *
	 * @return ${4:`vim_snippets#Filename()`}
	 */
	${5:public} function set${6:$1}(${7:$2 }$$1)
	{
		$this->${8:$1} = $$1;

		return $this;
	}
snippet gm "PHP Class Getter Setter"
	/**
	 * Gets the value of ${1:foo}
	 *
	 * @return ${2:string}
	 */
	${3:public} function get${4:$1}()
	{
		return $this->${5:$1};
	}
#setter
snippet $s
	${1:$foo}->set${2:Bar}(${0});
#getter
snippet $g
	${1:$foo}->get${0:Bar}();
# Tertiary conditional
snippet =?:
	$${1:foo} = ${2:true} ? ${3:a} : ${0};
snippet ?:
	${1:true} ? ${2:a} : ${0}
snippet t "$retVal = (condition) ? a : b"
	$${1:retVal} = (${2:condition}) ? ${3:a} : ${4:b};
# Predefined variables
snippet C
	$_COOKIE['${1:variable}']
snippet E
	$_ENV['${1:variable}']
snippet F
	$_FILES['${1:variable}']
snippet G
	$_GET['${1:variable}']
snippet P
	$_POST['${1:variable}']
snippet R
	$_REQUEST['${1:variable}']
snippet S
	$_SERVER['${1:variable}']
snippet SS
	$_SESSION['${1:variable}']
snippet get "get"
	$_GET['${1}']
snippet post "post"
	$_POST['${1}']
snippet session "session"
	$_SESSION['${1}']
# the following are old ones
snippet inc
	include '${1:file}';
snippet inc1
	include_once '${1:file}';
snippet req
	require '${1:file}';
snippet req1
	require_once '${1:file}';
# Start Docblock
snippet /*
	/**
	 * ${0}
	 */
# Class - post doc
snippet doc_cp
	/**
	 * ${1:undocumented class}
	 *
	 * @package ${2:default}
	 * @subpackage ${3:default}
	 * @author ${4:`g:snips_author`}
	 */
# Class Variable - post doc
snippet doc_vp
	/**
	 * ${1:undocumented class variable}
	 *
	 * @var ${2:string}
	 */
# Class Variable
snippet doc_v
	/**
	 * ${3:undocumented class variable}
	 *
	 * @var ${4:string}
	 */
	${1:var} $${2};
# Class
snippet doc_c
	/**
	 * ${3:undocumented class}
	 *
	 * @package ${4:default}
	 * @subpackage ${5:default}
	 * @author ${6:`g:snips_author`}
	 */
	${1:}class ${2:}
	{
		${0}
	} // END $1class $2
# Constant Definition - post doc
snippet doc_dp
	/**
	 * ${1:undocumented constant}
	 */
# Constant Definition
snippet doc_d
	/**
	 * ${3:undocumented constant}
	 */
	define(${1}, ${2});
# Function - post doc
snippet doc_fp
	/**
	 * ${1:undocumented function}
	 *
	 * @return ${2:void}
	 * @author ${3:`g:snips_author`}
	 */
# Function signature
snippet doc_s
	/**
	 * ${4:undocumented function}
	 *
	 * @return ${5:void}
	 * @author ${6:`g:snips_author`}
	 */
	${1}function ${2}(${3});
# Function
snippet doc_f
	/**
	 * ${4:undocumented function}
	 *
	 * @return ${5:void}
	 * @author ${6:`g:snips_author`}
	 */
	${1}function ${2}(${3})
	{${0}
	}
# Header
snippet doc_h
	/**
	 * ${1}
	 *
	 * @author ${2:`g:snips_author`}
	 * @version ${3:$Id$}
	 * @copyright ${4:$2}, `strftime('%d %B, %Y')`
	 * @package ${0:default}
	 */
snippet doc_i "interface someClass {}"
	/**
	 * $1
	 * @package ${2:default}
	 * @author ${3:`!v g:snips_author`}
	 **/
	interface ${1:someClass}
	{${4}
	}
snippet inheritdoc "@inheritdoc docblock"
	/**
	 * {@inheritdoc}
	 */
# Interface
snippet interface
	/**
	 * ${2:undocumented class}
	 *
	 * @package ${3:default}
	 * @author ${4:`g:snips_author`}
	 */
	interface ${1:`vim_snippets#Filename()`}
	{
		${0}
	}
# Trait
snippet trait
	/**
	 * ${2:undocumented class}
	 *
	 * @package ${3:default}
	 * @author ${4:`g:snips_author`}
	 */
	trait ${1:`vim_snippets#Filename()`}
	{
		${0}
	}
# class ...
snippet class
	/**
	 * ${1}
	 */
	class ${2:`vim_snippets#Filename()`}
	{
		${3}
		/**
		 * ${4}
		 */
		${5:public} function ${6:__construct}(${7:argument})
		{
			${0}
		}
	}
snippet nc
	namespace ${1:`substitute(substitute(expand("%:h"), '\v^\w+\/(\u)', '\1', ''), '\/', '\\\', 'g')`};

	${2:abstract }class ${3:`vim_snippets#Filename()`}
	{
		${0}
	}
# define(...)
snippet def "define('VARIABLE_NAME', 'definition')"
	define('${1:VARIABLE_NAME}', ${2:'definition'});
# defined(...)
snippet def?
	${1}defined('${2}')
snippet wh "while (condition) { ... }"
	while (${1:/* condition */}) {
		${0}
	}
snippet do "do { ... } while (condition)"
	do {
		${0}
	} while (${1});
snippet if "if (condition) { ... }"
	if (${1}) {
		${0}
	}
snippet ifn "if (!condition) { ... }"
	if (!${1}) {
		${2}
	}
snippet ifil "<?php if (condition): ?> ... <?php endif; ?>"
	<?php if (${1}): ?>
		${0}
	<?php endif; ?>
snippet ife "if (cond) { ... } else { ... }"
	if (${1}) {
		${2}
	} else {
		${3}
	}
	${0}
snippet ifeil "<?php if (condition): ?> ... <?php else: ?> ... <?php endif; ?>"
	<?php if (${1}): ?>
		${2}
	<?php else: ?>
		${3}
	<?php endif; ?>
	${0}
snippet el "else { ... }"
	else {
		${0}
	}
snippet eif "elseif(condition) { ... }"
	elseif (${1}) {
		${0}
	}
snippet switch "switch($var) { case 'xyz': ... default: .... }"
	switch ($${1:variable}) {
		case '${2:value}':
			${3}
			break;
		${0}
		default:
			${4}
			break;
	}
snippet case "case 'value': ... break"
	case '${1:value}':
		${2}
		break;
snippet for "for ($i = 0; $i < $count; $i++) { ... }"
	for ($${2:i} = 0; $$2 < ${1:count}; $$2${3:++}) {
		${0}
	}
snippet foreach "foreach ($var as $value) { .. }"
	foreach ($${1:variable} as $${2:value}) {
		${0}
	}
snippet foreachil "<?php foreach ($var as $value): ?>  ... <?php endforeach; ?>"
	<?php foreach ($${1:variable} as $${2:value}): ?>
		${0}
	<?php endforeach; ?>
snippet foreachk "foreach ($var as $key => $value) { .. }"
	foreach ($${1:variable} as $${2:key} => $${3:value}) {
		${0}
	}
snippet foreachkil "<?php foreach ($var as $key => $value): ?>  ... <?php endforeach; ?>"
	<?php foreach ($${1:variable} as $${2:key} => $${3:value}): ?>
		${0:<!-- html... -->}
	<?php endforeach; ?>
snippet array "$... = array(...)"
	$${1:arrayName} = array('${2}' => ${3});
snippet try "try { ... } catch (Exception $e) { ... }"
	try {
		${0}
	} catch (${1:Exception} $e) {
	}
# lambda with closure
snippet lambda
	${1:static }function (${2:args}) use (${3:&$x, $y /*put vars in scope (closure) */}) {
		${0}
	};
# pre_dump();
snippet pd
	echo '<pre>'; var_dump(${0}); echo '</pre>';
# pre_dump(); die();
snippet pdd
	echo '<pre>'; var_dump(${1}); echo '</pre>'; die(${0:});
snippet vd
	var_dump(${0});
snippet vdd
	var_dump(${1}); die(${0:});
snippet pr
	print_r(${0});
snippet prs
	print_r(${0}, 1);
snippet vdf
	error_log(print_r($${1:foo}, true), 3, '${2:/tmp/debug.log}');
snippet http_redirect
	header ("HTTP/1.1 301 Moved Permanently");
	header ("Location: ".URL);
	exit();
snippet log "error_log(var_export($var, true));"
	error_log(var_export(${1}, true));
snippet var "var_export($var)"
	var_export(${1});
snippet ve "Dumb debug helper in HTML"
	echo '<pre>' . var_export(${1}, 1) . '</pre>';
snippet pc "Dumb debug helper in cli"
	var_export($1);$0
# Getters & Setters
snippet gs "PHP Class Getter Setter"
	/**
	 * Gets the value of ${1:foo}
	 *
	 * @return ${2:string}
	 */
	public function get${3:$1}()
	{
		return $this->${4:$1};
	}

	/**
	 * Sets the value of $1
	 *
	 * @param $2 $$1 ${5:description}
	 *
	 * @return ${6:`vim_snippets#Filename()`}
	 */
	public function set$3(${7:$2 }$$1)
	{
		$this->$4 = $$1;
		return $this;
	}
# anotation, get, and set, useful for doctrine
snippet ags
	/**
	 * ${1:description}
	 *
	 * @${0}
	 */
	${2:protected} $${3:foo};

	public function get${4:$3}()
	{
		return $this->$3;
	}

	public function set$4(${5:$4 }$${6:$3})
	{
		$this->$3 = $$6;
		return $this;
	}
snippet rett
	return true;
snippet retf
	return false;
snippet am
	$${1:foo} = array_map(function($${2:v}) {
		${0}
		return $$2;
	}, $$1);
snippet aw
	array_walk($${1:foo}, function(&$${2:v}, $${3:k}) {
		$$2 = ${0};
	});
# static var assign once
snippet static_var
	static $${1} = null;
	if (is_null($$1)){
		$$1 = ${2};
	}
snippet CSVWriter
	<?php
	
	class CSVWriter {
		public function __construct($file_or_handle, $sep = "\t", $quot = '"'){
			$args = func_get_args();
			$mode = isset($opts['mode']) ? $opts['mode'] : 'w';
	
			$this->f =
				is_string($file_or_handle)
				? fopen($file_or_handle, $mode)
				: $file_or_handle;
	
			$this->fputcsv_args = array($this->f, null, $sep, $quot);
	
			if (!$this->f) throw new Exception('bad file descriptor');
		}
	
		public function write($row){
			$this->fputcsv_args[1] =& $row;
			call_user_func_array('fputcsv', $this->fputcsv_args);
		}
	
		public function close(){
			if (!is_null($this->f))
				fclose($this->f);
			$this->f = null;
		}
	
		public function __destruct(){
			$this->close();
		}
	
	}
snippet CSVIterator
	
	// http://snipplr.com/view.php?codeview&id=1986 // modified
	class CSVIterator implements Iterator
	{	
		private $f;
		private $curr;
		private $rowCounter;
	
		 /* opts keys:
			* row_size
			* escape
			* enclosure
			* delimiter
			*/
		public function __construct( $file_or_handle, $opts = array(4096, ',') )
		{
			$d = function($n) use(&$opts){ return isset($opts[$n]) ? $opts[$n] : false; };
	
			$this->combine = $d('combine');
			$this->headers = $d('headers');
			$this->headerCheckFunction = $d('header_check_function');
	
			$this->f =
				is_string($file_or_handle)
				? fopen( $file_or_handle, 'r' )
				: $file_or_handle;
			if (!$this->f) throw new Exception('bad file descriptor');
			$this->fgetcsv_args = array(
					$this->f,
					isset($opts['row_size']) ? $opts['row_size'] : 4096,
					isset($opts['delimiter']) ? $opts['delimiter'] : ',',
					isset($opts['enclosure']) ? $opts['enclosure'] : '"',
					isset($opts['escape']) ? $opts['escape'] : '\\',
			);
			$this->start();
		}
	
		protected function readRow(){
			$this->curr = call_user_func_array('fgetcsv', $this->fgetcsv_args );
			$this->rowCounter++;
			if ($this->rowCounter == 1){
				$this->processHeader();
			} elseif ($this->curr) {
				$this->processRow();
			}
		}
	
		public function processHeader(){
			if ($this->headers || $this->combine){
				$this->header = $this->curr;
				if ($this->headerCheckFunction){
					$f = $this->headerCheckFunction;
					$f($this->header);
				}
				$this->readRow();
			}
		}
	
		public function processRow(){
			if ($this->combine)
				$this->curr = array_combine($this->header, $this->curr);
		}
	
		public function start(){
			$this->rowCounter = 0;
			rewind( $this->f );
			$this->readRow();
		}
	
		public function rewind()
		{
			$this->start();
		}
	
		public function current()
		{
			$curr = $this->curr;
			$this->readRow();
			return $curr;
		}
	
		public function key()
		{
			return $this->rowCounter;
		}
	
		public function next()
		{
			return $this->curr;
		}
	
		public function valid(){
			if( !$this->next() )
			{
				fclose( $this->f );
				return FALSE;
			}
			return TRUE;
		}
	
	} // end class
# phpunit
snippet ase "$this->assertEquals()"
	$this->assertEquals(${1:expected}, ${2:actual});
snippet asne "$this->assertNotEquals()"
	$this->assertNotEquals(${1:expected}, ${2:actual});
snippet asf "$this->assertFalse()"
	$this->assertFalse(${1});
snippet ast "$this->assertTrue()"
	$this->assertTrue(${1});
snippet asfex "$this->assertFileExists()"
	$this->assertFileExists(${1:'path/to/file'});
snippet asfnex "$this->assertFileNotExists()"
	$this->assertFileNotExists(${1:'path/to/file'});
snippet ascon "$this->assertContains()"
	$this->assertContains(${1:$needle}, ${2:$haystack});
snippet ashk "$this->assertArrayHasKey()"
	$this->assertArrayHasKey(${1:$key}, ${2:$array});
snippet asnhk "$this->assertArrayNotHasKey()"
	this->assertArrayNotHasKey(${1:$key}, ${2:$array});
snippet ascha "$this->assertClassHasAttribute()"
	$this->assertClassHasAttribute(${1:$attributeName}, '${2:$className}');
snippet asi "$this->assertInstanceOf(...)"
	$this->assertInstanceOf(${1:expected}, ${2:actual});
snippet test "public function testXYZ() { ... }"
	public function test${1}()
	{
		${0}
	}
snippet setup "protected function setUp() { ... }"
	protected function setUp()
	{
		${0}
	}
snippet teardown "protected function tearDown() { ... }"
	protected function tearDown()
	{
		${0}
	}
snippet exp "phpunit expects"
	expects($this->${1:once}())
		->method('${2}')
		->with($this->equalTo(${3})${4})
		->will($this->returnValue(${5}));
snippet testcmt "phpunit comment with group"
	/**
	* @group ${1}
	*/
snippet fail "$this->fail()"
	$this->fail(${1});
snippet marki "$this->markTestIncomplete()"
	$this->markTestIncomplete(${1});
snippet marks "$this->markTestSkipped()"
	$this->markTestSkipped(${1});
# end of phpunit snippets
snippet te "throw new Exception()"
	throw new ${1:Exception}("${2:Error Processing Request}");
snippet fpc "file_put_contents" b
	file_put_contents(${1:file}, ${2:content}${3:, FILE_APPEND});$0
snippet sr "str_replace"
	str_replace(${1:search}, ${2:replace}, ${3:subject})$0
snippet ia "in_array"
	in_array(${1:needle}, ${2:haystack})$0
snippet is "isset"
	isset(${1:var})$0
snippet isa "isset array"
	isset($${1:array}[${2:key}])$0
snippet in "is_null"
	is_null($${1:var})$0
snippet fe "file_exists"
	file_exists(${1:file})$0
snippet id "is_dir"
	is_dir(${1:path})$0