Passed
Push — master ( 080caa...0a057b )
by Glynn
03:29 queued 01:18
created
src/Services/View/PHP_Engine.php 1 patch
Spacing   +50 added lines, -50 removed lines patch added patch discarded remove patch
@@ -51,8 +51,8 @@  discard block
 block discarded – undo
51 51
 	 *
52 52
 	 * @param string $base_view_path
53 53
 	 */
54
-	public function __construct( string $base_view_path ) {
55
-		$this->base_view_path = $this->verify_view_path( $base_view_path );
54
+	public function __construct(string $base_view_path) {
55
+		$this->base_view_path = $this->verify_view_path($base_view_path);
56 56
 	}
57 57
 
58 58
 	/**
@@ -61,7 +61,7 @@  discard block
 block discarded – undo
61 61
 	 * @param Component_Compiler $compiler
62 62
 	 * @return void
63 63
 	 */
64
-	public function set_component_compiler( Component_Compiler $compiler ): void {
64
+	public function set_component_compiler(Component_Compiler $compiler): void {
65 65
 		$this->component_compiler = $compiler;
66 66
 	}
67 67
 
@@ -73,12 +73,12 @@  discard block
 block discarded – undo
73 73
 	 * @param bool $print
74 74
 	 * @return string|void
75 75
 	 */
76
-	public function render( string $view, iterable $data, bool $print = true ) {
77
-		$view = $this->resolve_file_path( $view );
78
-		if ( $print ) {
79
-			print( $this->render_buffer( $view, $data ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
76
+	public function render(string $view, iterable $data, bool $print = true) {
77
+		$view = $this->resolve_file_path($view);
78
+		if ($print) {
79
+			print($this->render_buffer($view, $data)); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
80 80
 		} else {
81
-			return $this->render_buffer( $view, $data );
81
+			return $this->render_buffer($view, $data);
82 82
 		}
83 83
 	}
84 84
 
@@ -88,21 +88,21 @@  discard block
 block discarded – undo
88 88
 	 * @param Component $component
89 89
 	 * @return string|void
90 90
 	 */
91
-	public function component( Component $component, bool $print = true ) {
91
+	public function component(Component $component, bool $print = true) {
92 92
 
93 93
 		// Throw exception of no compiler passed.
94
-		if ( ! is_a( $this->component_compiler, Component_Compiler::class ) ) {
95
-			throw new Exception( 'No component compiler passed to PHP_Engine' );
94
+		if ( ! is_a($this->component_compiler, Component_Compiler::class)) {
95
+			throw new Exception('No component compiler passed to PHP_Engine');
96 96
 		}
97 97
 
98 98
 		// Compile the component.
99
-		$compiled = $this->component_compiler->compile( $component );
100
-		$template = $this->maybe_resolve_dot_notation( $compiled->template() );
101
-		$view     = sprintf( '%s%s.php', \DIRECTORY_SEPARATOR, $this->clean_filename( $template ) );
102
-		if ( $print ) {
103
-			print( $this->render_buffer( $view, $compiled->data() ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
99
+		$compiled = $this->component_compiler->compile($component);
100
+		$template = $this->maybe_resolve_dot_notation($compiled->template());
101
+		$view     = sprintf('%s%s.php', \DIRECTORY_SEPARATOR, $this->clean_filename($template));
102
+		if ($print) {
103
+			print($this->render_buffer($view, $compiled->data())); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
104 104
 		} else {
105
-			return $this->render_buffer( $view, $compiled->data() );
105
+			return $this->render_buffer($view, $compiled->data());
106 106
 		}
107 107
 	}
108 108
 
@@ -113,8 +113,8 @@  discard block
 block discarded – undo
113 113
 	 * @param View_Model $view_model
114 114
 	 * @return string|void
115 115
 	 */
116
-	public function view_model( View_Model $view_model, bool $print = true ) {
117
-		return $this->render( $view_model->template(), $view_model->data(), $print );
116
+	public function view_model(View_Model $view_model, bool $print = true) {
117
+		return $this->render($view_model->template(), $view_model->data(), $print);
118 118
 	}
119 119
 
120 120
 	/**
@@ -125,11 +125,11 @@  discard block
 block discarded – undo
125 125
 	 * @param bool $print
126 126
 	 * @return string|void
127 127
 	 */
128
-	public function partial( string $view, iterable $data = array(), bool $print = true ) {
129
-		if ( $print ) {
130
-			$this->render( $view, $data, $print );
128
+	public function partial(string $view, iterable $data = array(), bool $print = true) {
129
+		if ($print) {
130
+			$this->render($view, $data, $print);
131 131
 		} else {
132
-			return $this->render( $view, $data, $print );
132
+			return $this->render($view, $data, $print);
133 133
 		}
134 134
 	}
135 135
 
@@ -141,23 +141,23 @@  discard block
 block discarded – undo
141 141
 	 * @return string
142 142
 	 * @throws Exception
143 143
 	 */
144
-	protected function render_buffer( string $view, iterable $__data ): string {
144
+	protected function render_buffer(string $view, iterable $__data): string {
145 145
 
146
-		if ( ! file_exists( $view ) ) {
147
-			throw new Exception( "{$view} doesn't exist" );
146
+		if ( ! file_exists($view)) {
147
+			throw new Exception("{$view} doesn't exist");
148 148
 		}
149 149
 
150 150
 		$output = '';
151 151
 		ob_start();
152 152
 
153 153
 		// Set all the data values a parameters.
154
-		foreach ( $__data as $__key => $__value ) {
155
-			if ( is_string( $__key ) ) {
156
-				${\wp_strip_all_tags( $__key )} = $__value;
154
+		foreach ($__data as $__key => $__value) {
155
+			if (is_string($__key)) {
156
+				${\wp_strip_all_tags($__key)} = $__value;
157 157
 			}
158 158
 
159 159
 			// Unset the key and value.
160
-			unset( $__key, $__value, $__data );
160
+			unset($__key, $__value, $__data);
161 161
 		}
162 162
 
163 163
 		include $view;
@@ -172,10 +172,10 @@  discard block
 block discarded – undo
172 172
 	 * @param string $file
173 173
 	 * @return string
174 174
 	 */
175
-	protected function clean_filename( string $file ): string {
176
-		$file = ltrim( $file, '/' );
177
-		return substr( $file, -4 ) === '.php'
178
-			? substr( $file, 0, -4 )
175
+	protected function clean_filename(string $file): string {
176
+		$file = ltrim($file, '/');
177
+		return substr($file, -4) === '.php'
178
+			? substr($file, 0, -4)
179 179
 			: $file;
180 180
 
181 181
 	}
@@ -186,12 +186,12 @@  discard block
 block discarded – undo
186 186
 	 * @param string $filename
187 187
 	 * @return string
188 188
 	 */
189
-	protected function resolve_file_path( string $filename ): string {
190
-		$filename = $this->maybe_resolve_dot_notation( $filename );
189
+	protected function resolve_file_path(string $filename): string {
190
+		$filename = $this->maybe_resolve_dot_notation($filename);
191 191
 		return sprintf(
192 192
 			'%s%s.php',
193 193
 			$this->base_view_path,
194
-			$this->clean_filename( $filename )
194
+			$this->clean_filename($filename)
195 195
 		);
196 196
 	}
197 197
 
@@ -201,13 +201,13 @@  discard block
 block discarded – undo
201 201
 	 * @param string $filename
202 202
 	 * @return string
203 203
 	 */
204
-	protected function maybe_resolve_dot_notation( string $filename ): string {
205
-		if ( $this->str_ends_with( '.php', $filename ) ) {
206
-			$filename = substr( $filename, 0, -4 );
204
+	protected function maybe_resolve_dot_notation(string $filename): string {
205
+		if ($this->str_ends_with('.php', $filename)) {
206
+			$filename = substr($filename, 0, -4);
207 207
 		}
208 208
 
209
-		$parts    = explode( '.', $filename );
210
-		$filename = implode( DIRECTORY_SEPARATOR, $parts );
209
+		$parts    = explode('.', $filename);
210
+		$filename = implode(DIRECTORY_SEPARATOR, $parts);
211 211
 
212 212
 		return $filename;
213 213
 	}
@@ -219,9 +219,9 @@  discard block
 block discarded – undo
219 219
 	 * @param string $haystack
220 220
 	 * @return bool
221 221
 	 */
222
-	protected function str_ends_with( string $needle, string $haystack ): bool {
223
-		$needle_len = strlen( $needle );
224
-		return ( $needle_len === 0 || 0 === substr_compare( $haystack, $needle, - $needle_len ) );
222
+	protected function str_ends_with(string $needle, string $haystack): bool {
223
+		$needle_len = strlen($needle);
224
+		return ($needle_len === 0 || 0 === substr_compare($haystack, $needle, - $needle_len));
225 225
 	}
226 226
 
227 227
 
@@ -232,12 +232,12 @@  discard block
 block discarded – undo
232 232
 	 * @return string
233 233
 	 * @throws Exception
234 234
 	 */
235
-	protected function verify_view_path( string $path ): string {
236
-		$path = $this->maybe_resolve_dot_notation( $path );
237
-		$path = rtrim( $path, '/' ) . '/';
235
+	protected function verify_view_path(string $path): string {
236
+		$path = $this->maybe_resolve_dot_notation($path);
237
+		$path = rtrim($path, '/') . '/';
238 238
 
239
-		if ( ! \is_dir( $path ) ) {
240
-			throw new Exception( "{$path} doesn't exist and cant be used as the base view path." );
239
+		if ( ! \is_dir($path)) {
240
+			throw new Exception("{$path} doesn't exist and cant be used as the base view path.");
241 241
 		}
242 242
 
243 243
 		return $path;
Please login to merge, or discard this patch.