Completed
Push — master ( 86855a...8ad9a8 )
by
unknown
7s
created

TimberFunctionWrapper::add_to_twig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 9
rs 9.6666
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
3
class TimberFunctionWrapper {
4
5
	private $_class;
6
	private $_function;
7
	private $_args;
8
	private $_use_ob;
9
10
	public function __toString() {
11
		 try {
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 2 tabs, found 2
Loading history...
12
			return (string)$this->call();
0 ignored issues
show
introduced by
No space after closing casting parenthesis is prohibited
Loading history...
13
		 } catch (Exception $e) {
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 3 tabs, found 2
Loading history...
14
		 	return 'Caught exception: ' . $e->getMessage() . "\n";
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 4 tabs, found 3
Loading history...
15
		 }
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 3 tabs, found 2
Loading history...
16
	}
17
18
	/**
19
	 *
20
	 *
21
	 * @param callable $function
22
	 * @param array   $args
23
	 * @param bool    $return_output_buffer
24
	 */
25
	public function __construct( $function, $args = array(), $return_output_buffer = false ) {
26
		if( is_array( $function ) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
27
			if( (is_string( $function[0] ) && class_exists( $function[0] ) ) || gettype( $function[0] ) === 'object' ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
introduced by
Found "=== '". Use Yoda Condition checks, you must
Loading history...
28
				$this->_class = $function[0];
29
			}
30
			
31
			if( is_string( $function[1] ) ) $this->_function = $function[1];
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
32
		} else {
33
			$this->_function = $function;
34
		}
35
36
		$this->_args = $args;
37
		$this->_use_ob = $return_output_buffer;
38
39
		add_filter( 'get_twig', array( &$this, 'add_to_twig' ) );
40
	}
41
42
	/**
43
	 *
44
	 *
45
	 * @param Twig_Environment $twig
46
	 * @return Twig_Environment
47
	 */
48
	public function add_to_twig( $twig ) {
49
		$wrapper = $this;
50
51
		$twig->addFunction( new Twig_SimpleFunction( $this->_function, function () use ( $wrapper ) {
52
					return call_user_func_array( array( $wrapper, 'call' ), func_get_args() );
53
				} ) );
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 2 tabs, found 4
Loading history...
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 8 spaces, but found 16.
Loading history...
54
55
		return $twig;
56
	}
57
58
	/**
59
	 *
60
	 *
61
	 * @return string
62
	 */
63
	public function call() {
64
		$args = $this->_parse_args( func_get_args(), $this->_args );
65
		$callable = ( isset( $this->_class ) ) ? array( $this->_class, $this->_function ) : $this->_function;
66
67
		if ( $this->_use_ob ) {
68
			return TimberHelper::ob_function( $callable, $args );
69
		} else {
70
			return call_user_func_array( $callable, $args );
71
		}
72
	}
73
74
	/**
75
	 *
76
	 *
77
	 * @param array   $args
78
	 * @param array   $defaults
79
	 * @return array
80
	 */
81
	private function _parse_args( $args, $defaults ) {
82
		$_arg = reset( $defaults );
83
84
		foreach ( $args as $index => $arg ) {
85
			$defaults[$index] = is_null( $arg ) ? $_arg : $arg;
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
86
			$_arg = next( $defaults );
87
		}
88
89
		return $defaults;
90
	}
91
92
}
93